/// ----------------------------------------------- /// PRECACHING & STATIC DATA VALUES /// ----------------------------------------------- /// public static string ClassName = "RigidBox"; private static bool HasCached = false; public static void Precache() { HasCached = MPrecacher.Precache(ClassName, HasCached); } /// <summary> /// MEDIA /// </summary> static private MModel StaticModel = MPrecacher.PrecacheModel(ClassName, "BBox.xml"); /// ----------------------------------------------- /// ----------------------------------------------- /// MNxActor myNxActor; public RigidBox(MWorld world) : base(world) { Precache(); MyModel = new MModel(); StaticModel.CreateNewInstance(MyModel); PhysicsFlags = PHYSICS_FLAGS.PHYS_RIGIDBODYDYANMICS; CollisionFlags = COLLISION_FLAGS.CF_MESH; mass = 1; }
public override void Tick() { // only create the NxActor if we haven't already created it if (myNxActor == null) { // init the NxBody description, give it our Actor mass MNxBodyDesc BodyDesc = new MNxBodyDesc(); BodyDesc.Mass = mass; // init the NxShape description (box), giving it the // identity AABB size of this Actor's Model MNxBoxShapeDesc boxDesc = new MNxBoxShapeDesc(); MyModel.SetTransform(MHelpers.IdentityMatrix); boxDesc.Dimensions = MyModel.GetWorldBBoxMax(); // init our NxActor description with the NxBody and NxShape MNxActorDesc ActorDesc = new MNxActorDesc(); ActorDesc.Body = BodyDesc; ActorDesc.AddShapeDesc(boxDesc); // create our NxActor myNxActor = MNxPhysics.CreateNxActor(ActorDesc, this); // copy the Actor's current transformation into the // initial transformation of our NxActor myNxActor.Position = Location; myNxActor.Orientation = Rotation; } // if this Actor isn't selected in Reality Builder, // then copy the NxActor's transformation into it if (!IsSelected) { Location = myNxActor.Position; Rotation = myNxActor.Orientation; } // otherwise copy its transformation into the NxActor, // since when we're dragging this Actor // around in Reality Builder, we want to have control over it else { myNxActor.Position = Location; myNxActor.Orientation = Rotation; } // pass Tick along to the base class base.Tick(); }
protected override void DisposeResources() { MNxPhysics.DestroyNxActor(myNxActor); myNxActor= null; base.DisposeResources(); }
static ushort WheelMaterialIndex; public static void Precache() { if (!HasCached) { MNxMaterial WheelMaterial = new MNxMaterial(); WheelMaterial.Restitution = 0.25f; WheelMaterial.DynamicFriction = 0.75f; WheelMaterial.StaticFriction = 0.90f; WheelMaterialIndex = MNxPhysics.AddMaterial(WheelMaterial); } HasCached = MPrecacher.Precache(ClassName, HasCached); }
... // init the NxShape description (box), giving it the // identity AABB size of this Actor's Model MNxBoxShapeDesc boxDesc = new MNxBoxShapeDesc(); MyModel.SetTransform(MHelpers.IdentityMatrix); boxDesc.Dimensions = MyModel.GetWorldBBoxMax(); // set our box shape's MaterialIndex to our custom WheelMaterialIndex // otherwise it would use the default Material at Index 0 boxDesc.MaterialIndex = WheelMaterialIndex; ...
/// ----------------------------------------------- /// PRECACHING & STATIC DATA VALUES /// ----------------------------------------------- /// public static string ClassName = "JointedBoxes"; private static bool HasCached = false; public static void Precache() { HasCached = MPrecacher.Precache(ClassName, HasCached); } /// <summary> /// MEDIA /// </summary> static private MModel StaticModel = MPrecacher.PrecacheModel(ClassName, "BBox.xml"); /// ----------------------------------------------- /// ----------------------------------------------- /// MNxActor myNxActor; MNxActor mySecondNxActor; MNxJoint myNxJoint; MModel mySecondModel;
public JointedBoxes(MWorld world) : base(world) { Precache(); MyModel = new MModel(); StaticModel.CreateNewInstance(MyModel); mySecondModel = new MModel(); StaticModel.CreateNewInstance(mySecondModel); PhysicsFlags = PHYSICS_FLAGS.PHYS_RIGIDBODYDYANMICS; CollisionFlags = COLLISION_FLAGS.CF_MESH; mass = 1; }
public override void Tick() { // only create the NxActor if we haven't already created it if (myNxActor == null) { // init the NxBody description, give it our Actor mass MNxBodyDesc BodyDesc = new MNxBodyDesc(); BodyDesc.Mass = mass; // init the NxShape description (box), giving it the // identity AABB size of this Actor's Model MNxBoxShapeDesc boxDesc = new MNxBoxShapeDesc(); MyModel.SetTransform(MHelpers.IdentityMatrix); boxDesc.Dimensions = MyModel.GetWorldBBoxMax(); // init our NxActor description with the NxBody and NxShape MNxActorDesc ActorDesc = new MNxActorDesc(); ActorDesc.Body = BodyDesc; ActorDesc.AddShapeDesc(boxDesc); // create our NxActor myNxActor = MNxPhysics.CreateNxActor(ActorDesc, this); // copy the Actor's current transformation into the // initial transformation of our NxActor myNxActor.Position = Location; myNxActor.Orientation = Rotation; // offset our second NxActor to the (relative) Right of our first MVector SecondLocation = Location + Rotation.GetRight()*2.0f; mySecondNxActor = MNxPhysics.CreateNxActor(ActorDesc, this); mySecondNxActor.Position = SecondLocation; mySecondNxActor.Orientation = Rotation; //Create a description for the new Cylindrical Joint MNxRevoluteJointDesc jointDesc = new MNxRevoluteJointDesc(); // bind it to both NxActors jointDesc.Actor1 = myNxActor; jointDesc.Actor2 = mySecondNxActor; // put it in the middle of the two NxActors jointDesc.GlobalAnchor = Location + Rotation.GetRight(); jointDesc.GlobalAxis = Rotation.GetRight(); //Create the Joint myNxJoint = MNxPhysics.CreateNxJoint(jointDesc); } // if this Actor isn't selected in Reality Builder, // then copy the NxActor's transformation into it if (!IsSelected) { Location = myNxActor.Position; Rotation = myNxActor.Orientation; } // otherwise copy its transformation into the NxActor, // since when we're dragging this Actor // around in Reality Builder, we want to have control over it else { myNxActor.Position = Location; myNxActor.Orientation = Rotation; } // pass Tick along to the base class base.Tick(); }
public override void OnRender(MCamera camera) { mySecondModel.SetTransform(mySecondNxActor.Orientation, mySecondNxActor.Position); mySecondModel.Draw(MyWorld,this); base.OnRender(camera); }
protected override void DisposeResources() { MNxPhysics.DestroyNxJoint(myNxJoint); myNxJoint = null; MNxPhysics.DestroyNxActor(myNxActor); myNxActor = null; base.DisposeResources(); }
... // create our NxActor myNxActor = MNxPhysics.CreateNxActor(ActorDesc, this); // set collision notification Event myNxActor.OnCollision += new CollisionNotify(myNxActor_OnCollision); ...
void myNxActor_OnCollision(int pairIndex, MNxActor other, MVector normalForce) { // optionally create a CollisionInfo struct // and pass the collision to the overridable // MActor.RigidBodyContact function MCollisionInfo info = new MCollisionInfo(); info.normal = normalForce; if(other != null) info.touched = other.Parent; RigidBodyContact(info); }
myNxActor.OnCollision += new CollisionNotify(myNxActor_OnCollision); ... ... public override void rigidBody_OnCollision(int pairIndex, MNxActor other, MVector normalForce) { // we want to start accessing the contact stream // this must be paired with a ContactStopIteration() some time later! MNxPhysics.ContactStartIteration(); //user could call getNumPairs() here while(MNxPhysics.ContactGoNextPair()) { //user could also call getNumPatches() here MNxShape s = MNxPhysics.ContactGetShape(pairIndex); while(MNxPhysics.ContactGoNextPatch()) { //user could also call getPatchNormal() and getNumPoints() here while(MNxPhysics.ContactGoNextPoint()) { //user could also getSeparation() here MVector contactPoint = MNxPhysics.ContactGetPoint(); //assuming front wheel drive we need to apply a force at the wheels. if (s.ShapeType == MNxShapeType.NX_SHAPE_CAPSULE) //assuming only the wheels of the car are capsules, otherwise we need more checks. //this branch can't be pulled out of loops because we have to do a full // iteration through the stream { CarWheelContact cwc; cwc.WheelIndex = s.UserData; cwc.ContactPoint = contactPoint; CarWheelContacts.Add(cwc); } } } } // we're done with the contactstream MNxPhysics.ContactStopIteration(); }
public class RigidBox : MActor { ... NxBox myRigidBody; ... public override void Tick() { // only create the rigid body if we haven't already created it if (myRigidBody == null) { myRigidBody = new NxBox(); MyModel.SetTransform(MHelpers.IdentityMatrix); myRigidBody.Create(this, MyModel.GetWorldBBoxMax(), mass, 0); } // if this Actor isn't selected in Reality Builder, // then copy the rigid body's transformation into it if (!IsSelected) { Location = myRigidBody.Location; Rotation = myRigidBody.Rotation; } // otherwise copy its transformation into the rigid body, // since when we're dragging this Actor // around in Reality Builder, we want to have control over it else { myRigidBody.Location = Location; myRigidBody.Rotation = Rotation; } // pass Tick along to the base class base.Tick(); } public override void RigidBodyContact(MCollisionInfo info) { MHelpers.Printf("RigidBox collision!"); } protected override void DisposeResources() { myRigidBody.Dispose(); myRigidBody = null; base.DisposeResources(); }