public TransformNode getPatientNameNode(Object markerID) { if (markerID.Equals("PatientMarkerConfig.txt")) { GeometryNode sphereNode = new GeometryNode("Sphere"); sphereNode.Model = new Sphere(3.5f, 20, 20); sphereNode.Model.CastShadows = true; sphereNode.Model.ReceiveShadows = true; Material sphereMat = new Material(); sphereMat.Diffuse = Color.Red.ToVector4(); sphereMat.Specular = Color.White.ToVector4(); sphereMat.SpecularPower = 20; sphereNode.Material = sphereMat; //sphereNode.Physics.Interactable = false; TransformNode sphereTrans = new TransformNode(); sphereTrans.Translation = new Vector3(0, 0, 5); sphereTrans.AddChild(sphereNode); return sphereTrans; } return null; }
private void CreateObjects() { // Create a model of box and sphere boxModel = new Box(Vector3.One); PrimitiveModel sphereModel = new Sphere(1f, 20, 20); // Create our ground plane GeometryNode groundNode = new GeometryNode("Ground"); groundNode.Model = boxModel; // Define the material name of this ground model groundNode.Physics.MaterialName = "Ground"; // Make this ground plane collidable, so other collidable objects can collide // with this ground groundNode.Physics.Collidable = true; groundNode.Physics.Shape = GoblinXNA.Physics.ShapeType.Box; groundNode.AddToPhysicsEngine = true; // Create a material for the ground Material groundMaterial = new Material(); groundMaterial.Diffuse = Color.LightGreen.ToVector4(); groundMaterial.Specular = Color.White.ToVector4(); groundMaterial.SpecularPower = 20; groundNode.Material = groundMaterial; // Create a parent transformation for both the ground and the sphere models TransformNode parentTransNode = new TransformNode(); parentTransNode.Translation = new Vector3(0, -10, -20); // Create a scale transformation for the ground to make it bigger TransformNode groundScaleNode = new TransformNode(); groundScaleNode.Scale = new Vector3(100, 1, 100); // Add this ground model to the scene scene.RootNode.AddChild(parentTransNode); parentTransNode.AddChild(groundScaleNode); groundScaleNode.AddChild(groundNode); // Create a material that will be applied to all of the sphere models Material sphereMaterial = new Material(); sphereMaterial.Diffuse = Color.Cyan.ToVector4(); sphereMaterial.Specular = Color.White.ToVector4(); sphereMaterial.SpecularPower = 10; Random rand = new Random(); // Create bunch of sphere models and pile them up for (int i = 0; i <= 0; i++) { for (int j = -1; j <= 1; j++) { TransformNode pileTrans = new TransformNode(); pileTrans.Translation = new Vector3(2 * j + (float)rand.NextDouble()/5, 2*i + 5f + (i + 1) * 0.05f, 0 + 0.01f * i + (float)rand.NextDouble()/5); GeometryNode gNode = new GeometryNode("Sphere" + (10 * i + j)); gNode.Model = sphereModel; gNode.Material = sphereMaterial; // Make the sphere models interactable, which means that they // participate in the physical simulation gNode.Physics.Interactable = true; gNode.Physics.Collidable = true; gNode.Physics.Shape = GoblinXNA.Physics.ShapeType.Sphere; gNode.Physics.Mass = 30f; gNode.AddToPhysicsEngine = true; parentTransNode.AddChild(pileTrans); pileTrans.AddChild(gNode); } } // Create a material for shooting box models shooterMat = new Material(); shooterMat.Diffuse = Color.Pink.ToVector4(); shooterMat.Specular = Color.Yellow.ToVector4(); shooterMat.SpecularPower = 10; }
public List<TransformNode> getListTransformNodes(Object PtID, Object EqID) { //create Box GeometryNode boxNode = new GeometryNode("Box"); boxNode.Model = new Box(6); boxNode.Model.CastShadows = true; boxNode.Model.ReceiveShadows = true; Material boxMat = new Material(); boxMat.Diffuse = Color.Blue.ToVector4(); boxMat.Specular = Color.White.ToVector4(); boxMat.SpecularPower = 20; boxNode.Material = boxMat; TransformNode boxTrans = new TransformNode(); boxTrans.Translation = new Vector3(-35, -18, 8); boxTrans.AddChild(boxNode); //Create cylinder GeometryNode cylinderNode = new GeometryNode("Cylinder"); cylinderNode.Model = new Cylinder(3.5f, 3.5f, 10, 20); cylinderNode.Model.CastShadows = true; cylinderNode.Model.ReceiveShadows = true; Material cylinderMat = new Material(); cylinderMat.Diffuse = Color.Green.ToVector4(); cylinderMat.Specular = Color.White.ToVector4(); cylinderMat.SpecularPower = 20; cylinderNode.Material = cylinderMat; TransformNode cylinderTrans = new TransformNode(); cylinderTrans.Translation = new Vector3(35, -18, 8); cylinderTrans.AddChild(cylinderNode); List<TransformNode> nodes = new List<TransformNode>(); nodes.Add(boxTrans); nodes.Add(cylinderTrans); return nodes; }
public void CreateSceneObj(string Folder, string ObjName, Vector3 ObjPos, Vector3 ObjScale) { GeometryNode ObjNode; ObjNode = LoadModel("Models/" + Folder, ObjName, true); //Setup Physics Aspecte ObjNode.Physics.Collidable = false; ObjNode.AddToPhysicsEngine = false; TransformNode parentTransNode = new TransformNode(ObjName); parentTransNode.Scale = ObjScale; parentTransNode.Rotation = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), MathHelper.ToRadians(90)); parentTransNode.Translation = ObjPos; parentTransNode.AddChild(ObjNode); //Add Model in the Scene parentTNodeGrd.AddChild(parentTransNode); }
public void AddLapida(Vector3 PosCar, int LevelHeight, float Scala) { GeometryNode LapidaNode = LoadModel("Models", "lapida", true); LapidaNode.Physics.Collidable = true; TransformNode parentTransNode = new TransformNode("Lapida"); parentTransNode.Scale = new Vector3(Scala, Scala + 2, Scala); parentTransNode.Rotation = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), MathHelper.ToRadians(90)); parentTransNode.Translation = new Vector3(PosCar.X, PosCar.Y, LevelHeight + 10); //Add Rotation because Blender Axis parentTransNode.AddChild(LapidaNode); //Add Model in the Scene groundMarkerNode.AddChild(parentTransNode); }
public static GeometryNode AddModel(MarkerNode Marker, String Folder, String Model, bool IntMaterial, float Scala) { GeometryNode ObstNode; if (Model == "null") return ObstNode = new GeometryNode(); ObstNode = Graphics3D.LoadModel("Models/" + Folder, Model, IntMaterial); //define the Physic Material name ObstNode.Physics.MaterialName = "Obstacle"; TransformNode parentTransNode = new TransformNode(); parentTransNode.Name = Model; parentTransNode.Scale = new Vector3(Scala, Scala + 2, Scala); parentTransNode.Rotation = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), MathHelper.ToRadians(90)); //Add Rotation because Blender Axis parentTransNode.AddChild(ObstNode); // Add this box model node to the ground marker node if(Marker != null) Marker.AddChild(parentTransNode); return ObstNode; }
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); }
public static void AddRollingBeats(Scene scene, TransformNode parentTrans) { Vector3 size = new Vector3(10.0f, 0.25f, 0.25f); Vector3 location = new Vector3(0, 3, 3); GeometryNode bar; // ////////////////////////////////////////////////////////////////// // // Create a bar and attach it to the world with a hinge with limits // // ////////////////////////////////////////////////////////////////// { TransformNode pileTrans = new TransformNode(); pileTrans.Translation = location; pileTrans.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitZ, MathHelper.PiOver2); Material barMat = new Material(); barMat.Diffuse = Color.Purple.ToVector4(); barMat.Specular = Color.White.ToVector4(); barMat.SpecularPower = 10; bar = new GeometryNode("Bar"); bar.Model = new Cylinder(size.Y, size.Y, size.X, 20); bar.Material = barMat; bar.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; bar.AddToPhysicsEngine = true; bar.Physics.Interactable = true; bar.Physics.Collidable = true; bar.Physics.Shape = ShapeType.Cylinder; bar.Physics.Mass = 2.0f; parentTrans.AddChild(pileTrans); pileTrans.AddChild(bar); Vector3 pivot = location + parentTrans.Translation; Vector3 pin = Vector3.UnitY; pivot.X -= size.X * 0.5f; HingeJoint joint = new HingeJoint(pivot, pin); joint.NewtonHingeCallback = doubleDoor; ((NewtonPhysics)scene.PhysicsEngine).CreateJoint(bar.Physics, null, joint); } // ///////////////////////////////////////////////////////////////// // // Add a sliding visualObject with limits // //////////////////////////////////////////////////////////////////// { Vector3 beatLocation = location; Vector3 beatSize = new Vector3(0.5f, 2.0f, 2.0f); beatLocation.X += size.X * 0.25f; TransformNode pileTrans = new TransformNode(); pileTrans.Translation = beatLocation; Material beatMat = new Material(); beatMat.Diffuse = Color.Red.ToVector4(); beatMat.Specular = Color.White.ToVector4(); beatMat.SpecularPower = 10; GeometryNode beat = new GeometryNode("Beat Slider"); beat.Model = new Box(beatSize); beat.Material = beatMat; beat.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; beat.AddToPhysicsEngine = true; beat.Physics.Interactable = true; beat.Physics.Collidable = true; beat.Physics.Shape = ShapeType.Box; beat.Physics.Mass = 2.0f; parentTrans.AddChild(pileTrans); pileTrans.AddChild(beat); Vector3 pivot = beatLocation + parentTrans.Translation; Vector3 pin = Vector3.UnitX; sliderLimit.X = ((location.X - beatLocation.X) - size.X * 0.5f); sliderLimit.Y = ((location.X - beatLocation.X) + size.X * 0.5f); SliderJoint joint = new SliderJoint(pivot, pin); sliderUpdate = delegate(IntPtr slider, ref Newton.NewtonHingeSliderUpdateDesc desc) { float distance = Newton.NewtonSliderGetJointPosit(slider); if (distance < sliderLimit.X) { // if the distance is smaller than the predefine interval, stop the slider desc.m_Accel = Newton.NewtonSliderCalculateStopAccel(slider, ref desc, sliderLimit.X); return 1; } else if (distance > sliderLimit.Y) { // if the distance is larger than the predefine interval, stop the slider desc.m_Accel = Newton.NewtonSliderCalculateStopAccel(slider, ref desc, sliderLimit.Y); return 1; } // no action need it if the joint angle is with the limits return 0; }; joint.NewtonSliderCallback = sliderUpdate; ((NewtonPhysics)scene.PhysicsEngine).CreateJoint(beat.Physics, bar.Physics, joint); } // ///////////////////////////////////////////////////////////////// // // Add a corkscrew visualObject with limits // // ///////////////////////////////////////////////////////////////// { Vector3 beatLocation = location; Vector3 beatSize = new Vector3(0.5f, 2.0f, 2.0f); beatLocation.X -= size.X * 0.25f; TransformNode pileTrans = new TransformNode(); pileTrans.Translation = beatLocation; Material beatMat = new Material(); beatMat.Diffuse = Color.YellowGreen.ToVector4(); beatMat.Specular = Color.White.ToVector4(); beatMat.SpecularPower = 10; GeometryNode beat = new GeometryNode("Beat Corkscrew"); beat.Model = new Box(beatSize); beat.Material = beatMat; beat.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; beat.AddToPhysicsEngine = true; beat.Physics.Interactable = true; beat.Physics.Collidable = true; beat.Physics.Shape = ShapeType.Box; beat.Physics.Mass = 2.0f; parentTrans.AddChild(pileTrans); pileTrans.AddChild(beat); Vector3 pivot = beatLocation + parentTrans.Translation; Vector3 pin = Vector3.UnitX; corkscrewLimit.X = ((location.X - beatLocation.X) - size.X * 0.5f); corkscrewLimit.Y = ((location.X - beatLocation.X) + size.X * 0.5f); CorkscrewJoint joint = new CorkscrewJoint(pivot, pin); corkscrewUpdate = delegate(IntPtr corkscrew, Newton.NewtonHingeSliderUpdateDesc[] desc) { // no action need it if the joint angle is with the limits uint retCode = 0; float distance = Newton.NewtonCorkscrewGetJointPosit(corkscrew); // The first entry in NewtonHingeSliderUpdateDesc control the screw linear acceleration if (distance < corkscrewLimit.X) { // if the distance is smaller than the predefine interval, stop the slider desc[0].m_Accel = Newton.NewtonCorkscrewCalculateStopAccel(corkscrew, ref desc[0], corkscrewLimit.X); retCode |= 1; } else if (distance > corkscrewLimit.Y) { // if the distance is larger than the predefine interval, stop the slider desc[0].m_Accel = Newton.NewtonCorkscrewCalculateStopAccel(corkscrew, ref desc[0], corkscrewLimit.Y); retCode |= 1; } // The second entry in NewtonHingeSliderUpdateDesc control the screw angular acceleration. // Make s small screw motor by setting the angular acceleration of the screw axis // We are not going to limit the angular rotation of the screw, but is we did we should // or return code with 2 float omega = Newton.NewtonCorkscrewGetJointOmega(corkscrew); desc[1].m_Accel = 1.5f - 0.2f * omega; // or with 0x10 to tell newton this axis is active retCode |= 2; // return the code return retCode; }; joint.NewtonCorkscrewCallback = corkscrewUpdate; ((NewtonPhysics)scene.PhysicsEngine).CreateJoint(beat.Physics, bar.Physics, joint); } // ///////////////////////////////////////////////////////////////// // // Add a universal joint visualObject with limits // // ///////////////////////////////////////////////////////////////// { Vector3 beatLocation = location; Vector3 beatSize = new Vector3(0.5f, 2.0f, 2.0f); beatLocation.X -= size.X * 0.45f; TransformNode pileTrans = new TransformNode(); pileTrans.Translation = beatLocation; Material beatMat = new Material(); beatMat.Diffuse = Color.YellowGreen.ToVector4(); beatMat.Specular = Color.White.ToVector4(); beatMat.SpecularPower = 10; GeometryNode beat = new GeometryNode("Beat Universal"); beat.Model = new Box(beatSize); beat.Material = beatMat; beat.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; beat.AddToPhysicsEngine = true; beat.Physics.Interactable = true; beat.Physics.Collidable = true; beat.Physics.Shape = ShapeType.Box; beat.Physics.Mass = 2.0f; parentTrans.AddChild(pileTrans); pileTrans.AddChild(beat); Vector3 pivot = beatLocation + parentTrans.Translation; Vector3 pin0 = Vector3.UnitX; Vector3 pin1 = Vector3.UnitY; universalLimit.X = -30.0f * MathHelper.Pi / 180.0f; universalLimit.Y = 30.0f * MathHelper.Pi / 180.0f; UniversalJoint joint = new UniversalJoint(pivot, pin0, pin1); universalUpdate = delegate(IntPtr universal, Newton.NewtonHingeSliderUpdateDesc[] desc) { // no action need it if the joint angle is with the limits uint retCode = 0; float omega = Newton.NewtonUniversalGetJointOmega0(universal); desc[0].m_Accel = -1.5f - 0.2f * omega; retCode |= 1; float angle = Newton.NewtonUniversalGetJointAngle1(universal); if (angle < universalLimit.X) { desc[1].m_Accel = Newton.NewtonUniversalCalculateStopAlpha1(universal, ref desc[1], universalLimit.X); retCode |= 2; } else if (angle > universalLimit.Y) { desc[1].m_Accel = Newton.NewtonUniversalCalculateStopAlpha1(universal, ref desc[1], universalLimit.Y); retCode |= 2; } // return the code return retCode; }; joint.NewtonUniversalCallback = universalUpdate; ((NewtonPhysics)scene.PhysicsEngine).CreateJoint(beat.Physics, bar.Physics, joint); } }
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; // This ship model has material definitions in the model file, so instead // of creating a material node for this ship model, we simply use its internal materials shipNode.Model.UseInternalMaterials = true; // Create a transform node to define the transformation for the ship shipTransNode = new TransformNode(); shipTransNode.Translation = new Vector3(0, 0, 0); shipTransNode.Scale = new Vector3(0.002f, 0.002f, 0.002f); // It's huge! shipTransNode.Rotation = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), MathHelper.ToRadians(0)); scene.RootNode.AddChild(shipTransNode); shipTransNode.AddChild(shipNode); }
private void CreateObject() { GeometryNode sphereNode = new GeometryNode("Sphere"); sphereNode.Model = new Box(60); sphereTransNode = new TransformNode(); sphereTransNode.Translation = new Vector3(0, 0, 0); Material sphereMaterial = new Material(); sphereMaterial.Diffuse = new Vector4(0.5f, 0, 0, 1); sphereMaterial.Specular = Color.White.ToVector4(); sphereMaterial.SpecularPower = 10; // Apply this material to the sphere model sphereNode.Material = sphereMaterial; sphereTransNode.AddChild(sphereNode); scene.RootNode.AddChild(sphereTransNode); }
private void MousePressHandler(int button, Point mouseLocation) { // If the GUI is shown, and the mouse is within the bound of the GUI, then // don't perform the mouse press event for the scene if(!(gameState.CurrentGameMode == GameState.GameMode.Edit && gameState.CurrentEditMode == GameState.EditMode.Multiple) && uiManager.Frame.Visible && UI2DHelper.IsWithin(mouseLocation, uiManager.Frame.Bounds)) return; if (gameState.CurrentGameMode == GameState.GameMode.Add) { if (gameState.CurrentAdditionMode == GameState.AdditionMode.Single) { // Add a domino on the ground if ((button == MouseInput.LeftButton) && (dominos.Count <= DOMINO_LIMIT)) { foreach (GeometryNode domino in selectedDominos) { Vector4 orig = domino.Material.Diffuse; domino.Material.Diffuse = new Vector4(orig.X, orig.Y, orig.Z, 1.0f); } selectedDominos.Clear(); GeometryNode dominoNode = new GeometryNode("Domino " + dominos.Count); dominoNode.Model = dominoModel; dominoNode.Physics.Mass = 20; dominoNode.Physics.Shape = ShapeType.Box; dominoNode.Physics.MaterialName = "Domino"; dominoNode.Physics.Pickable = true; dominoNode.AddToPhysicsEngine = true; Material dominoMaterial = new Material(); dominoMaterial.Diffuse = new Vector4(1, 1, 1, 0.5f); dominoMaterial.Specular = Color.White.ToVector4(); dominoMaterial.SpecularPower = 10; dominoMaterial.Texture = cguiLogoTexture; dominoNode.Material = dominoMaterial; TransformNode dominoTransNode = new TransformNode(); Vector3 nearSource = new Vector3(mouseLocation.X, mouseLocation.Y, 0); Vector3 farSource = new Vector3(mouseLocation.X, mouseLocation.Y, 1); Matrix viewMatrix = markerNode.WorldTransformation * State.ViewMatrix; Vector3 nearPoint = graphics.GraphicsDevice.Viewport.Unproject(nearSource, State.ProjectionMatrix, viewMatrix, Matrix.Identity); Vector3 farPoint = graphics.GraphicsDevice.Viewport.Unproject(farSource, State.ProjectionMatrix, viewMatrix, Matrix.Identity); // Cast a ray to the scene to pick a point on the ground where the domino // will be added List<PickedObject> pickedObjects = ((NewtonPhysics)scene.PhysicsEngine).PickRayCast(nearPoint, farPoint); for (int i = 0; i < pickedObjects.Count; i++) { if (((GeometryNode)pickedObjects[i].PickedPhysicsObject.Container). Name.Equals("Ground")) { Vector3 intersectPoint = nearPoint * (1 - pickedObjects[i].IntersectParam) + pickedObjects[i].IntersectParam * farPoint; dominoTransNode.Translation = intersectPoint + Vector3.UnitZ * dominoSize.Y / 2; break; } } markerNode.AddChild(dominoTransNode); dominoTransNode.AddChild(dominoNode); dominos.Add(dominoNode); selectedDominos.Add(dominoNode); } } else { if (button == MouseInput.LeftButton) { foreach (GeometryNode domino in selectedDominos) { Vector4 orig = domino.Material.Diffuse; domino.Material.Diffuse = new Vector4(orig.X, orig.Y, orig.Z, 1.0f); } selectedDominos.Clear(); AddPoint(mouseLocation); } } } else if (gameState.CurrentGameMode == GameState.GameMode.Edit) { if (button == MouseInput.LeftButton) { if (gameState.CurrentEditMode == GameState.EditMode.Single) { foreach (GeometryNode domino in selectedDominos) { Vector4 orig = domino.Material.Diffuse; domino.Material.Diffuse = new Vector4(orig.X, orig.Y, orig.Z, 1.0f); } selectedDominos.Clear(); Vector3 nearSource = new Vector3(mouseLocation.X, mouseLocation.Y, 0); Vector3 farSource = new Vector3(mouseLocation.X, mouseLocation.Y, 1); Matrix viewMatrix = markerNode.WorldTransformation * State.ViewMatrix; Vector3 nearPoint = graphics.GraphicsDevice.Viewport.Unproject(nearSource, State.ProjectionMatrix, viewMatrix, Matrix.Identity); Vector3 farPoint = graphics.GraphicsDevice.Viewport.Unproject(farSource, State.ProjectionMatrix, viewMatrix, Matrix.Identity); // Cast a ray to the scene to select the closest object (to the eye location) that hits the ray List<PickedObject> pickedObjects = ((NewtonPhysics)scene.PhysicsEngine).PickRayCast(nearPoint, farPoint); // If one or more objects intersect with our ray vector if (pickedObjects.Count > 0) { // Sort the picked objects in ascending order of intersection parameter // (the closest to the eye is the first one in the sorted list) pickedObjects.Sort(); // If the closest object is the ground, then don't do anything if (((GeometryNode)pickedObjects[0].PickedPhysicsObject.Container). Name.Equals("Ground")) return; selectedDominos.Add((GeometryNode)pickedObjects[0].PickedPhysicsObject.Container); GeometryNode domino = selectedDominos[0]; Vector4 mod = domino.Material.Diffuse; domino.Material.Diffuse = new Vector4(mod.X, mod.Y, mod.Z, 0.5f); foreach (PickedObject obj in pickedObjects) { if (((GeometryNode)obj.PickedPhysicsObject.Container).Name. Equals("Ground")) { Vector3 intersectPoint = nearPoint * (1 - obj.IntersectParam) + obj.IntersectParam * farPoint; break; } } } } else { if (multiSelection) { anchorPoint = mouseLocation; dragPoint = mouseLocation; } else { } } } } else { ballPressDistance = markerNode.WorldTransformation.Translation.Length(); } }
private void MouseReleaseHandler(int button, Point mouseLocation) { // If the GUI is shown, and the mouse is within the bound of the GUI, then // don't perform the mouse press event for the scene if (uiManager.Frame.Visible && UI2DHelper.IsWithin(mouseLocation, uiManager.Frame.Bounds)) return; if (gameState.CurrentGameMode == GameState.GameMode.Add) { if (gameState.CurrentAdditionMode == GameState.AdditionMode.Single) { // Nothing to do for single mode } else { if (button == MouseInput.LeftButton) { // Places the dominos on the drawn line with certain fixed gaps as long as // it doesn't go beyond the domino count limit for (int i = 0; i < pointList.Count && (dominos.Count <= DOMINO_LIMIT); i++) { GeometryNode dominoNode = new GeometryNode("Domino " + dominos.Count); dominoNode.Model = dominoModel; dominoNode.Physics.Mass = 20; dominoNode.Physics.Shape = ShapeType.Box; dominoNode.Physics.Pickable = true; dominoNode.Physics.MaterialName = "Domino"; dominoNode.Physics.Pickable = true; dominoNode.AddToPhysicsEngine = true; Material dominoMaterial = new Material(); dominoMaterial.Diffuse = new Vector4(1, 1, 1, 0.5f); dominoMaterial.Specular = Color.White.ToVector4(); dominoMaterial.SpecularPower = 10; dominoMaterial.Texture = cguiLogoTexture; dominoNode.Material = dominoMaterial; TransformNode dominoTransNode = new TransformNode(); dominoTransNode.Translation = pointList[i].Position + (Vector3.UnitZ * dominoSize.Y / 2); int start = 0, end = 0; // Get the point that is 2 points behind the current point (if any) if ((i - 2) <= 0) start = 0; else start = i - 2; // Get the point that is 2 points ahead of the current point (if any) if ((i + 2) >= pointList.Count) end = pointList.Count - 1; else end = i + 2; // Calculate the orientation of the domino based on the tangent line of // the current point (which is based on end pos - start pos) Vector3 u = -Vector3.UnitY; Vector3 v = pointList[end].Position - pointList[start].Position; u.Normalize(); v.Normalize(); float angle = (float)Math.Acos(Vector3.Dot(u, v)); if (v.X < 0) angle = -angle; dominoTransNode.Rotation = Quaternion.CreateFromAxisAngle( Vector3.UnitZ, angle - MathHelper.PiOver2); markerNode.AddChild(dominoTransNode); dominoTransNode.AddChild(dominoNode); dominos.Add(dominoNode); selectedDominos.Add(dominoNode); // Moves the point position to the next one that has at least the gap // of 'dominoGap' from the current position Vector3 curPoint = pointList[i].Position; while (i < pointList.Count) { if (Vector3.Distance(curPoint, pointList[i++].Position) >= dominoGap) break; } } // Clears the line points pointList.Clear(); lineListIndices.Clear(); } } } else if (gameState.CurrentGameMode == GameState.GameMode.Edit) { if (gameState.CurrentEditMode == GameState.EditMode.Single) { // Nothing to do for single mode } else { /*if (multiSelection) { Point upLeft = new Point(Math.Min(anchorPoint.X, dragPoint.X), Math.Min(anchorPoint.Y, dragPoint.Y)); Point downRight = new Point(Math.Max(anchorPoint.X, dragPoint.X), Math.Max(anchorPoint.Y, dragPoint.Y)); float dist = scene.CameraNode.Camera.ZNearPlane; float height = 2 * (dist / (float)Math.Tan(scene.CameraNode.Camera.FieldOfViewY / 2)); float width = height * scene.CameraNode.Camera.AspectRatio; float left = upLeft.X / (float)State.Width * width - width / 2; float right = downRight.X / (float)State.Width * width - width / 2; float bottom = upLeft.Y / (float)State.Height * height - height / 2; float top = downRight.Y / (float)State.Height * height - height / 2; Matrix proj = Matrix.CreatePerspectiveOffCenter(left, right, bottom, top, scene.CameraNode.Camera.ZNearPlane, scene.CameraNode.Camera.ZFarPlane); BoundingFrustum frustum = new BoundingFrustum(State.ViewMatrix * proj); foreach (GeometryNode domino in dominos) { if (frustum.Intersects(domino.Model.MinimumBoundingBox)) { selectedDominos.Add(domino); Vector4 mod = ((MaterialNode)domino.Parent).Material.Diffuse; ((MaterialNode)domino.Parent).Material.Diffuse = new Vector4(mod.X, mod.Y, mod.Z, 0.5f); scene.PhysicsEngine.RemovePhysicsObject(domino.Physics); } } anchorPoint = Point.Zero; dragPoint = Point.Zero; multiSelection = false; } else { selectedDominos.Clear(); multiSelection = true; }*/ } } else { // If game is not over, and the released button is either left button (for normal balls) // or right button (for heavier balls) if (button != MouseInput.MiddleButton && !gameState.GameOver) { bool heavy = (button == MouseInput.RightButton); float additionalSpeed = 0; // Add extra speed to the ball being shot if the camera point is moved from // far point to closer point (to the ground) float ballReleaseDistance = markerNode.WorldTransformation.Translation.Length(); if (ballReleaseDistance < ballPressDistance) additionalSpeed = ballPressDistance - ballReleaseDistance; Vector3 nearSource = new Vector3(uiManager.CrossHairPoint.X, uiManager.CrossHairPoint.Y, 0); Vector3 farSource = new Vector3(uiManager.CrossHairPoint.X, uiManager.CrossHairPoint.Y, 1); Matrix viewMatrix = markerNode.WorldTransformation * State.ViewMatrix; Vector3 nearPoint = graphics.GraphicsDevice.Viewport.Unproject(nearSource, State.ProjectionMatrix, viewMatrix, Matrix.Identity); Vector3 farPoint = graphics.GraphicsDevice.Viewport.Unproject(farSource, State.ProjectionMatrix, viewMatrix, Matrix.Identity); ballCount++; uiManager.BallCount++; // Create a ball and shoot it CreateBallCharactor(nearPoint, farPoint, additionalSpeed, heavy); } } }
/// <summary> /// Create both normal and heavy balls for shooting /// </summary> private void CreateBalls() { PrimitiveModel smallSphere = new TexturedSphere(3.5f, 20, 20); PrimitiveModel bigSphere = new TexturedSphere(6.5f, 20, 20); smallSphere.Shader = new SimpleShadowShader(scene.ShadowMap); bigSphere.Shader = new SimpleShadowShader(scene.ShadowMap); for (int i = 0; i < BALL_NUM; i++) { if (i == BALL_NUM / 2) { ballColor = new Vector4(1, 0, 0, 1); rState = 0; gState = 1; bState = 0; } GeometryNode ballNode = new GeometryNode(); ballNode.Name = "Ball " + ballNode.ID; if (i < BALL_NUM / 2) ballNode.Model = smallSphere; else ballNode.Model = bigSphere; ballNode.Model.ShadowAttribute = ShadowAttribute.None; ballNode.Physics.Collidable = true; ballNode.Physics.Interactable = true; ballNode.AddToPhysicsEngine = true; Material ballMaterial = new Material(); ballMaterial.Diffuse = GetNextColor(); ballMaterial.Specular = Color.White.ToVector4(); ballMaterial.SpecularPower = 30; ballNode.Material = ballMaterial; if (i < BALL_NUM / 2) ballNode.Physics.Mass = 150f; else ballNode.Physics.Mass = 250f; ballNode.Physics.Shape = ShapeType.Sphere; ballNode.Physics.MaterialName = "Ball"; TransformNode ballTransNode = new TransformNode(); ballTransNode.Translation = new Vector3(1000, 1000, 1000); markerNode.AddChild(ballTransNode); ballTransNode.AddChild(ballNode); if (i < BALL_NUM / 2) balls.Add(ballNode); else heavyBalls.Add(ballNode); } }
private void CreateDominos() { dominoModel = new DominoBox(new Vector3(dominoSize.X, dominoSize.Z, dominoSize.Y), new Vector2(0.663f, 0.707f)); dominoModel.ShadowAttribute = ShadowAttribute.ReceiveCast; dominoModel.Shader = new SimpleShadowShader(scene.ShadowMap); float radius = 18; for (int x = 0; x < 360; x += 30) { GeometryNode dominoNode = new GeometryNode("Domino " + dominos.Count); dominoNode.Model = dominoModel; dominoNode.Physics.Mass = 20; dominoNode.Physics.Shape = ShapeType.Box; dominoNode.Physics.MaterialName = "Domino"; dominoNode.Physics.Pickable = true; dominoNode.AddToPhysicsEngine = true; Material dominoMaterial = new Material(); dominoMaterial.Diffuse = Color.White.ToVector4(); dominoMaterial.Specular = Color.White.ToVector4(); dominoMaterial.SpecularPower = 10; dominoMaterial.Texture = cguiLogoTexture; dominoNode.Material = dominoMaterial; TransformNode dominoTransNode = new TransformNode(); dominoTransNode.Translation = new Vector3( (float)(radius * Math.Cos(MathHelper.ToRadians(x))), radius * (float)(Math.Sin(MathHelper.ToRadians(x))), dominoSize.Y / 2); dominoTransNode.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitZ, MathHelper.ToRadians(x + 90)); markerNode.AddChild(dominoTransNode); dominoTransNode.AddChild(dominoNode); dominos.Add(dominoNode); } }
/// <summary> /// Shoot a box from the clicked mouse location /// </summary> /// <param name="near"></param> /// <param name="far"></param> private void ShootBox(Vector3 near, Vector3 far) { GeometryNode shootBox = new GeometryNode("ShooterBox" + shooterID++); shootBox.Model = boxModel; shootBox.Material = shooterMat; #if !WINDOWS shootBox.Physics = new MataliObject(shootBox); ((MataliObject)shootBox.Physics).CollisionStartCallback = BoxCollideWithGround; #endif // Define the material name of this shooting box model shootBox.Physics.MaterialName = "ShootingBox"; shootBox.Physics.Interactable = true; shootBox.Physics.Collidable = true; shootBox.Physics.Shape = GoblinXNA.Physics.ShapeType.Box; shootBox.Physics.Mass = 60f; shootBox.AddToPhysicsEngine = true; // Calculate the direction to shoot the box based on the near and far point Vector3 linVel = far - near; linVel.Normalize(); // Multiply the direction with the velocity of 20 linVel *= 20f; // Assign the initial velocity to this shooting box shootBox.Physics.InitialLinearVelocity = linVel; TransformNode shooterTrans = new TransformNode(); shooterTrans.Translation = near; scene.RootNode.AddChild(shooterTrans); shooterTrans.AddChild(shootBox); }
/// <summary> /// Creates a rope-like object by connecting several ball and socket joints. /// </summary> /// <param name="scene"></param> /// <param name="parentTrans"></param> public static void AddRope(Scene scene, TransformNode parentTrans) { Vector3 size = new Vector3(1.5f, 0.25f, 0.25f); Vector3 location = new Vector3(0, 9, 0); IPhysicsObject link0 = null; Color[] colors = {Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Indigo, Color.Violet}; PrimitiveModel capsule = new Capsule(size.Y, size.X, 12); for (int i = 0; i < 7; i++) { TransformNode pileTrans = new TransformNode(); pileTrans.Translation = location; Material linkMat = new Material(); linkMat.Diffuse = colors[i].ToVector4(); linkMat.Specular = Color.White.ToVector4(); linkMat.SpecularPower = 10; GeometryNode link1 = new GeometryNode("Link " + i); link1.Model = capsule; link1.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; link1.Material = linkMat; link1.AddToPhysicsEngine = true; link1.Physics.Interactable = true; link1.Physics.Collidable = true; link1.Physics.Shape = ShapeType.Capsule; link1.Physics.Mass = 2.0f; link1.Physics.LinearDamping = 1f; link1.Physics.AngularDamping = new Vector3(1f, 1f, 1f); parentTrans.AddChild(pileTrans); pileTrans.AddChild(link1); Vector3 pivot = location + parentTrans.Translation; pivot.Y += (size.X - size.Y) * 0.5f; BallAndSocketJoint joint = new BallAndSocketJoint(pivot); joint.Pin = -Vector3.UnitY; joint.MaxConeAngle = 0.0f; joint.MaxTwistAngle = 10.0f * MathHelper.Pi / 180; ((NewtonPhysics)scene.PhysicsEngine).CreateJoint(link1.Physics, link0, joint); link0 = link1.Physics; location = new Vector3(location.X, location.Y - (size.X - size.Y), location.Z); } }
public static void AddDoubleSwingDoors(Scene scene, TransformNode parentTrans) { Vector3 size = new Vector3(2.0f, 5.0f, 0.5f); Vector3 location = new Vector3(0, 3.5f, -3); Material linkMat = new Material(); linkMat.Diffuse = Color.Cyan.ToVector4(); linkMat.Specular = Color.White.ToVector4(); linkMat.SpecularPower = 10; GeometryNode link1 = null; // Make first wing { TransformNode pileTrans = new TransformNode(); pileTrans.Translation = location; link1 = new GeometryNode("Door 1"); link1.Model = new Box(size.X, size.Y, size.Z); link1.Material = linkMat; link1.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; link1.AddToPhysicsEngine = true; link1.Physics.Interactable = true; link1.Physics.Collidable = true; link1.Physics.Shape = ShapeType.Box; link1.Physics.Mass = 2.0f; parentTrans.AddChild(pileTrans); pileTrans.AddChild(link1); Vector3 pivot = location + parentTrans.Translation; Vector3 pin = Vector3.UnitY; pivot.X += size.X * 0.5f; pivot.Y -= size.Y * 0.5f; HingeJoint joint = new HingeJoint(pivot, pin); joint.NewtonHingeCallback = doubleDoor; ((NewtonPhysics)scene.PhysicsEngine).CreateJoint(link1.Physics, null, joint); } // Make second wing { location.X -= size.X; TransformNode pileTrans = new TransformNode(); pileTrans.Translation = location; GeometryNode link2 = new GeometryNode("Door 2"); link2.Model = new Box(size.X, size.Y, size.Z); link2.Material = linkMat; link2.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; link2.AddToPhysicsEngine = true; link2.Physics.Interactable = true; link2.Physics.Collidable = true; link2.Physics.Shape = ShapeType.Box; link2.Physics.Mass = 2.0f; parentTrans.AddChild(pileTrans); pileTrans.AddChild(link2); Vector3 pivot = location + parentTrans.Translation; Vector3 pin = Vector3.UnitY; pivot.X += size.X * 0.5f; pivot.Y -= size.Y * 0.5f; HingeJoint joint = new HingeJoint(pivot, pin); joint.NewtonHingeCallback = doubleDoor; ((NewtonPhysics)scene.PhysicsEngine).CreateJoint(link2.Physics, link1.Physics, joint); } }
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 void CreateObjects() { // Create a sphere geometry { GeometryNode sphereNode = new GeometryNode("Sphere"); sphereNode.Model = new Sphere(3.5f, 20, 20); sphereNode.Model.CastShadows = true; sphereNode.Model.ReceiveShadows = true; Material sphereMat = new Material(); sphereMat.Diffuse = Color.Red.ToVector4(); sphereMat.Specular = Color.White.ToVector4(); sphereMat.SpecularPower = 20; sphereNode.Material = sphereMat; TransformNode sphereTrans = new TransformNode(); sphereTrans.Translation = new Vector3(0, 0, 5); groundMarkerNode.AddChild(sphereTrans); sphereTrans.AddChild(sphereNode); } // Create a box geometry { GeometryNode boxNode = new GeometryNode("Box"); boxNode.Model = new Box(6); boxNode.Model.CastShadows = true; boxNode.Model.ReceiveShadows = true; Material boxMat = new Material(); boxMat.Diffuse = Color.Blue.ToVector4(); boxMat.Specular = Color.White.ToVector4(); boxMat.SpecularPower = 20; boxNode.Material = boxMat; TransformNode boxTrans = new TransformNode(); boxTrans.Translation = new Vector3(-35, -18, 8); groundMarkerNode.AddChild(boxTrans); boxTrans.AddChild(boxNode); } // Create a cylinder geometry { GeometryNode cylinderNode = new GeometryNode("Cylinder"); cylinderNode.Model = new Cylinder(3.5f, 3.5f, 10, 20); cylinderNode.Model.CastShadows = true; cylinderNode.Model.ReceiveShadows = true; Material cylinderMat = new Material(); cylinderMat.Diffuse = Color.Green.ToVector4(); cylinderMat.Specular = Color.White.ToVector4(); cylinderMat.SpecularPower = 20; cylinderNode.Material = cylinderMat; TransformNode cylinderTrans = new TransformNode(); cylinderTrans.Translation = new Vector3(35, -18, 8); groundMarkerNode.AddChild(cylinderTrans); cylinderTrans.AddChild(cylinderNode); } // Create a torus geometry { GeometryNode torusNode = new GeometryNode("Torus"); torusNode.Model = new Torus(2.5f, 6.0f, 20, 20); torusNode.Model.CastShadows = true; torusNode.Model.ReceiveShadows = true; Material torusMat = new Material(); torusMat.Diffuse = Color.Yellow.ToVector4(); torusMat.Specular = Color.White.ToVector4(); torusMat.SpecularPower = 20; torusNode.Material = torusMat; TransformNode torusTrans = new TransformNode(); torusTrans.Translation = new Vector3(-35, 18, 8); groundMarkerNode.AddChild(torusTrans); torusTrans.AddChild(torusNode); } // Create a capsule geometry { GeometryNode capsuleNode = new GeometryNode("Capsule"); capsuleNode.Model = new Capsule(3, 12, 20); capsuleNode.Model.CastShadows = true; capsuleNode.Model.ReceiveShadows = true; Material capsuleMat = new Material(); capsuleMat.Diffuse = Color.Cyan.ToVector4(); capsuleMat.Specular = Color.White.ToVector4(); capsuleMat.SpecularPower = 20; capsuleNode.Material = capsuleMat; TransformNode capsuleTrans = new TransformNode(); capsuleTrans.Translation = new Vector3(35, 18, 8); groundMarkerNode.AddChild(capsuleTrans); capsuleTrans.AddChild(capsuleNode); } }
public static RaceCar AddRaceCar(Scene scene, TransformNode parentTrans) { TransformNode transNode = new TransformNode(); transNode.Translation = new Vector3(0, 10, 10); Material carMat = new Material(); carMat.Diffuse = Color.Pink.ToVector4(); carMat.Specular = Color.White.ToVector4(); carMat.SpecularPower = 10; GeometryNode carNode = new GeometryNode("Race Car"); carNode.Model = new Box(3, 1.0f, 2); carNode.Material = carMat; carNode.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; NewtonPhysics physicsEngine = (NewtonPhysics)scene.PhysicsEngine; RaceCar car = new RaceCar(carNode, physicsEngine); for (int i = 0; i < 4; i++) car.Tires[i] = CreateTire((TireID)Enum.ToObject(typeof(TireID), i), car.TireTransformNode[i], carNode, scene.PhysicsEngine.Gravity); car.Collidable = true; car.Interactable = true; carNode.Physics = car; carNode.Physics.NeverDeactivate = true; carNode.AddToPhysicsEngine = true; parentTrans.AddChild(transNode); transNode.AddChild(carNode); Newton.NewtonSetBodyLeaveWorldEvent(physicsEngine.NewtonWorld, car.LeaveWorldCallback); return car; }
private void build(IModel model, string name) { this.name = name; this.instanceNumber = instance; restrictedDimension = new Vector3(1,1,0); geo = new GeometryNode(this.Label); trans = new TransformNode(this.Label + "_Trans"); instance++; trans.AddChild(geo); geo.Model = model; geo.Physics.Shape = GoblinXNA.Physics.ShapeType.Box; geo.Physics.Pickable = true; geo.AddToPhysicsEngine = true; trans.Rotation = Quaternion.CreateFromYawPitchRoll((float)Math.PI / 2, 0, (float)Math.PI / 2); }
private void createCorderBackground() { cornerPanelTransformNode = new TransformNode("Corner Panel Trans"); scene.RootNode.AddChild(cornerPanelTransformNode); cornerPanelNode = new GeometryNode("Corner Panel"); cornerPanelNode.Model = new TexturedLayer(new Vector2(250, 250));//new Box(300, 250, 1); cornerPanelTransformNode.AddChild(cornerPanelNode); cornerPanelTransformNode.Translation = new Vector3(2.3f, -1.58f, -5); cornerPanelTransformNode.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitX, MathHelper.ToRadians(90)); //cornerPanelTransformNode.Translation = new Vector3(0, .06f, -1); cornerPanelTransformNode.Scale = new Vector3(0.005f, 0.005f, 0.005f); Material pointerLabelMaterial = new Material(); pointerLabelMaterial.Diffuse = Color.White.ToVector4(); ;// new Vector4(0, 0.5f, 0, 1); // pointerLabelMaterial.Specular = Color.White.ToVector4(); // pointerLabelMaterial.SpecularPower = 50; pointerLabelMaterial.Texture = Content.Load<Texture2D>("hud/cornerPanel"); cornerPanelNode.Material = pointerLabelMaterial; //cornerPanelTransformNode.SetAlpha(0.55f); Vector4 tempColor = cornerPanelNode.Material.Diffuse; Vector3 tempVec = new Vector3(tempColor.X, tempColor.Y, tempColor.Z); cornerPanelNode.Material.Diffuse = new Vector4(tempVec, 0.7f); }
public static void CreateBall(MarkerNode Marker) { // Create a geometry node for Sphere PrimitiveModel PsphereModel = new Sphere(15f, 20, 20); // Create a material to apply to the sphere model Material sphereMaterial = new Material(); sphereMaterial.Diffuse = new Vector4(0, 0.5f, 0, 1); sphereMaterial.Specular = Color.White.ToVector4(); sphereMaterial.SpecularPower = 10; GeometryNode sphereNode = new GeometryNode("Sphere"); //sphereNode.Model = new TexturedSphere(16, 20, 20); sphereNode.Model = PsphereModel; sphereNode.Material = sphereMaterial; // Add this sphere model to the physics engine for collision detection sphereNode.Physics.Interactable = true; sphereNode.Physics.Collidable = true; sphereNode.Physics.Shape = ShapeType.Sphere; sphereNode.Physics.Mass = 30; //sphereNode.Physics.ApplyGravity = false; sphereNode.Physics.InitialLinearVelocity = new Vector3(30, 0, 0); //sphereNode.Physics.MaterialName = "Sphere"; //sphereNode.Physics.ApplyGravity = true; sphereNode.AddToPhysicsEngine = true; // Make this sphere model cast and receive shadows //sphereNode.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; // Assign a shadow shader for this model that uses the IShadowMap we assigned to the scene //sphereNode.Model.Shader = new SimpleShadowShader(scene.ShadowMap); TransformNode sphereTransNode = new TransformNode(); sphereTransNode.Translation = new Vector3(50, 0, 50); // Now add the above nodes to the scene graph in the appropriate order. // Note that only the nodes added below the marker node are affected by // the marker transformation. Marker.AddChild(sphereTransNode); sphereTransNode.AddChild(sphereNode); }
private GeometryNode ShootBullet; //Geometry for Bullet #endregion Fields #region Constructors public Bullet(Vector3 InitPos, PrimitiveModel BulletModel, Material Material, Vector3 DirBullet, MarkerNode grdMarkerNode) { //Create Bullet ShootBullet = new GeometryNode(); ShootBullet.Name = "ShootBullet" + ShootBullet.ID; ShootBullet.Model = BulletModel; ShootBullet.Material = Material; ShootBullet.Physics.Interactable = true; ShootBullet.Physics.Collidable = true; ShootBullet.Physics.Shape = GoblinXNA.Physics.ShapeType.Box; ShootBullet.Physics.Mass = 60f; ShootBullet.Physics.MaterialName = "Bullet"; ShootBullet.AddToPhysicsEngine = true; // Assign the initial velocity to this shooting box ShootBullet.Physics.InitialLinearVelocity = new Vector3(DirBullet.X * 80, DirBullet.Y * 80, DirBullet.Z * 50); BulletTrans = new TransformNode(); BulletTrans.Translation = InitPos; grdMarkerNode.AddChild(BulletTrans); BulletTrans.AddChild(ShootBullet); //Normal asignament isvisible = true; //Agrego Segundo desde que se creo livetime = Convert.ToInt32(DateTime.Now.TimeOfDay.TotalSeconds) + BULLET_TIMELIVE; }
public void CreateGroundLevel(string Folder, String Model, Vector3 ObjPos, Vector3 Scala) { GeometryNode groundNode; groundNode = LoadModel("Models/" + Folder, Model, true); //Setup Physics Aspecte groundNode.Physics.Collidable = true; // Make the ground model to receive shadow casted by other objects //groundNode.Model.ShadowAttribute = ShadowAttribute.ReceiveOnly; // Assign a shadow shader //groundNode.Model.Shader = new SimpleShadowShader(scene.ShadowMap); TransformNode parentTransNode = new TransformNode("ground"); parentTransNode.Scale = Scala; parentTransNode.Rotation = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), MathHelper.ToRadians(90)); //Add Rotation because Blender Axis parentTransNode.Translation = ObjPos; parentTransNode.AddChild(groundNode); //Add Model in the Scene parentTNodeGrd.AddChild(parentTransNode); }
private void CreateCamera() { // Set up the camera of the scene graph Camera camera = new Camera(); // Put the camera at (0, 3, 7) camera.Translation = new Vector3(0, 3, 7); // Rotate the camera -15 degrees along the X axis camera.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitX, MathHelper.ToRadians(-15)); // Set the vertical field of view to be 45 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; // Set the initial translation and rotation of the generic input which controls // the camera transform in this application GenericInput.Instance.InitialTranslation = camera.Translation; GenericInput.Instance.InitialRotation = camera.Rotation; // Adjust the speed of the camera control GenericInput.Instance.PanSpeed = 0.05f; GenericInput.Instance.ZoomSpeed = 0.5f; GenericInput.Instance.RotateSpeed = 0.02f; // Now assign this camera to a camera node CameraNode cameraNode = new CameraNode(camera); // Add a transform node which will be updated based on GenericInput class // which implements simple camera rotation and translation based on mouse // drag and key presses. // To rotate the camera, hold right mouse button and drag around genericInputNode = new TransformNode(); scene.RootNode.AddChild(genericInputNode); genericInputNode.AddChild(cameraNode); // Assign the camera node to be our scene graph's current camera node scene.CameraNode = cameraNode; }
public void CreateShootBullet(Vector3 InitPos, Vector3 DirBullet, Color BulletColor) { if (BulletID == 20) return; GeometryNode ShootBullet = new GeometryNode("ShootBullet" + BulletID++); ShootBullet.Model = BulletModel; BulletrMat.Diffuse = BulletColor.ToVector4(); ShootBullet.Material = BulletrMat; ShootBullet.Physics.Interactable = true; ShootBullet.Physics.Collidable = true; ShootBullet.Physics.Shape = GoblinXNA.Physics.ShapeType.Box; ShootBullet.Physics.Mass = 60f; ShootBullet.Physics.MaterialName = "Bullet"; ShootBullet.AddToPhysicsEngine = true; // Assign the initial velocity to this shooting box ShootBullet.Physics.InitialLinearVelocity = new Vector3(DirBullet.X * 80, DirBullet.Y * 80, DirBullet.Z * 50); TransformNode BulletTrans = new TransformNode(); BulletTrans.Translation = InitPos; groundMarkerNode.AddChild(BulletTrans); BulletTrans.AddChild(ShootBullet); }
private void CreateObjects() { // Create a sphere geometry { GeometryNode sphereNode = new GeometryNode("Sphere"); sphereNode.Model = new TexturedSphere(14, 20, 20); sphereNode.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; sphereNode.Model.Shader = new SimpleShadowShader(scene.ShadowMap); Material sphereMat = new Material(); sphereMat.Diffuse = Color.Red.ToVector4(); sphereMat.Specular = Color.White.ToVector4(); sphereMat.SpecularPower = 20; sphereNode.Material = sphereMat; TransformNode sphereTrans = new TransformNode(); sphereTrans.Translation = new Vector3(0, 0, 20); groundMarkerNode.AddChild(sphereTrans); sphereTrans.AddChild(sphereNode); } // Create a box geometry { GeometryNode boxNode = new GeometryNode("Box"); boxNode.Model = new TexturedBox(24); boxNode.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; boxNode.Model.Shader = new SimpleShadowShader(scene.ShadowMap); Material boxMat = new Material(); boxMat.Diffuse = Color.Blue.ToVector4(); boxMat.Specular = Color.White.ToVector4(); boxMat.SpecularPower = 20; boxNode.Material = boxMat; TransformNode boxTrans = new TransformNode(); boxTrans.Translation = new Vector3(-140, -72, 32); groundMarkerNode.AddChild(boxTrans); boxTrans.AddChild(boxNode); } // Create a cylinder geometry { GeometryNode cylinderNode = new GeometryNode("Cylinder"); cylinderNode.Model = new Cylinder(14, 14, 10, 20); cylinderNode.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; Material cylinderMat = new Material(); cylinderMat.Diffuse = Color.Green.ToVector4(); cylinderMat.Specular = Color.White.ToVector4(); cylinderMat.SpecularPower = 20; cylinderNode.Material = cylinderMat; TransformNode cylinderTrans = new TransformNode(); cylinderTrans.Translation = new Vector3(140, -72, 32); groundMarkerNode.AddChild(cylinderTrans); cylinderTrans.AddChild(cylinderNode); } // Create a torus geometry { GeometryNode torusNode = new GeometryNode("Torus"); torusNode.Model = new Torus(10, 24, 20, 20); torusNode.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; Material torusMat = new Material(); torusMat.Diffuse = Color.Yellow.ToVector4(); torusMat.Specular = Color.White.ToVector4(); torusMat.SpecularPower = 20; torusNode.Material = torusMat; TransformNode torusTrans = new TransformNode(); torusTrans.Translation = new Vector3(-140, 72, 32); groundMarkerNode.AddChild(torusTrans); torusTrans.AddChild(torusNode); } // Create a capsule geometry { GeometryNode capsuleNode = new GeometryNode("Capsule"); capsuleNode.Model = new Capsule(12, 48, 20); capsuleNode.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; Material capsuleMat = new Material(); capsuleMat.Diffuse = Color.Cyan.ToVector4(); capsuleMat.Specular = Color.White.ToVector4(); capsuleMat.SpecularPower = 20; capsuleNode.Material = capsuleMat; TransformNode capsuleTrans = new TransformNode(); capsuleTrans.Translation = new Vector3(140, 72, 32); groundMarkerNode.AddChild(capsuleTrans); capsuleTrans.AddChild(capsuleNode); } }
private void CreateObjects() { // Create a geometry node with a model of a sphere that will be overlaid on // top of the ground marker array GeometryNode sphereNode = new GeometryNode("Sphere"); // We will use TexturedSphere instead of regular Box class since we will need the // texture coordinates elements for passing the vertices to the SimpleShadowShader // we will be using sphereNode.Model = new TexturedSphere(16, 20, 20); // Add this sphere model to the physics engine for collision detection sphereNode.AddToPhysicsEngine = true; sphereNode.Physics.Shape = ShapeType.Sphere; // Make this sphere model cast and receive shadows sphereNode.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; // Assign a shadow shader for this model that uses the IShadowMap we assigned to the scene sphereNode.Model.Shader = new SimpleShadowShader(scene.ShadowMap); // Create a marker node to track a ground marker array. #if USE_NYARTOOLKIT groundMarkerNode = new MarkerNode(scene.MarkerTracker, "NyARToolkitGroundArray.xml", NyARToolkitTracker.ComputationMethod.Average); #else groundMarkerNode = new MarkerNode(scene.MarkerTracker, "ALVARGroundArray.xml"); #endif TransformNode sphereTransNode = new TransformNode(); sphereTransNode.Translation = new Vector3(0, 0, 50); // Create a material to apply to the sphere model Material sphereMaterial = new Material(); sphereMaterial.Diffuse = new Vector4(0, 0.5f, 0, 1); sphereMaterial.Specular = Color.White.ToVector4(); sphereMaterial.SpecularPower = 10; sphereNode.Material = sphereMaterial; // Now add the above nodes to the scene graph in the appropriate order. // Note that only the nodes added below the marker node are affected by // the marker transformation. scene.RootNode.AddChild(groundMarkerNode); groundMarkerNode.AddChild(sphereTransNode); sphereTransNode.AddChild(sphereNode); // Create a geometry node with a model of a box that will be overlaid on // top of the ground marker array initially. (When the toolbar marker array is // detected, it will be overlaid on top of the toolbar marker array.) boxNode = new GeometryNode("Box"); // We will use TexturedBox instead of regular Box class since we will need the // texture coordinates elements for passing the vertices to the SimpleShadowShader // we will be using boxNode.Model = new TexturedBox(32.4f); // Add this box model to the physics engine for collision detection boxNode.AddToPhysicsEngine = true; boxNode.Physics.Shape = ShapeType.Box; // Make this box model cast and receive shadows boxNode.Model.ShadowAttribute = ShadowAttribute.ReceiveCast; // Assign a shadow shader for this model that uses the IShadowMap we assigned to the scene boxNode.Model.Shader = new SimpleShadowShader(scene.ShadowMap); // Create a marker node to track a toolbar marker array. #if USE_NYARTOOLKIT toolbarMarkerNode = new MarkerNode(scene.MarkerTracker, "ToolbarNyARToolkit.xml", NyARToolkitTracker.ComputationMethod.Average); #else toolbarMarkerNode = new MarkerNode(scene.MarkerTracker, "ALVARToolbar.xml"); #endif scene.RootNode.AddChild(toolbarMarkerNode); // Create a material to apply to the box model Material boxMaterial = new Material(); boxMaterial.Diffuse = new Vector4(0.5f, 0, 0, 1); boxMaterial.Specular = Color.White.ToVector4(); boxMaterial.SpecularPower = 10; boxNode.Material = boxMaterial; // Add this box model node to the ground marker node groundMarkerNode.AddChild(boxNode); // Create a collision pair and add a collision callback function that will be // called when the pair collides NewtonPhysics.CollisionPair pair = new NewtonPhysics.CollisionPair(boxNode.Physics, sphereNode.Physics); ((NewtonPhysics)scene.PhysicsEngine).AddCollisionCallback(pair, BoxSphereCollision); }
private void CreateObjects() { // 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; // This ship model has material definitions in the model file, so instead // of creating a material node for this ship model, we simply use its internal materials ((Model)shipNode.Model).UseInternalMaterials = true; ((Model)shipNode.Model).ContainsTransparency = true; // Create a transform node to define the transformation for the ship TransformNode shipTransNode = new TransformNode(); // shipTransNode.Translation = new Vector3(0, 5, -12); shipTransNode.Scale = new Vector3(0.002f, 0.002f, 0.002f); // It's huge! shipTransNode.Rotation = Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), MathHelper.ToRadians(-90)); shipTransParentNode = new TransformNode(); //Set our rotation animation to ease in initially animationRotation = new AnimationHelper(Easing.EaseIn); startPosition = new Vector3(-10, 0, -10); endPosition = new Vector3(5, 2, -5); startRotationVector = new Vector3(0, 0, 0); endRotationVector = new Vector3(MathHelper.ToRadians(0), MathHelper.ToRadians(180), MathHelper.ToRadians(180)); //Set our translation animation to ease in initially animationTranslation = new AnimationHelper(Easing.EaseIn); //Define what kind of interpolation to use. animationTranslation.Animate(arrayOfTransitions[currentInterpolation], startPosition, endPosition, 2); // animationTranslation.SetLooping(true, 5); // Use if you want to loop through an animation. // Set an action to take place once the animation concludes. animationTranslation.SetEndAction(delegate() { animationRotation.Animate(arrayOfTransitions[currentInterpolation], startRotationVector, endRotationVector, 2.0); }); // Note: if you loop and set this animation, it'll occur after the end of the first loop, not after the end of all loops! textToPrint = "Linear interpolation"; // Now add the above nodes to the scene graph in appropriate order scene.RootNode.AddChild(shipTransParentNode); shipTransParentNode.AddChild(shipTransNode); shipTransNode.AddChild(shipNode); }