position and orientation of objects in compound body

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
grobi
Posts: 34
Joined: Wed Sep 23, 2009 5:47 pm

position and orientation of objects in compound body

Post by grobi »

Hi,
i got some difficulties with compoundbody.
I create a tank vehicle, the vehicle body is a compoundobject with the tanks body as convexhull.
That works so far, now i want to add a tank turret, which shall be rotateable on the tanks body.
I want to place the turret on top of the body but it gets always placed in the middle of the tanks body and i don`t understand why...

Code: Select all

modelBody = new CompoundBody(false);

// TANK BODY
List<Vector3> vertices = new List<Vector3>();
Matrix[] transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);
XNAutils.XNAutils.ExtractVectors(model.Meshes[0], vertices, transforms[model.Meshes[0].ParentBone.Index]);
ConvexHull cHull = new ConvexHull(vertices);

modelBody.addBody(cHull);

modelVehicle = new Vehicle(modelBody);

// TANK TURRET
vertices.Clear();
transforms = new Matrix[modelTurret.Bones.Count];
modelTurret.CopyAbsoluteBoneTransformsTo(transforms);
XNAutils.XNAutils.ExtractVectors(modelTurret.Meshes[0], vertices, transforms[modelTurret.Meshes[0].ParentBone.Index]);
Vector3 turretPosition = modelPosition;
turretPosition.Y += 15.0f;
cHull = new ConvexHull(vertices);
cHull.centerPosition = turretPosition;
modelBody.addBody(cHull);
//Matrix turretMatrix = (Matrix.CreateRotationX(MathHelper.ToRadians(modelRotation.X)) * Matrix.CreateRotationY(MathHelper.ToRadians(modelRotation.Y)) * Matrix.CreateRotationZ(MathHelper.ToRadians(modelRotation.Z))) * Matrix.CreateTranslation(turretPosition);
//modelBody.subBodies[1].internalWorldTransform = turretMatrix;

// TANK WHEELs
...

modelBody.becomeDynamic(400f);

modelMatrix = (Matrix.CreateRotationX(MathHelper.ToRadians(modelRotation.X)) * Matrix.CreateRotationY(MathHelper.ToRadians(modelRotation.Y)) * Matrix.CreateRotationZ(MathHelper.ToRadians(modelRotation.Z))) * Matrix.CreateTranslation(modelPosition);

modelVehicle.body.worldTransform = modelMatrix;
space.add(modelVehicle);
thx Grobi
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: position and orientation of objects in compound body

Post by Norbo »

I don't see anything obviously wrong in the code at a glance. Are the CompoundBody's SubBodyLocalOffsets and SubBodyLocalRotations reasonable? Keep in mind that they'll be measured from the center of mass.


Also, as a somewhat related suggestion, if the turret is actually going to be truly rotating, you might want to use a separate body attached using a constraint instead. That will ensure more physical behavior in case of collisions. Manually rotating a subbody isn't 'illegal,' but it does require some care.
Post Reply