BeRo1985 / kraft

Kraft Physics Engine is an open source Object Pascal physics engine library that can be used in 3D games.
107 stars 21 forks source link

Centre of Mass not working with compound objects .. #32

Open PascalCorpsman opened 1 year ago

PascalCorpsman commented 1 year ago

Dear Benjamin,

i created this example: https://github.com/PascalCorpsman/kraft_examples/tree/main/001_Compound which creates a rigid body containing two "simple" shapes. Than i adjusted the center of mass and let it fall down on the floor. As soon the object touches the ground it completly freaks out and is shoot "away". If you look carefully you can see that it is rotation around the correct CG. But i expect it not to behave like this (i think it should fall down and then fall over to the side). As following i post the important code parts:

Procedure TForm1.CreateWorld;
Var
  RigidBody: TKraftRigidBody;
  Shape: TKraftShape;
  Floor: TKraftRigidBody;
  FloorShape: TKraftShapePlane; // A Plane infinite in width
Begin
  (*
   * Each element in the Physiks Engine is a TKraftRigidBody
   * it can consists of multiple shapes.
   * A Shape is a container vor a hull and connects the Body with the hull ?
   *)
  // 1. Floor Creation
  Floor := TKraftRigidBody.Create(KraftWorld);
  Floor.SetRigidBodyType(krbtSTATIC);
  // Planes are defined by their normal vector and distance to the Origin.
  FloorShape := TKraftShapePlane.Create(KraftWorld, Floor, Plane(Vector3Norm(Vector3(0.0, 1.0, 0.0)), 0.0));
  FloorShape.Restitution := 0.3; // TODO: Why, it is static ?
  FloorShape.Density := 1.0; // TODO: Why, it is static ?
  Floor.ForcedMass := 0.01; // TODO: Why, it is static ?
  Floor.Finish;
  // Move the Floor plane to the Needed position ;)
  Floor.SetWorldTransformation(Matrix4x4Translate(0.0, 0.0, 0.0));
  (*
   * Create a box consisting of to convex hulls
   *)
  RigidBody := TKraftRigidBody.Create(KraftWorld);
  RigidBody.SetRigidBodyType(krbtSTATIC); // Init as static Object
  (*
   * Lets form a ****
   *                *
   *                *
   *                *
   *                * structure
   *)
  shape := TKraftShapeBox.Create(KraftWorld, RigidBody, Vector3(0.5, 2, 0.5));
  Shape.Restitution := 0.3;
  Shape.Density := 1.0;
  Shape.LocalTransform := Matrix4x4Translate(0, 2, 0);
  Shape.ForcedMass := 1;
  // Shape.SynchronizeTransform; -- is this needed ?
  Shape.Finish;
  shape := TKraftShapeBox.Create(KraftWorld, RigidBody, Vector3(2, 0.5, 0.5));
  Shape.Restitution := 0.3;
  Shape.Density := 1.0;
  Shape.LocalTransform := Matrix4x4Translate(1.5, 4.5, 0);
  Shape.ForcedMass := 1;
  // Shape.SynchronizeTransform; -- is this needed ?
  Shape.Finish;
  RigidBody.ForcedMass := 10;
  RigidBody.Finish;
  RigidBody.SetWorldPosition(MoveableBoxStartingPosition);

  CompoundBox := RigidBody;

  // Set the Centre of mass 
  CompoundBox.ForcedCenterOfMass.Vector := Vector3(-ScrollBar1.Position / 50 + 1.5, 2, 0);
  CompoundBox.BodyInertiaTensor := Matrix3x3Identity;
  // Set the Physics Engine to use the given Centre of Mass and do not auto calc it by the shapes !
  CompoundBox.Flags := CompoundBox.Flags + [krbfHasForcedCenterOfMass];
  CompoundBox.Finish; // Update the internal data structures

// and make the Body Moveable
  CompoundBox.SetRigidBodyType(krbtDynamic); // krbtKinematic ??
  CompoundBox.Finish; 

End; 

So the question is, is this a Bug in the engine ? Do i use it wrong ? (and if so how is it done correct ?) Do you need further informations (if so the code of the complete example is linked at the top)