void CreateRigidBodyStack(int count) { const float mass = 10.0f; CompoundShape cylinderCompound = new CompoundShape(); CollisionShape cylinderShape = new CylinderShapeX(4, 1, 1); CollisionShape boxShape = new BoxShape(4, 1, 1); cylinderCompound.AddChildShape(Matrix.Identity, boxShape); Quaternion orn = Quaternion.RotationYawPitchRoll((float)Math.PI / 2.0f, 0.0f, 0.0f); Matrix localTransform = Matrix.RotationQuaternion(orn); //localTransform *= Matrix.Translation(new Vector3(1,1,1)); cylinderCompound.AddChildShape(localTransform, cylinderShape); CollisionShape[] shape = { cylinderCompound, new BoxShape(new Vector3(1, 1, 1)), new SphereShape(1.5f) }; for (int i = 0; i < count; ++i) { LocalCreateRigidBody(mass, Matrix.Translation(0, 2 + 6 * i, 0), shape[i % shape.Length]); } }
public override CollisionShape CopyCollisionShape() { CylinderShape cs = new CylinderShapeX(halfExtent.ToBullet()); cs.LocalScaling = m_localScaling.ToBullet(); return(cs); }
public override CollisionShape GetCollisionShape() { if (collisionShapePtr == null) { collisionShapePtr = new CylinderShapeX(halfExtent.ToBullet()); ((CylinderShapeX)collisionShapePtr).LocalScaling = m_localScaling.ToBullet(); } return(collisionShapePtr); }
//---------------------------------------------------------------------------------------------------------------- public override void RenderSceneAll(GameTime gameTime) { IndexedVector3 halfExtents = new IndexedVector3(wheelWidth, wheelRadius, wheelRadius); CylinderShapeX wheelShape = new CylinderShapeX(ref halfExtents); IndexedVector3 wheelColor = new IndexedVector3(1, 0, 0); IndexedVector3 worldBoundsMin, worldBoundsMax; GetDynamicsWorld().GetBroadphase().GetBroadphaseAabb(out worldBoundsMin, out worldBoundsMax); for (int i = 0; i < m_vehicle.GetNumWheels(); i++) { //synchronize the wheels with the (interpolated) chassis worldtransform m_vehicle.UpdateWheelTransform(i, true); //draw wheels (cylinders) IndexedMatrix m = m_vehicle.GetWheelInfo(i).m_worldTransform; m_shapeDrawer.DrawXNA(ref m, m_wheelShape, ref wheelColor, m_debugDraw.GetDebugMode(), ref worldBoundsMin, ref worldBoundsMax, ref m_lookAt, ref m_perspective); } base.RenderSceneAll(gameTime); }
/// <summary> /// Creates a Rigid Body from a .bxda file /// </summary> /// <param name="FilePath"></param> public void CreateRigidBody(string FilePath) { CollisionShape shape; WheelDriverMeta wheel = null; DefaultMotionState motion; BXDAMesh mesh = new BXDAMesh(); mesh.ReadFromFile(FilePath); Vector3 loc; Quaternion rot = Quaternion.Identity; //Is it a wheel? if ((wheel = GetSkeletalJoint()?.cDriver?.GetInfo <WheelDriverMeta>()) != null && true) { //Align the cylinders Vector3 min, max; GetShape(mesh).GetAabb(Matrix4.Identity, out min, out max); Vector3 extents = max - min; //Find the thinnest dimension, that is probably wheat the cylinder should be aligned to if (extents.X < extents.Y) //X or Z { if (extents.X < extents.Z) { shape = new CylinderShapeX(wheel.width, wheel.radius, wheel.radius); //X } else { shape = new CylinderShapeZ(wheel.radius, wheel.radius, wheel.width); //Z } } else //Y or Z { if (extents.Y < extents.Z) { shape = new CylinderShape(wheel.radius, wheel.width, wheel.radius); //Y } else { shape = new CylinderShapeZ(wheel.radius, wheel.radius, wheel.width); //Z } } loc = MeshUtilities.MeshCenter(mesh); } //Rigid Body Construction else { shape = GetShape(mesh); loc = MeshUtilities.MeshCenter(mesh); } if (debug) { Console.WriteLine("Rotation is " + rot); } motion = new DefaultMotionState(Matrix4.CreateTranslation(loc + new Vector3(0, 100, 0))); RigidBodyConstructionInfo info = new RigidBodyConstructionInfo(mesh.physics.mass, motion, shape, shape.CalculateLocalInertia(mesh.physics.mass)); //Temp? info.Friction = 100; info.RollingFriction = 100; //info.AngularDamping = 0f; //info.LinearDamping = 0.5f; BulletObject = new RigidBody(info); }
//---------------------------------------------------------------------------------------------------------------- public override void InitializeDemo() { CollisionShape groundShape = new BoxShape(new IndexedVector3(50, 0.1f, 50)); IndexedVector3 wheelDimensions = new IndexedVector3(wheelWidth, wheelRadius, wheelRadius); m_wheelShape = new CylinderShapeX(ref wheelDimensions); m_collisionShapes.Add(groundShape); m_collisionConfiguration = new DefaultCollisionConfiguration(); m_dispatcher = new CollisionDispatcher(m_collisionConfiguration); IndexedVector3 worldMin = new IndexedVector3(-1000, -1000, -1000); IndexedVector3 worldMax = new IndexedVector3(1000, 1000, 1000); m_broadphase = new AxisSweep3Internal(ref worldMin, ref worldMax, 0xfffe, 0xffff, 16384, null, false); m_constraintSolver = new SequentialImpulseConstraintSolver(); m_dynamicsWorld = new DiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_constraintSolver, m_collisionConfiguration); IndexedMatrix tr = IndexedMatrix.CreateTranslation(0, -10, 0); LocalCreateRigidBody(0f, ref tr, groundShape); #if true CollisionShape chassisShape = new BoxShape(new IndexedVector3(1.0f, 0.5f, 2.0f)); //CollisionShape chassisShape = new BoxShape(new IndexedVector3(1.0f, 0.5f, 1.0f)); m_collisionShapes.Add(chassisShape); CompoundShape compound = new CompoundShape(); m_collisionShapes.Add(compound); //localTrans effectively shifts the center of mass with respect to the chassis IndexedMatrix localTrans = IndexedMatrix.CreateTranslation(0, 1, 0); compound.AddChildShape(ref localTrans, chassisShape); { CollisionShape suppShape = new BoxShape(new IndexedVector3(0.5f, 0.1f, 0.5f)); //localTrans effectively shifts the center of mass with respect to the chassis IndexedMatrix suppLocalTrans = IndexedMatrix.CreateTranslation(0f, 1.0f, 2.5f); compound.AddChildShape(ref suppLocalTrans, suppShape); } tr._origin = IndexedVector3.Zero; m_carChassis = LocalCreateRigidBody(800f, ref tr, compound);//chassisShape); #endif { #if true CollisionShape liftShape = new BoxShape(new IndexedVector3(0.5f, 2.0f, 0.05f)); m_collisionShapes.Add(liftShape); m_liftStartPos = new IndexedVector3(0.0f, 2.5f, 3.05f); IndexedMatrix liftTrans = IndexedMatrix.CreateTranslation(m_liftStartPos); m_liftBody = LocalCreateRigidBody(10f, ref liftTrans, liftShape); IndexedMatrix localA = MathUtil.SetEulerZYX(0f, MathUtil.SIMD_HALF_PI, 0f); localA._origin = new IndexedVector3(0f, 1.0f, 3.05f); IndexedMatrix localB = MathUtil.SetEulerZYX(0f, MathUtil.SIMD_HALF_PI, 0f); localB._origin = new IndexedVector3(0f, -1.5f, -0.05f); m_liftHinge = new HingeConstraint(m_carChassis, m_liftBody, ref localA, ref localB); MathUtil.PrintMatrix(BulletGlobals.g_streamWriter, "hinge aFrame", m_liftHinge.GetAFrame()); MathUtil.PrintMatrix(BulletGlobals.g_streamWriter, "hinge bFrame", m_liftHinge.GetBFrame()); m_liftHinge.SetLimit(0.0f, 0.0f); m_dynamicsWorld.AddConstraint(m_liftHinge, true); CollisionShape forkShapeA = new BoxShape(new IndexedVector3(1.0f, 0.1f, 0.1f)); m_collisionShapes.Add(forkShapeA); CompoundShape forkCompound = new CompoundShape(); m_collisionShapes.Add(forkCompound); IndexedMatrix forkLocalTrans = IndexedMatrix.Identity; forkCompound.AddChildShape(ref forkLocalTrans, forkShapeA); CollisionShape forkShapeB = new BoxShape(new IndexedVector3(0.1f, 0.02f, 0.6f)); m_collisionShapes.Add(forkShapeB); forkLocalTrans = IndexedMatrix.CreateTranslation(-0.9f, -0.08f, 0.7f); forkCompound.AddChildShape(ref forkLocalTrans, forkShapeB); CollisionShape forkShapeC = new BoxShape(new IndexedVector3(0.1f, 0.02f, 0.6f)); m_collisionShapes.Add(forkShapeC); forkLocalTrans = IndexedMatrix.CreateTranslation(0.9f, -0.08f, 0.7f); forkCompound.AddChildShape(ref forkLocalTrans, forkShapeC); m_forkStartPos = new IndexedVector3(0.0f, 0.6f, 3.2f); IndexedMatrix forkTrans = IndexedMatrix.CreateTranslation(m_forkStartPos); m_forkBody = LocalCreateRigidBody(5f, ref forkTrans, forkCompound); localA = IndexedMatrix.Identity; localB = IndexedMatrix.Identity; localA._basis.SetEulerZYX(0, 0, MathUtil.SIMD_HALF_PI); localA._origin = new IndexedVector3(0.0f, -1.9f, 0.05f); localB._basis.SetEulerZYX(0, 0, MathUtil.SIMD_HALF_PI); localB._origin = new IndexedVector3(0.0f, 0.0f, -0.1f); m_forkSlider = new SliderConstraint(m_liftBody, m_forkBody, ref localA, ref localB, true); m_forkSlider.SetLowerLinLimit(0.1f); m_forkSlider.SetUpperLinLimit(0.1f); // m_forkSlider.setLowerAngLimit(-LIFT_EPS); // m_forkSlider.setUpperAngLimit(LIFT_EPS); m_forkSlider.SetLowerAngLimit(0.0f); m_forkSlider.SetUpperAngLimit(0.0f); m_dynamicsWorld.AddConstraint(m_forkSlider, true); #endif #if true CompoundShape loadCompound = new CompoundShape(false); m_collisionShapes.Add(loadCompound); CollisionShape loadShapeA = new BoxShape(new IndexedVector3(2.0f, 0.5f, 0.5f)); m_collisionShapes.Add(loadShapeA); IndexedMatrix loadTrans = IndexedMatrix.Identity; loadCompound.AddChildShape(ref loadTrans, loadShapeA); CollisionShape loadShapeB = new BoxShape(new IndexedVector3(0.1f, 1.0f, 1.0f)); m_collisionShapes.Add(loadShapeB); loadTrans = IndexedMatrix.CreateTranslation(2.1f, 0.0f, 0.0f); loadCompound.AddChildShape(ref loadTrans, loadShapeB); CollisionShape loadShapeC = new BoxShape(new IndexedVector3(0.1f, 1.0f, 1.0f)); m_collisionShapes.Add(loadShapeC); loadTrans = IndexedMatrix.CreateTranslation(-2.1f, 0.0f, 0.0f); loadCompound.AddChildShape(ref loadTrans, loadShapeC); m_loadStartPos = new IndexedVector3(0.0f, -3.5f, 7.0f); loadTrans = IndexedMatrix.CreateTranslation(m_loadStartPos); m_loadBody = LocalCreateRigidBody(4f, ref loadTrans, loadCompound); #endif } //m_carChassis.setDamping(0.2f, 0.2f); ClientResetScene(); /// create vehicle { m_vehicleRayCaster = new DefaultVehicleRaycaster(m_dynamicsWorld); m_vehicle = new RaycastVehicle(m_tuning, m_carChassis, m_vehicleRayCaster); ///never deactivate the vehicle m_carChassis.SetActivationState(ActivationState.DISABLE_DEACTIVATION); m_dynamicsWorld.AddVehicle(m_vehicle); float connectionHeight = 1.2f; bool isFrontWheel = true; //choose coordinate system m_vehicle.SetCoordinateSystem(rightIndex, upIndex, forwardIndex); IndexedVector3 connectionPointCS0 = IndexedVector3.Zero; //connectionPointCS0 = new IndexedVector3(CUBE_HALF_EXTENTS, connectionHeight, CUBE_HALF_EXTENTS); //m_vehicle.addWheel(ref connectionPointCS0, ref wheelDirectionCS0, ref wheelAxleCS, suspensionRestLength, wheelRadius, m_tuning, isFrontWheel); connectionPointCS0 = new IndexedVector3(CUBE_HALF_EXTENTS - (0.3f * wheelWidth), connectionHeight, 2.0f * CUBE_HALF_EXTENTS - wheelRadius); m_vehicle.AddWheel(ref connectionPointCS0, ref wheelDirectionCS0, ref wheelAxleCS, suspensionRestLength, wheelRadius, m_tuning, isFrontWheel); connectionPointCS0 = new IndexedVector3(-CUBE_HALF_EXTENTS + (0.3f * wheelWidth), connectionHeight, 2.0f * CUBE_HALF_EXTENTS - wheelRadius); m_vehicle.AddWheel(ref connectionPointCS0, ref wheelDirectionCS0, ref wheelAxleCS, suspensionRestLength, wheelRadius, m_tuning, isFrontWheel); //isFrontWheel = false; connectionPointCS0 = new IndexedVector3(-CUBE_HALF_EXTENTS + (0.3f * wheelWidth), connectionHeight, -2.0f * CUBE_HALF_EXTENTS + wheelRadius); m_vehicle.AddWheel(ref connectionPointCS0, ref wheelDirectionCS0, ref wheelAxleCS, suspensionRestLength, wheelRadius, m_tuning, isFrontWheel); connectionPointCS0 = new IndexedVector3(CUBE_HALF_EXTENTS - (0.3f * wheelWidth), connectionHeight, -2.0f * CUBE_HALF_EXTENTS + wheelRadius); m_vehicle.AddWheel(ref connectionPointCS0, ref wheelDirectionCS0, ref wheelAxleCS, suspensionRestLength, wheelRadius, m_tuning, isFrontWheel); for (int i = 0; i < m_vehicle.GetNumWheels(); i++) { WheelInfo wheel = m_vehicle.GetWheelInfo(i); wheel.m_suspensionStiffness = suspensionStiffness; wheel.m_wheelsDampingRelaxation = suspensionDamping; wheel.m_wheelsDampingCompression = suspensionCompression; wheel.m_frictionSlip = wheelFriction; wheel.m_rollInfluence = rollInfluence; } } SetCameraDistance(36.0f); SetTexturing(true); SetShadows(true); }