/// <summary> /// Creates a camera that chases a given object. /// </summary> /// <param name="chasedObject"></param> private void CreateChasingCamera(GeometryNode chasedObject) { // Set up the camera of the scene graph Camera camera = new Camera(); camera.Translation = new Vector3(5, 3, 0); // Rotate the camera -20 degrees along the X axis camera.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitY, MathHelper.ToRadians(90)) * Quaternion.CreateFromAxisAngle(Vector3.UnitX, MathHelper.ToRadians(-20)); // Set the vertical field of view to be 60 degrees camera.FieldOfViewY = MathHelper.ToRadians(45); // Set the near clipping plane to be 0.1f unit away from the camera camera.ZNearPlane = 0.1f; // Set the far clipping plane to be 1000 units away from the camera camera.ZFarPlane = 1000; // Add this camera node to a geometry node we want to chase for CameraNode cameraNode = new CameraNode("ChasingCamera", camera); chasedObject.AddChild(cameraNode); // Initially assign the chasing camera to be our scene graph's active camera node scene.CameraNode = cameraNode; }
private void CreateObject() { // Loads a textured model of a ship ModelLoader loader = new ModelLoader(); Model shipModel = (Model)loader.Load("", "p1_wedge"); // Create a geometry node of a loaded ship model GeometryNode shipNode = new GeometryNode("Ship"); shipNode.Model = shipModel; ((Model)shipNode.Model).UseInternalMaterials = true; // Create a transform node to define the transformation for the ship TransformNode shipTransNode = new TransformNode(); shipTransNode.Translation = new Vector3(0, 0, -50); shipTransNode.Scale = new Vector3(0.01f, 0.01f, 0.01f); // It's huge! shipTransNode.Rotation = Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), MathHelper.ToRadians(90)); shipTransParentNode = new TransformNode(); shipTransParentNode.Translation = Vector3.Zero; // Create a geometry node with model of a torus GeometryNode torusNode = new GeometryNode("Torus"); torusNode.Model = new Torus(12f, 15.5f, 20, 20); TransformNode torusTransNode = new TransformNode(); torusTransNode.Translation = new Vector3(-50, 0, 0); torusTransNode.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitX, MathHelper.ToRadians(90)); // Create a material node for this torus model Material torusMaterial = new Material(); torusMaterial.Diffuse = Color.DarkGoldenrod.ToVector4(); torusMaterial.Specular = Color.White.ToVector4(); torusMaterial.SpecularPower = 5; torusNode.Material = torusMaterial; // Now add the above nodes to the scene graph in appropriate order scene.RootNode.AddChild(shipTransParentNode); shipTransParentNode.AddChild(shipTransNode); shipTransNode.AddChild(shipNode); scene.RootNode.AddChild(torusTransNode); torusTransNode.AddChild(torusNode); // Now create couple of particle effects to attach to the models // Create a smoke particle effect and fire particle effect to simulate a // ring of fire around the torus model #if WINDOWS_PHONE SmokePlumeParticleEffect smokeParticles = new SmokePlumeParticleEffect(20, spriteBatch); FireParticleEffect fireParticles = new FireParticleEffect(40, spriteBatch); #else SmokePlumeParticleEffect smokeParticles = new SmokePlumeParticleEffect(); FireParticleEffect fireParticles = new FireParticleEffect(); // The order defines which particle effect to render first. Since we want // to show the fire particles in front of the smoke particles, we make // the smoke particles to be rendered first, and then fire particles smokeParticles.DrawOrder = 200; fireParticles.DrawOrder = 300; #endif // Create a particle node to hold these two particle effects ParticleNode fireRingEffectNode = new ParticleNode(); fireRingEffectNode.ParticleEffects.Add(smokeParticles); fireRingEffectNode.ParticleEffects.Add(fireParticles); // Implement an update handler for each of the particle effects which will be called // every "Update" call fireRingEffectNode.UpdateHandler += new ParticleUpdateHandler(UpdateRingOfFire); torusNode.AddChild(fireRingEffectNode); // Create another set of fire and smoke particle effects to simulate the fire // the ship catches when the ship passes the ring of fire #if WINDOWS_PHONE FireParticleEffect shipFireEffect = new FireParticleEffect(150, spriteBatch); SmokePlumeParticleEffect shipSmokeEffect = new SmokePlumeParticleEffect(80, spriteBatch); shipFireEffect.MinScale *= 1.5f; shipFireEffect.MaxScale *= 1.5f; #else FireParticleEffect shipFireEffect = new FireParticleEffect(); SmokePlumeParticleEffect shipSmokeEffect = new SmokePlumeParticleEffect(); shipSmokeEffect.DrawOrder = 400; shipFireEffect.DrawOrder = 500; #endif ParticleNode shipFireNode = new ParticleNode(); shipFireNode.ParticleEffects.Add(shipFireEffect); shipFireNode.ParticleEffects.Add(shipSmokeEffect); shipFireNode.UpdateHandler += new ParticleUpdateHandler(UpdateShipFire); shipNode.AddChild(shipFireNode); }
private static NewtonTire CreateTire(TireID tireID, TransformNode tireTrans, GeometryNode carNode, float gravity) { NewtonTire tire = new NewtonTire(); Material tireMat = new Material(); tireMat.Diffuse = Color.Orange.ToVector4(); tireMat.Specular = Color.White.ToVector4(); tireMat.SpecularPower = 10; float tireRadius = 0.7f; GeometryNode tireNode = new GeometryNode("Race Car " + tireID + " Tire"); tireNode.Model = new Cylinder(tireRadius, tireRadius, 0.3f, 20); tireNode.Material = tireMat; carNode.AddChild(tireTrans); tireTrans.AddChild(tireNode); tire.Mass = 5.0f; tire.Width = 0.3f * 1.25f; tire.Radius = tireRadius; switch (tireID) { case TireID.FrontLeft: tire.TireOffsetMatrix = Matrix.CreateTranslation(new Vector3(-1.5f, 0, -1f)); break; case TireID.FrontRight: tire.TireOffsetMatrix = Matrix.CreateTranslation(new Vector3(-1.5f, 0, 1f)); break; case TireID.RearLeft: tire.TireOffsetMatrix = Matrix.CreateTranslation(new Vector3(1.5f, 0, -1f)); break; case TireID.RearRight: tire.TireOffsetMatrix = Matrix.CreateTranslation(new Vector3(1.5f, 0, 1f)); break; } // the tire will spin around the lateral axis of the same tire space tire.Pin = Vector3.UnitZ; tire.SuspensionLength = RaceCar.SUSPENSION_LENGTH; // calculate the spring and damper contact for the subquestion // // from the equation of a simple spring the force is given by // a = k * x // where k is a spring constant and x is the displacement and a is the spring acceleration. // we desire that a rest length the acceleration generated by the spring equal that of the gravity // several gravity forces // m * gravity = k * SUSPENSION_LENGTH * 0.5f, float x = RaceCar.SUSPENSION_LENGTH; tire.SuspensionSpring = (200.0f * (float)Math.Abs(gravity)) / x; //tireSuspesionSpring = (100.0f * dAbs (GRAVITY)) / x; // from the equation of a simple spring / damper system // the system resonate at a frequency // w^2 = ks // // and the damping attenuation is given by // d = ks / 2.0f // where: // if d = w = 2 * pi * f -> the system is critically damped // if d > w < 2 * pi * f -> the system is super critically damped // if d < w < 2 * pi * f -> the system is under damped damped // for a vehicle we usually want a suspension that is about 10% super critically damped float w = (float)Math.Sqrt(tire.SuspensionSpring); // a critically damped suspension act too jumpy for a race car tire.SuspensionShock = 1.0f * w; // make it a little super critical y damped //tireSuspesionShock = 2.0f * w; tire.CollisionID = 0x100; return tire; }
private static NewtonTire CreateTire(TireID tireID, TransformNode tireTrans, GeometryNode carNode, float gravity) { NewtonTire tire = new NewtonTire(); Material tireMat = new Material(); tireMat.Diffuse = Color.Orange.ToVector4(); tireMat.Specular = Color.White.ToVector4(); tireMat.SpecularPower = 10; float tireRadius = 1.4f; GeometryNode tireNode = new GeometryNode("Race Car " + tireID + " Tire"); tireNode.Model = new Cylinder(tireRadius, tireRadius, 0.9f, 20); tireNode.Material = tireMat; carNode.AddChild(tireTrans); tireTrans.AddChild(tireNode); tire.Mass = 5.0f; tire.Width = 0.3f * 1.25f; tire.Radius = tireRadius; switch (tireID) { case TireID.FrontLeft: tire.TireOffsetMatrix = Matrix.CreateTranslation(new Vector3(-5.9f, 0, -3.0f)); break; case TireID.FrontRight: tire.TireOffsetMatrix = Matrix.CreateTranslation(new Vector3(-5.9f, 0, 3.0f)); break; case TireID.RearLeft: tire.TireOffsetMatrix = Matrix.CreateTranslation(new Vector3(5.0f, 0, -3.0f)); break; case TireID.RearRight: tire.TireOffsetMatrix = Matrix.CreateTranslation(new Vector3(5.0f, 0, 3.0f)); break; } // the tire will spin around the lateral axis of the same tire space tire.Pin = Vector3.UnitZ; tire.SuspensionLength = RaceCar.SUSPENSION_LENGTH; float x = RaceCar.SUSPENSION_LENGTH; tire.SuspensionSpring = (200.0f * (float)Math.Abs(gravity)) / x; //tireSuspesionSpring = (100.0f * dAbs (GRAVITY)) / x; float w = (float)Math.Sqrt(tire.SuspensionSpring); // a critically damped suspension act too jumpy for a race car tire.SuspensionShock = 1.0f * w; // make it a little super critical y damped //tireSuspesionShock = 2.0f * w; tire.CollisionID = 0x100; return tire; }
private void CreateObject() { // Loads a textured model of a ship ModelLoader loader = new ModelLoader(); Model shipModel = (Model)loader.Load("", "p1_wedge"); // Create a geometry node of a loaded ship model GeometryNode shipNode = new GeometryNode("Ship"); shipNode.Model = shipModel; ((Model)shipNode.Model).UseInternalMaterials = true; // Create a transform node to define the transformation for the ship TransformNode shipTransNode = new TransformNode(); shipTransNode.Translation = new Vector3(0, 0, -50); shipTransNode.Scale = new Vector3(0.01f, 0.01f, 0.01f); // It's huge! shipTransNode.Rotation = Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), MathHelper.ToRadians(90)); shipTransParentNode = new TransformNode(); shipTransParentNode.Translation = Vector3.Zero; // Create a geometry node with model of a torus GeometryNode torusNode = new GeometryNode("Torus"); torusNode.Model = new Torus(12f, 15.5f, 20, 20); TransformNode torusTransNode = new TransformNode(); torusTransNode.Translation = new Vector3(-50, 0, 0); torusTransNode.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitX, MathHelper.ToRadians(90)); // Create a material node for this torus model Material torusMaterial = new Material(); torusMaterial.Diffuse = Color.DarkGoldenrod.ToVector4(); torusMaterial.Specular = Color.White.ToVector4(); torusMaterial.SpecularPower = 5; torusNode.Material = torusMaterial; // Now add the above nodes to the scene graph in appropriate order scene.RootNode.AddChild(shipTransParentNode); shipTransParentNode.AddChild(shipTransNode); shipTransNode.AddChild(shipNode); scene.RootNode.AddChild(torusTransNode); torusTransNode.AddChild(torusNode); // Now create couple of particle effects to attach to the models // Create a smoke particle effect and fire particle effect to simulate a // ring of fire around the torus model #if WINDOWS_PHONE SmokePlumeParticleEffect smokeParticles = new SmokePlumeParticleEffect(20); FireParticleEffect fireParticles = new FireParticleEffect(40); #else SmokePlumeParticleEffect smokeParticles = new SmokePlumeParticleEffect(); FireParticleEffect fireParticles = new FireParticleEffect(); // The order defines which particle effect to render first. Since we want // to show the fire particles in front of the smoke particles, we make // the smoke particles to be rendered first, and then fire particles smokeParticles.DrawOrder = 200; fireParticles.DrawOrder = 300; #endif // Create a particle node to hold these two particle effects ParticleNode fireRingEffectNode = new ParticleNode(); fireRingEffectNode.ParticleEffects.Add(smokeParticles); fireRingEffectNode.ParticleEffects.Add(fireParticles); // Implement an update handler for each of the particle effects which will be called // every "Update" call fireRingEffectNode.UpdateHandler += new ParticleUpdateHandler(UpdateRingOfFire); torusNode.AddChild(fireRingEffectNode); // Create another set of fire and smoke particle effects to simulate the fire // the ship catches when the ship passes the ring of fire #if WINDOWS_PHONE FireParticleEffect shipFireEffect = new FireParticleEffect(150); SmokePlumeParticleEffect shipSmokeEffect = new SmokePlumeParticleEffect(80); shipFireEffect.MinScale *= 1.5f; shipFireEffect.MaxScale *= 1.5f; #else FireParticleEffect shipFireEffect = new FireParticleEffect(); SmokePlumeParticleEffect shipSmokeEffect = new SmokePlumeParticleEffect(); shipSmokeEffect.DrawOrder = 400; shipFireEffect.DrawOrder = 500; #endif ParticleNode shipFireNode = new ParticleNode(); shipFireNode.ParticleEffects.Add(shipFireEffect); shipFireNode.ParticleEffects.Add(shipSmokeEffect); shipFireNode.UpdateHandler += new ParticleUpdateHandler(UpdateShipFire); shipNode.AddChild(shipFireNode); }
private static NewtonTire CreateTire(TireID tireID, TransformNode tireTrans, GeometryNode carNode, float gravity) { NewtonTire tire = new NewtonTire(); Material tireMat = new Material(); tireMat.Diffuse = Color.Orange.ToVector4(); tireMat.Specular = Color.White.ToVector4(); tireMat.SpecularPower = 10; float tireRadius = 1.4f; GeometryNode tireNode = new GeometryNode("Race Car " + tireID + " Tire"); tireNode.Model = new Cylinder(tireRadius, tireRadius, 0.9f, 20); tireNode.Material = tireMat; carNode.AddChild(tireTrans); tireTrans.AddChild(tireNode); tire.Mass = 5.0f; tire.Width = 0.3f * 1.25f; tire.Radius = tireRadius; switch (tireID) { case TireID.FrontLeft: tire.TireOffsetMatrix = Matrix.CreateTranslation(new Vector3(-5.9f, 0, -3.0f)); break; case TireID.FrontRight: tire.TireOffsetMatrix = Matrix.CreateTranslation(new Vector3(-5.9f, 0, 3.0f)); break; case TireID.RearLeft: tire.TireOffsetMatrix = Matrix.CreateTranslation(new Vector3(5.0f, 0, -3.0f)); break; case TireID.RearRight: tire.TireOffsetMatrix = Matrix.CreateTranslation(new Vector3(5.0f, 0, 3.0f)); break; } // the tire will spin around the lateral axis of the same tire space tire.Pin = Vector3.UnitZ; tire.SuspensionLength = RaceCar.SUSPENSION_LENGTH; float x = RaceCar.SUSPENSION_LENGTH; tire.SuspensionSpring = (200.0f * (float)Math.Abs(gravity)) / x; //tireSuspesionSpring = (100.0f * dAbs (GRAVITY)) / x; float w = (float)Math.Sqrt(tire.SuspensionSpring); // a critically damped suspension act too jumpy for a race car tire.SuspensionShock = 1.0f * w; // make it a little super critical y damped //tireSuspesionShock = 2.0f * w; tire.CollisionID = 0x100; return(tire); }
private static NewtonTire CreateTire(TireID tireID, TransformNode tireTrans, GeometryNode carNode, float gravity) { NewtonTire tire = new NewtonTire(); Material tireMat = new Material(); tireMat.Diffuse = Color.Orange.ToVector4(); tireMat.Specular = Color.White.ToVector4(); tireMat.SpecularPower = 10; float tireRadius = 0.7f; GeometryNode tireNode = new GeometryNode("Race Car " + tireID + " Tire"); tireNode.Model = new Cylinder(tireRadius, tireRadius, 0.3f, 20); tireNode.Material = tireMat; carNode.AddChild(tireTrans); tireTrans.AddChild(tireNode); tire.Mass = 5.0f; tire.Width = 0.3f * 1.25f; tire.Radius = tireRadius; switch (tireID) { case TireID.FrontLeft: tire.TireOffsetMatrix = Matrix.CreateTranslation(new Vector3(-1.5f, 0, -1f)); break; case TireID.FrontRight: tire.TireOffsetMatrix = Matrix.CreateTranslation(new Vector3(-1.5f, 0, 1f)); break; case TireID.RearLeft: tire.TireOffsetMatrix = Matrix.CreateTranslation(new Vector3(1.5f, 0, -1f)); break; case TireID.RearRight: tire.TireOffsetMatrix = Matrix.CreateTranslation(new Vector3(1.5f, 0, 1f)); break; } // the tire will spin around the lateral axis of the same tire space tire.Pin = Vector3.UnitZ; tire.SuspensionLength = RaceCar.SUSPENSION_LENGTH; // calculate the spring and damper contact for the subquestion // // from the equation of a simple spring the force is given by // a = k * x // where k is a spring constant and x is the displacement and a is the spring acceleration. // we desire that a rest length the acceleration generated by the spring equal that of the gravity // several gravity forces // m * gravity = k * SUSPENSION_LENGTH * 0.5f, float x = RaceCar.SUSPENSION_LENGTH; tire.SuspensionSpring = (200.0f * (float)Math.Abs(gravity)) / x; //tireSuspesionSpring = (100.0f * dAbs (GRAVITY)) / x; // from the equation of a simple spring / damper system // the system resonate at a frequency // w^2 = ks // // and the damping attenuation is given by // d = ks / 2.0f // where: // if d = w = 2 * pi * f -> the system is critically damped // if d > w < 2 * pi * f -> the system is super critically damped // if d < w < 2 * pi * f -> the system is under damped damped // for a vehicle we usually want a suspension that is about 10% super critically damped float w = (float)Math.Sqrt(tire.SuspensionSpring); // a critically damped suspension act too jumpy for a race car tire.SuspensionShock = 1.0f * w; // make it a little super critical y damped //tireSuspesionShock = 2.0f * w; tire.CollisionID = 0x100; return(tire); }