///this can be useful to synchronize a single rigid body . graphics object public void SynchronizeSingleMotionState(RigidBody body) { Debug.Assert(body != null); if (body.GetMotionState() != null && !body.IsStaticOrKinematicObject()) { //we need to call the update at least once, even for sleeping objects //otherwise the 'graphics' transform never updates properly ///@todo: add 'dirty' flag //if (body.getActivationState() != ISLAND_SLEEPING) { Matrix interpolatedTransform = Matrix.Identity; TransformUtil.IntegrateTransform(body.GetInterpolationWorldTransform(), body.SetInterpolationLinearVelocity(), body.GetInterpolationAngularVelocity(), m_localTime * body.GetHitFraction(), ref interpolatedTransform); body.GetMotionState().SetWorldTransform(ref interpolatedTransform); } } }
private static void Main(string[] args) { var collisionConfig = new DefaultCollisionConfiguration(); var dispatcher = new CollisionDispatcher(collisionConfig); var pairCache = new DbvtBroadphase(); var solver = new SequentialImpulseConstraintSolver(); var dynamicsWorld = new DiscreteDynamicsWorld(dispatcher, pairCache, solver, collisionConfig); dynamicsWorld.Gravity = new Vector3(0, -10, 0); var groundShape = new BoxShape(new Vector3(50, 50, 50)); var groundTransform = Matrix.CreateTranslation(0, -56, 0); { float mass = 0; bool isDynamic = mass != 0; Vector3 localInertia = Vector3.Zero; if (isDynamic) localInertia = groundShape.CalculateLocalInertia(mass); var motionState = new DefaultMotionState(groundTransform); var rbinfo = new RigidBodyConstructionInfo(mass, motionState, groundShape, localInertia); var body = new RigidBody(rbinfo); dynamicsWorld.AddRigidBody(body); } { var collisionShape = new SphereShape(1); var transform = Matrix.Identity; float mass = 1; bool isDynamic = mass != 0; Vector3 localInertia = Vector3.Zero; if (isDynamic) localInertia = collisionShape.CalculateLocalInertia(mass); transform.Translation = new Vector3(2, 10, 0); var motionState = new DefaultMotionState(transform); var rbinfo = new RigidBodyConstructionInfo(mass, motionState, collisionShape, localInertia); var body = new RigidBody(rbinfo); dynamicsWorld.AddRigidBody(body); } Console.WriteLine("Starting simulation"); for (int i = 0; i < 100; i++) { dynamicsWorld.StepSimulation(1f / 60f, 10); for (int j = dynamicsWorld.GetNumCollisionObjects() - 1; j >= 0; j--) { var obj = dynamicsWorld.GetCollisionObjectArray()[j]; var body = (RigidBody)obj; if (body != null && body.GetMotionState() != null) { Matrix transform = Matrix.Identity; body.GetMotionState().GetWorldTransform(ref transform); Console.WriteLine("Object@{0} Position={1}", body.GetHashCode(), transform.Translation); } } } Console.Read(); }