Customizing the centerpoint of a convex hull shape

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
mcmonkey
Posts: 92
Joined: Fri Apr 17, 2015 11:42 pm

Customizing the centerpoint of a convex hull shape

Post by mcmonkey »

Let's say I'm constructing a convex hull shape, and I find the center point to not accurately represent the center of mass as well as I want it to.

What's the quickest / best way to force the center to a be at a specific point?

I noticed I can give it a custom convex shape description, but that has a lot of properties that I'd rather leave calculated for me, and the closest to what I need is the VolumeDistribution, which is a matrix, and I'm not sure how I would edit that to exactly place the center-of-mass...?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Customizing the centerpoint of a convex hull shape

Post by Norbo »

Entity.CollisionInformation.LocalPosition can be set to position the shape arbitrarily in the entity's local space.

If you want to position things precisely and need to know exactly where the ConvexHullShape recentered things on, use the ConvexHullShape constructor that outputs the center it computed.

(Notably, the volume distribution does not describe the center of mass, but only the distribution of mass around the center of mass. In fact, using a nonzero LocalPosition makes the volume distribution 'wrong', but that doesn't matter too much if all you're doing is trying to customize the feel.)
mcmonkey
Posts: 92
Joined: Fri Apr 17, 2015 11:42 pm

Re: Customizing the centerpoint of a convex hull shape

Post by mcmonkey »

Norbo wrote: Sat Jul 01, 2017 4:17 am Entity.CollisionInformation.LocalPosition can be set to position the shape arbitrarily in the entity's local space.

If you want to position things precisely and need to know exactly where the ConvexHullShape recentered things on, use the ConvexHullShape constructor that outputs the center it computed.

(Notably, the volume distribution does not describe the center of mass, but only the distribution of mass around the center of mass. In fact, using a nonzero LocalPosition makes the volume distribution 'wrong', but that doesn't matter too much if all you're doing is trying to customize the feel.)
That property is documented as merely offsetting the geometry from the center of mass... which doesn't quite help my case.

Based on your post above, the volume distribution is likely closer to what I need?

Though what I might need is both of them... my need here is to offset the center of mass with mass distributed in object space the same (and thus distributed differently relative to the new center point).

I want to make it so a force on the front tip of the object has an effect as if it were pushing very close to the center-of-mass (a weak rotation, and more linear velocity change), and a force on the back of the object has an effect as if it were pushing very far from the center-of-mass (a strong rotation, and little linear velocity change)

I tried setting the Location position to 4 units forward, and the object seemed to move and rotate exactly the same, but the rendering of it was offset from the objects "true" location.

My testing is a free floating object with a force countering gravity and some objects weighing it down in various positions

It's currently tipping the forward-end down at the same rate no matter what I do...

What I want is for the various objects to exactly counter each other out such that the object-with-attachments floats perfectly in place until explicitly disturbed.
mcmonkey
Posts: 92
Joined: Fri Apr 17, 2015 11:42 pm

Re: Customizing the centerpoint of a convex hull shape

Post by mcmonkey »

It might actually be an imperfection of the physics engine I'm correcting for here? Not sure, hopefully not but just in case I'll explain:

so I have the small objects jointed onto the large object in the air.

By all the physics I know of, this should be stable as far as rotation, and just fall flat until it hits something.

Instead it turns as though the weight of jointed-on objects is fighting the large object.


I have some custom forces applied to the large object (EG the one combatting gravity): it's probable that it's just that force needs to be somehow spread to the jointed objects too?
If this sounds right, what's the the cleanest way to ensure this spreads in a physically reasonable manner?

Pretty much just to ensure the object doesn't rotate arbitrarily.

