Center Of Mass

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
DanStory
Posts: 7
Joined: Tue Aug 07, 2012 8:38 pm

Center Of Mass

Post by DanStory »

I found an answer (viewtopic.php?f=4&t=1795&p=10530) to set the center of mass but doing so moves the collision shape which then causes it to misalign from the render shape/model. And also the suspension/wheels.

Code: Select all

this.Entity.CollisionInformation.LocalPosition += vehicle.CenterOfMass;
Image


At first I though about doing this:

Code: Select all

this.Entity.Position -= vehicle.CenterOfMass;
this.Entity.CollisionInformation.LocalPosition += vehicle.CenterOfMass;
which just gave this result:
Image
the body collision shape (blue box) is causing the vehicle to sit in the air where the wheels can't touch the ground (however the wheels are now in the normal position)
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Center Of Mass

Post by Norbo »

Constraints operate on the center of mass, which is the entity.Position. Setting the entity.CollisionInformation.LocalPosition offsets the collision shape from the center of mass. So, to make the center of mass lower relative to the body, the body should be offset upwards.

If the wheel entities, constraints, and graphics are not configured to match the new collision shape offset, it won't be aligned right. Check out the SuspensionCarDemo for an example of the LocalPosition being used with a constraint car.

The Vehicle class, on the other hand, is more aware of collision shape offsets so the wheels adapt automatically. It would still require an offset on the graphic, though.
DanStory
Posts: 7
Joined: Tue Aug 07, 2012 8:38 pm

Re: Center Of Mass

Post by DanStory »

Think I need to reread the Shape recentering doc on codeplex and look over the demo for it, then get a better understanding of IGF's bepu integration with the SunBurn engine

rather new at this
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Center Of Mass

Post by Norbo »

Shape recentering takes place when a complex shape like a ConvexHullShape or MobileMeshShape is created from a dataset which produces a center of mass not aligned with the local origin. The shape computes a center of mass for the shape and all vertices/pieces are moved around such that the center of mass is the origin in the shape's local space.

Shape recentering does not affect boxes or other simple primitives. BoxShapes and most simple shapes just have dimensions which extend symmetrically out from the center of mass, so no extra recentering complexity is required.

Setting an entity.CollisionInformation.LocalPosition just scoots the collision shape away from the entity.Position. If an entity uses a BoxShape of dimensions (1,1,1), and the entity.CollisionInformation.LocalPosition is set to (0, 0.5, 0), then the bottom of the box shape will be aligned with the center of mass (the entity.Position) instead of the center of the box.
DanStory
Posts: 7
Joined: Tue Aug 07, 2012 8:38 pm

Re: Center Of Mass

Post by DanStory »

I got must everything working, but for the life of me can't think how to get the wheels in place.
At first I had it correct but once I set the center of mass at an extreme value it should I was completely wrong.

This is when my center of mass is y=-1:
Image
Looks all good, but

Then i changed it to y=-5:
Image

this is my model:
Image

Code: Select all

var height = this.ObjectBoundingBox.Max.X - this.ObjectBoundingBox.Min.X;
var radius = (this.ObjectBoundingBox.Max.Y - this.ObjectBoundingBox.Min.Y) * 0.5f;

//offset of center of mass and collision model
var diff = (this.Axle.Vehicle.Body.Entity.CollisionInformation.LocalPosition - this.Axle.Vehicle.Body.Entity.Position);

//position of wheel (center) in model space
var position = ((this.WorldBoundingBox.Max - this.WorldBoundingBox.Min) * 0.5f) + this.WorldBoundingBox.Min;

position += diff;

this.CollisionMove = new CylinderCollisionMove(this, height, radius);
this.Entity.Position = position;
this.Entity.Orientation = Quaternion.CreateFromAxisAngle(Vector3.Forward, MathHelper.PiOver2);
this.CollisionMove.LocalTransform *= Matrix.CreateTranslation(-(position - diff)) * Matrix.CreateRotationZ(-MathHelper.PiOver2);
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Center Of Mass

Post by Norbo »

Here's one thing that looks suspect:
var diff = (this.Axle.Vehicle.Body.Entity.CollisionInformation.LocalPosition - this.Axle.Vehicle.Body.Entity.Position);
LocalPosition is in the local space of the entity. So, if LocalPosition is (0, 1, 0) and the entity.Position is (1,1,0), then the center of the collision shape will be positioned at (1, 2, 0). If the entity were rotated 90 degrees around the z axis so that the local space Y axis pointed along the world space X axis, then the center of the collision shape will be positioned at (2,1,0). If you want the world space offset from the center of mass (entity.Position) to the center of the collision shape, then rotate the LocalPosition by the entity.Orientation.

Because of this, subtracting the center of mass (entity.Position) from the LocalPosition isn't really a valid operation; the vectors are in different spaces.

I would recommend trying to configure something more restricted in scope. For example, trying to fiddle with the SuspensionCarDemo would let you see the physical effects of individual changes without worrying about the graphics yet.
DanStory
Posts: 7
Joined: Tue Aug 07, 2012 8:38 pm

Re: Center Of Mass

Post by DanStory »

Thanks, after leaving it for the day and coming back to it. It all started to make sense and was able to get everything working.
Post Reply