Probably better than my initial plan of moving the center of mass to balance it out (Though I'd still like to know how to do that, just as a later reference...)
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Customizing the centerpoint of a convex hull shape

Post by Norbo »

Some background:
Offsetting the geometry from the local entity origin is equivalent to modifying the location of the center of mass. Using a LocalPosition of (0,1,0) is equivalent to a hypothetical center of mass offset of (0,-1,0).

Further, either of these equivalent operations can be considered a change in the mass distribution of the body. Without changing the inertia tensor (which is the 'VolumeDistribution' scaled by mass), a nonzero local position asserts that the integral over the mass has produced a center offset from the volumetric center. For that to be true, the shape must have a nonuniform mass distribution. If the center of mass is pushed out near the edge of the shape, the unmodified inertia tensor would imply that that side of the shape carries a disproportionate amount of mass.

(And of course you're permitted to set the LocalPosition to values such that the pivot of rotation is outside the collision geometry, which would imply some sort of nonsensical negative density in parts of the shape, but sometimes it's handy for gameplay purposes.)

The inertia tensor simply describes the layout of mass around the center of mass. Changing the inertia tensor can never change the center of mass.
I have some custom forces applied to the large object (EG the one combatting gravity): it's probable that it's just that force needs to be somehow spread to the jointed objects too?
If this sounds right, what's the the cleanest way to ensure this spreads in a physically reasonable manner?
So first, the physical interpretation of what it sounds like is going on:
In order to stop the system from gaining an angular velocity, an anti-gravity impulse must be applied at the system's center of mass, which is different from any constituent object's center of mass for multibody systems. And, if the system is dynamic and its pieces are able to move independently at all, the system's center of mass will change over time relative to the local space of any one shape.

You can compute the current center of mass of a system by taking the mass-weighted average of all constituent objects' centers of mass:

Code: Select all

weighted center = (position1 * mass1 + position2 * mass2 + ... + positionN * massN) / (mass1 + mass2 + ... + massN)
However, given that a constrained system isn't perfectly rigid, it is likely that small errors will accumulate over time that will result in sufficient angular velocity to topple it. To counteract that, you could dynamically scoot the anti-gravity impulse around, like spinning a plate on a pole. More easily, you could directly control the angular velocity with a constraint or something. If your goal is to make an unstable platform that tips when stood upon, you could make the angular constraint pretty weak. Or just remove the angular constraint upon interaction.

Or, if the only goal is to counteract gravity, then just set the linear velocity of every involved shape to oppose gravity. Or just turn off gravity for those shapes.
mcmonkey
Posts: 92
Joined: Fri Apr 17, 2015 11:42 pm

Re: Customizing the centerpoint of a convex hull shape

Post by mcmonkey »

Hm... it sounds like you're describing the right thing with the first part, but ... then my question is, what did I screw up to not see any changes from moving that position significantly out-of-line?

As for the alternate correction: you stated the problem yourself... a system is not sufficiently rigid as to do that.

Imagine this scene perhaps:

You have an airplane.
The plane is flying, and has a custom constraint keeping it aerial with a super simplified anti-gravity force loosely based on something about how wings work.
Now... you add wheels to this plane, wheels that are non-rigidly attached (with suspension and all). Or you add passengers or cargo inside the plane, which can move around inside the plane. Or you attach physically separate wing flaps for the purpose of animating the movement of the flaps better.

Suddenly, it's spinning midair because of all the objects weighing it down unevenly.

... How do you keep it not rotating in the air, except when explicitly trying to rotate (Via the flaps), or acted upon by an outside force (EG collision, wind, etc)?
The forces causing it to rotate are dynamic to the point of being a problem: you can't just apply the force to all of them, or their center as that's fairly well imperfect to the point of being noticeable thanks to non-rigidity (and that doesn't account for externally applied forces either, which may become involved). You can't dynamically counter the rotation because sometimes rotation is intended, you might not even be able to partially dynamically fight it as that would require knowing all forces on the plane as specifically they are caused by internal vs. external occurrences to the system.

My initial idea of just offsetting the center of mass for all purposes was a lazy but halfway possible fix : keep it re-aligned to the relevant position somehow calculated from the sum of all forces (my followup question to that would be, how do I calculate that ... heh)
But realistically that'd be intentionally offsetting it from reality to make it feel right, rather than offsetting it to the correct value for the object potentially...

Just disabling gravity for all attachments would definitely look wrong...

I'd like to keep this done somehow reasonably and generically though, as while I do actually intend to apply it to a plane in a similar situation, I have a few other cases where better natural balance will be helpful...
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Customizing the centerpoint of a convex hull shape

Post by Norbo »

what did I screw up to not see any changes from moving that position significantly out-of-line?
Were the graphics not aligned? It offsets the collision geometry from the center of mass, which is positioned at the entity's Position property. So if the graphical representation is centered on the entity's Position, it won't match. It should use the collidable's transform instead.
How do you keep it not rotating in the air, except when explicitly trying to rotate (Via the flaps), or acted upon by an outside force (EG collision, wind, etc)?
The easiest thing to do is just control the angular motion of the plane directly. Things like adding to the angular velocity, applying angular impulses, or using a constraint are all valid options.

Note that this does not mean that 'external influences' are necessarily overridden, and it does not require that you calculate what is an 'internal' versus 'external' influence. Just because a spaceship has some RCS thrusters doesn't mean that an asteroid impact won't make it spin- just that the spaceship has some degree of control. You can choose how much control that is.

You can get as detailed as you want, too. For example, you probably don't want the plane to bank while on a runway, so make its angular control proportional to its airspeed or something. Or maybe even check the airspeed across control surfaces and use that to determine how strong their influence would be, and use that to tune the angular control.

Or you could implement a full aerodynamic simulation that forces you to figure out how to actually design an airworthy plane, and, if you don't do well, it just spins out of control and crashes. But that seems hard! :P
mcmonkey
Posts: 92
Joined: Fri Apr 17, 2015 11:42 pm

Re: Customizing the centerpoint of a convex hull shape

Post by mcmonkey »

I'm past that point though?

I can rotate the plane and all, the problem is it rotates on its own due to semi-external forces (the weight of the wheels, etc. attachments), and I can't figure out how to make it not be rotating on its own...

I'm not sure if I'm explaining myself poorly or you're distracted, but I'm starting to feel a communication gap in this topic here :/

If that's still going on after this post, would it be alright if I made a short simulation in the demos to demonstrate what I mean and see if you can clean away the problem in the demo version? ( After which I'll move the fix to my live environment and thank you profusely :P )
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Customizing the centerpoint of a convex hull shape

Post by Norbo »

A real plane loaded with imbalanced cargo will also suffer from a tendency to rotate on its own- in fact, planes have crashed because of it. In less extreme cases, a pilot can compensate for this by using the control surfaces and engines to balance out the tail's excess weight, or to stop rolling influences from lateral imbalance, and so on.

The key insight is that the tools a real pilot has to control their angular motion can be boiled down into a single net torque/change in angular velocity for anything approximating a rigid system (and unless your plane is made of jello, it's close enough). Even if you did a really in-depth simulation, it would still boil down to a simple change in angular velocity. Just like a pilot can stop their plane from spinning out of the sky, a simulated plane can apply some angular change in velocity to keep things as desired.

The specifics of that control are up to the gameplay design, but it does work.

Edit:
And again, if you find that the angular impulses required to keep things reasonable are too great (analogy: the plane wouldn't fly in real life because the lift surfaces are badly designed), you can apply the lift impulses at arbitrary locations too. This actually still boils down to a change in angular velocity at the end of the day, but it can be more intuitive to apply a lift impulse at the system's center of mass (ideally affecting the dominant mass entity) and then use an angular velocity control scheme for the more minor variations.
would it be alright if I made a short simulation in the demos to demonstrate what I mean and see if you can clean away the problem in the demo version?
Sure, if you'd like, but I'd probably only go so far as to check for configuration problems that would break stuff, like fighting constraints that are creating uncontrollable impulses. The actual control scheme for this kind of thing is very game specific, so I wouldn't do much more than a quick test and 'yup that's about right'.
mcmonkey
Posts: 92
Joined: Fri Apr 17, 2015 11:42 pm

Re: Customizing the centerpoint of a convex hull shape

Post by mcmonkey »

That's... not really reasonable.
You're telling me to tell players to 'just deal with it', as opposed to helping me fix the fault?

Players will often have a keyboard, not a joystick, and thus can't hold it easily at position where it perfectly counters the downward rotation.

Further, real planes are generally balanced as much as possible - at least insofar as wing flaps and wheels - to minimize this issue.

I've yet to encounter the game where the plane rapidly spins downward and out of control by default because the landing wheels are placed too far forward relative to a center of mass that isn't quite right...

I guess if you're uninterested in helping, I'll go back to figuring out how to fiddle the center-of-mass to minimize this .-.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Customizing the centerpoint of a convex hull shape

Post by Norbo »

Please be more charitable when reading! It is very frustrating to be accused of not helping. If I didn't want to help, I would not respond. Please spend more time trying to understand what I'm trying to say.

I am giving you the physical basis for why the issue is occurring, and some options for how to deal with it at the level of simulation. I cannot design your game's control scheme, that part's up to you. Also, if you did not see it, check the edit to my previous post.
mcmonkey
Posts: 92
Joined: Fri Apr 17, 2015 11:42 pm

Re: Customizing the centerpoint of a convex hull shape

Post by mcmonkey »

Norbo wrote: Sun Jul 02, 2017 5:51 pm Please be more charitable when reading! It is very frustrating to be accused of not helping. If I didn't want to help, I would not respond. Please spend more time trying to understand what I'm trying to say.

I am giving you the physical basis for why the issue is occurring, and some options for how to deal with it at the level of simulation. I cannot design your game's control scheme, that part's up to you. Also, if you did not see it, check the edit to my previous post.
Sorry, I'm a bit frustrated with something else and may not be properly separating your innocent self from unrelated problems :/

I know you're just doing your best to get things answered and I should definitely be acting more grateful that you're responding (even at all), considering you're providing this service to the world for free (Though I've personally donated a bit, intend to do more, and hope other users will do the same).

I think what I'm missing with the center-of-mass offsetting is that I'm not accounting for it when spawning the wheels, and if I fix that (and also make it render at a more correct offset) it will rotate how I expect it to (by which I mean, not rotate on its own).

I just have to calculate the center of system's mass, which you've given the straightforward equation for, and offset the local position in that direction.

I think I can do this by the plan I had earlier (which is, center of mass offset for balance, using the local position as you explained) - (As best I can tell, correct me if I'm wrong, but we've figured out already that correcting things via magically system-compliant constraints wouldn't be a simple task).

<3 all the great work you do!

Sorry for my rudeness earlier too!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Customizing the centerpoint of a convex hull shape

Post by Norbo »

S'all good!

Notably, assigning a fixed center of mass offset with the LocalPosition and then applying lift on the entity's Position (i.e. just directly adjusting the LinearVelocity) can have more error than dynamically calculating the current center of mass and calling ApplyImpulse on the dominant mass with the system center of mass as the lift impulse location.

For example, after takeoff and gear up, a precomputed center of mass would no longer line up with the 'true' system center of mass because the landing gear moved.

And as you've found, dealing with maintaining consistency between all the different connections and transforms gets annoying.

Either way, the imbalance will reduce. Most likely not to exactly zero, but much closer. To give a concrete example of how a somewhat arcadey game could deal with the remainder: the player might specify a goal heading/orientation, and the angular impulses applied would try to reach that heading with some maximum strength. That way, the player doesn't have to continually compensate for the slight imbalance; the game's control scheme would handle the translation from input to angular impulse for the player.
Post Reply