/PhysicsEngines/Tests3D/Program.cs
C# | 1054 lines | 651 code | 149 blank | 254 comment | 44 complexity | 12feeb8eb07cb5074e25b2a0e16fae4d MD5 | raw file
Possible License(s): Apache-2.0
- using System;
- using System.Collections.Generic;
- using Delta.ContentSystem.Rendering;
- using Delta.ContentSystem.Rendering.Helpers;
- using Delta.Engine;
- using Delta.InputSystem;
- using Delta.PhysicsEngines.Advanced;
- using Delta.PhysicsEngines.Effects;
- using Delta.PhysicsEngines.VisualShapes;
- using Delta.Rendering;
- using Delta.Rendering.Basics.Drawing;
- using Delta.Rendering.Basics.Fonts;
- using Delta.Rendering.Cameras;
- using Delta.Rendering.Effects;
- using Delta.Rendering.Effects.Modifiers;
- using Delta.Rendering.Enums;
- using Delta.Rendering.Models;
- using Delta.Utilities;
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Datatypes.Advanced;
- using Delta.Utilities.Math;
- using NUnit.Framework;
-
- namespace Delta.PhysicsEngines.Tests3D
- {
- /// <summary>
- /// Physics tests
- /// </summary>
- internal class Program
- {
- #region Main
- /// <summary>
- /// Main entry point, will just call one of the tests, uncomment the rest
- /// </summary>
- private static void Main()
- {
- // Note: In the settings for this project Jitter is used. You can switch
- // to other physics engines here (required files are copied automatically,
- // but for your release you should set this to the content project).
- // Warning: Farseer has no 3D Physics support (just good for 2D)
- //Settings.Modules.PhysicsModule = "Bullet";
- //Settings.Modules.PhysicsModule = "JigLib";
- //Settings.Modules.PhysicsModule = "Jitter";
- //coming soon: Settings.Modules.PhysicsModule = "Havok";
- //coming soon: Settings.Modules.PhysicsModule = "PhysX";
-
- #region Tests
- //TestSimpleSimulation3D();
- //TestRayCast();
- //Test3DObjectPicking();
- //TestMultiple3DObjectPicking();
- //TestControlledSpherePlane();
- //does not work: TestBallMovement();
- //TestSimpleRagdoll();
- //TestRestitution();
- //TestRopeSimulation();
- //TestPrismaticJoint();
- //TestCollisionEvents();
- //FountainEffectPhysics3D();
- //FountainPhysicsEffectWithTornado3D();
- //TestJigLibXCreateScene1(9);
- //TestJigLibXCreateScene3();
- //Jitter.JitterPhysics.JitterPhysicsTests.TestCreate3DBodyWithShape();
- #endregion
-
- #region Tutorials
- //Tutorials.Simple3DSimulation();
- //Tutorials.RopeSimulation();
- //Tutorials.CollisionEvents();
- //Tutorials.ControlledSphere();
- //Tutorials.RayCasting3D();
- Tutorials.PyramidSimulation();
- //Tutorials.BodyRestitution();
- #endregion
- }
- #endregion
-
- #region TestSimpleSimulation3D (Static)
- /// <summary>
- /// Test a simple simulation with 3D physics
- /// </summary>
- [Test, Category("Visual")]
- public static void TestSimpleSimulation3D()
- {
- // create 4 vertices that will define the "ground" static plane
- const int planeSize = 200;
- const int height = -5;
-
- // Enable plane to physics world.
- Physics.Instance.SetGroundPlane(true, height);
-
- Physics.DebugEnabled = true;
-
- // set the params of the spheres
- float radius = 2.0f;
- Vector spherePosition1 = new Vector(2.0f, 0.0f, 5.0f);
- Vector spherePosition2 = new Vector(12.0f, 0.0f, 5.0f);
-
- // create two sphere and add them to the physics world
- VisualPhysicsSphere visualSphere1 = new VisualPhysicsSphere(
- spherePosition1, radius, Color.Red);
-
- VisualPhysicsSphere visualSphere2 = new VisualPhysicsSphere(
- spherePosition2, radius, Color.Green);
-
- // create two boxes
- VisualPhysicsBox visualBox1 = new VisualPhysicsBox(
- spherePosition1 + 15.0f * Vector.UnitZ, radius * 2);
-
- VisualPhysicsBox visualBox2 = new VisualPhysicsBox(
- visualBox1.Body.Position + new Vector(3.0f, 1.0f, 7.0f) +
- 5.0f * Vector.UnitZ + 1.0f * Vector.UnitX, radius * 2);
-
- // this line of code also sets the camera, i.e. ViewProjection matrix
- LookAtCamera camera = new LookAtCamera(new Vector(0, -10, 10));
-
- Font fpsFont = new Font(Font.Default,
- HorizontalAlignment.Left, VerticalAlignment.Top);
- Mesh plane = Mesh.CreatePlane("GroundPlane", planeSize, planeSize,
- new MaterialData());
-
- Application.Start(delegate
- {
- fpsFont.Draw("Fps: " + Time.Fps, ScreenSpace.DrawArea);
-
- // Draw the spheres
- visualSphere1.Draw();
- visualSphere2.Draw();
-
- // Draw the boxes
- visualBox1.Draw();
- visualBox2.Draw();
-
- // Draw the ground plane
- Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
- plane.Draw(ref planeHeight);
- });
- }
- #endregion
-
- #region TestSimpleRagdoll (Static)
- /// <summary>
- /// Test a simple simulation with ragdoll
- /// </summary>
- [Test, Category("Visual")]
- public static void TestSimpleRagdoll()
- {
- // create 4 vertices that will define the "ground" static plane
- const int size = 50;
- const int height = -5;
-
- // add a plane to the physics world
- Physics.Instance.SetGroundPlane(true, height);
-
- List<Ragdoll> ragdolls = new List<Ragdoll>();
-
- for (int i = 3; i < 8; i++)
- {
- for (int e = 3; e < 8; e++)
- {
- Vector position = new Vector(i * 6 - 25, e * 6 - 25, 5);
- ragdolls.Add(new Ragdoll(position));
- }
- }
-
- // this line of code also sets the camera, i.e. ViewProjection matrix
- LookAtCamera camera = new LookAtCamera(new Vector(0, -10, 10));
- Mesh plane = Mesh.CreatePlane("GroundPlane", size, size,
- new MaterialData());
-
- Application.Start(delegate
- {
- // Draw the ground plane
- Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
- plane.Draw(ref planeHeight);
-
- if (Input.Mouse.LeftButtonIsPressed)
- {
- Point mousePosition = Input.Mouse.Position;
- Ray ray = ScreenSpace.GetRayFromScreenPoint(mousePosition);
-
- PhysicsBody grabBody;
- Vector hitNormal;
- float fraction;
- bool result = Physics.FindRayCast(ray, false, out grabBody,
- out hitNormal, out fraction);
-
- if (result)
- {
- grabBody.IsActive = true;
- }
- }
- foreach (Ragdoll ragdoll in ragdolls)
- {
- ragdoll.Draw();
- }
- });
- }
- #endregion
-
- #region TestControlledSpherePlane (Static)
- /// <summary>
- /// Test a Sphere controlled by the user in a plane
- /// </summary>
- [Test, Category("Visual")]
- public static void TestControlledSpherePlane()
- {
- /*TODO
- // create 4 vertices that will define the "ground" static plane
- Vector v0 = new Vector(16, 16, -5);
- Vector v1 = new Vector(16, -16, -5);
- Vector v2 = new Vector(-16, -16, -5);
- Vector v3 = new Vector(-16, 16, -5);
-
- // add a plane to the physics world
- VisualPhysicsPlane groundPlane = new VisualPhysicsPlane(80, 80, new Vector(0, 0, -5), new MaterialData());//"NormalMap", "RocksHighDiffuse", "None", "None", "None", "None"));
-
- //Mesh sphereMesh = Mesh.CreateSphere("SomeName", 5, new MaterialData());
-
- // create the sphere and add it to the physics world
- VisualPhysicsSphere sphere = new VisualPhysicsSphere(new Vector(0, 0, 10), 3.0f, new MaterialData());//"NormalMap", "RocksHighDiffuse", "None", "None", "None", "None"));
-
- // set a controller for the sphere
- IController3D ctrler =
- PhysicsManager.CreateController(sphere.PhysicsSphere, 1.0f, 2.0f, 0.0f);
-
- // Manipulate the sphere with the help of the controller.
- Vector direction = new Vector(RandomHelper.RandomFloat(-1000, +1000),
- RandomHelper.RandomFloat(-1000, +1000),
- RandomHelper.RandomFloat(-1000, +1000));
- direction.Normalize();
- ctrler.MovingDirection = direction;
- ctrler.MoveSpeed = RandomHelper.RandomFloat(5, 30);
-
- // set the gravity
- PhysicsManager.Gravity = new Vector(0, 0, -9.8f);
-
- LookAtCamera camera = new LookAtCamera(new Vector(0, 25, 10));
-
- Application.Start(delegate
- {
- //Draw our sphere and groundPlane
- sphere.Draw();
- groundPlane.Draw();
-
- //Font.Default.WriteTopLeft(sphere.PhysicsSphere.Position.ToString());
- });*/
- }
- #endregion
-
- #region FountainEffectPhysics3D (Static)
- /// <summary>
- /// Tests that simulates usage of physics particles
- /// </summary>
- [Test, Category("Visual")]
- public static void FountainEffectPhysics3D()
- {
- // Set up camera
- LookAtCamera cam = new LookAtCamera(new Vector(1, -5, 3));
-
- float maxLifeTime = 30.0f;
-
- // Create a new template.
- EffectData data = EffectData.Get("<SmokeEffect3D>");
- // Add an emitter and modifiers.
- EmitterData emitter = new EmitterData();
- emitter.Modifiers.AddRange(
- new IEffectModifier[]
- {
- new AlwaysSpawnModifier(),
- new SpawnIntervalModifier
- {
- SpawnInterval = 0.4f,
- },
- new PhysicsModifier
- {
- DebugDisplay = true,
- PhysicsShapeType = ParticlePhysicsShape.Sphere,
- Mass = 10.0f,
- },
- new SizeModifier
- {
- WidthRange = 20f,
- HeightRange = 20f,
- },
- new ParticleLifetimeModifier
- {
- ParticleLifetime = maxLifeTime,
- },
- new RotationModifier
- {
- Rotation = new Range(0f, 360f),
- },
- new MaterialModifier
- {
- Material = new MaterialColored("SmokeSmallAdditive")
- {
- BlendColor = new Color(0.3f, 0.4f, 0.8f),
- },
- BillboardMode = BillboardMode.CameraFacingPrecise,
- },
- });
- data.Emitters.Add(emitter);
- int effectId = Effect.Spawn(data, new Vector(0.0f, 0.0f, 8.5f));
-
- // Setup Physics
- Physics.Gravity = new Vector(0f, 0f, -9.81f);
-
- // Create invisile Plane
- Physics.Instance.SetGroundPlane(true, 0.0f);
-
- // Start the application.
- Application.Start(delegate
- {
- // Draw a grid for orientation.
- Grid.Draw();
-
- if (Input.Keyboard.GetState(InputButton.M) == InputState.Released)
- {
- Physics.Multithreading = !Physics.Multithreading;
- }
-
- Font.DrawTopLeftInformation(
- "Fps: " + Time.Fps + "\n" +
- Effect.NumberOfActiveParticles(effectId) +
- " currently active particles\n" +
- (Physics.Multithreading
- ? "Multithreaded "
- : "Single Threaded"));
- });
- }
- #endregion
-
- #region FountainPhysicsEffectWithTornado3D (Static)
- /// <summary>
- /// Tests that simulates usage of physics particles with apply
- /// of tornado, press N to activated, U to remove it.
- /// </summary>
- [Test, Category("Visual")]
- public static void FountainPhysicsEffectWithTornado3D()
- {
- // Set up camera
- LookAtCamera cam = new LookAtCamera(new Vector(1, -5, 3));
-
- float maxLifeTime = 10.0f;
-
- // Create a new template.
- EffectData data = EffectData.Get("<SmokeEffect3D>");
- // Add an emitter and modifiers.
- EmitterData emitter = new EmitterData();
- emitter.Modifiers.AddRange(
- new IEffectModifier[]
- {
- new AlwaysSpawnModifier(),
- new SpawnIntervalModifier
- {
- SpawnInterval = 1.0f,
- },
- new PhysicsModifier
- {
- DebugDisplay = true,
- PhysicsShapeType = ParticlePhysicsShape.Sphere,
- Mass = 10.0f,
- },
- new SizeModifier
- {
- WidthRange = 20f,
- HeightRange = 20f,
- },
- new ParticleLifetimeModifier
- {
- ParticleLifetime = maxLifeTime,
- },
- new RotationModifier
- {
- Rotation = new Range(0f, 360f),
- },
- new MaterialModifier
- {
- Material = new MaterialColored("SmokeSmallAdditive")
- {
- BlendColor = new Color(0.3f, 0.4f, 0.8f),
- },
- BillboardMode = BillboardMode.CameraFacingPrecise,
- },
- });
- data.Emitters.Add(emitter);
- int effectId = Effect.Spawn(data, new Vector(0.0f, 0.0f, 8.5f));
-
- // Setup Physics
- Physics.Gravity = new Vector(0f, 0f, -9.81f);
-
- // Create invisile Plane
- Physics.Instance.SetGroundPlane(true, 0.0f);
-
- Tornado tornado = Physics.CreateTornado(Vector.Zero);
- tornado.IsEnabled = false;
-
- // Start the application.
- Application.Start(delegate
- {
- // Draw a grid for orientation.
- Grid.Draw();
-
- if (Input.Keyboard.GetState(InputButton.N) == InputState.Released)
- {
- tornado.IsEnabled = !tornado.IsEnabled;
- tornado.Position = new Vector(0, 0, -5.0f);
-
- if (tornado.IsEnabled &&
- tornado.IsRemoved)
- {
- // Trigger to add to simulation if removed with U button.
- tornado.AddToSimulation();
- }
- }
-
- if (Input.Keyboard.GetState(InputButton.U) == InputState.Released)
- {
- Physics.RemoveForceField(tornado);
- }
-
- if (Input.Keyboard.GetState(InputButton.M) == InputState.Released)
- {
- Physics.Multithreading = !Physics.Multithreading;
- }
-
- Font.DrawTopLeftInformation(
- "Fps: " + Time.Fps + "\n" +
- "Currently active particles: " +
- Effect.NumberOfActiveParticles(effectId) + "\n" +
- (Physics.Multithreading
- ? "Multithreaded "
- : "Single Threaded"));
- });
- }
- #endregion
-
- #region Methods (Private)
-
- #region TestRayCast
- /// <summary>
- /// Test ray cast
- /// </summary>
- private static void TestRayCast()
- {
- // create a plane so that every ray that doesn't hit an object will hit
- // the plane
- const int size = 50;
- const int height = -5;
-
- // add a plane to the physics world
- Physics.Instance.SetGroundPlane(true, height);
-
- // create a box and a sphere
- VisualPhysicsBox visualBox = new VisualPhysicsBox(new Vector(-2, 0, 0), 4);
- VisualPhysicsSphere visualSphere =
- new VisualPhysicsSphere(new Vector(5, 0, 0), 2);
-
- // this line of code also sets the camera, i.e. ViewProjection matrix
- LookAtCamera cam = new LookAtCamera(new Vector(0, 25, 10));
-
- // create a moving ray
- Vector rayStart = new Vector(0, 0, 10);
- Vector lookTarget = new Vector();
- Ray testRay = new Ray(rayStart, lookTarget - rayStart);
-
- Mesh plane = Mesh.CreatePlane("GroundPlane", size, size, new MaterialData());
-
- Application.Start(delegate
- {
- // Draw the ground plane
- Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
- plane.Draw(ref planeHeight);
-
- PhysicsBody catchedBody;
- Vector normal;
- float fraction;
-
- //Draw the sphere and box
- visualBox.Draw();
- visualSphere.Draw();
-
- // perform ray cast
- bool rayCasted = Physics.FindRayCast(testRay, false,
- out catchedBody, out normal, out fraction);
-
- // update the objects color according to the result of the ray cast
- if (catchedBody == visualBox.Body)
- {
- visualBox.DebugDrawColor = Color.Red;
- }
- else
- {
- visualBox.DebugDrawColor = Color.Green;
- }
-
- if (catchedBody == visualSphere.Body)
- {
- visualSphere.DebugDrawColor = Color.Red;
- }
- else
- {
- visualSphere.DebugDrawColor = Color.Green;
- }
-
- // render the line according to the deleted intersection
- if (rayCasted)
- {
- // render up to the intersection point
- //DrawManager.Line3D(testRay.Position, testRay.Direction, Color.Yellow);
-
- // render the surface normal at this point
- //DrawManager.Line3D(testRay.Position, testRay.Direction + normal, Color.White);
- }
- else
- {
- //DrawManager.Line3D(testRay.Position,
- // testRay.Position + 15.0f * testRay.Direction, Color.Yellow);
- }
-
- // update the direction of the ray
- lookTarget.X = 8 * (float)Math.Cos(0.3f * Time.CurrentExactTime);
- lookTarget.Y = 2.0f * (float)Math.Sin(5.0f * Time.CurrentExactTime);
- testRay.Direction = Vector.Normalize(lookTarget - testRay.Position);
- });
- }
- #endregion
-
- #region Test3DObjectPicking
- /// <summary>
- /// Test 3D object picking
- /// </summary>
- private static void Test3DObjectPicking()
- {
- // create a box
- VisualPhysicsBox visualBox = new VisualPhysicsBox(Vector.Zero, 4);
-
- // this line of code also sets the camera, i.e. ViewProjection matrix
- LookAtCamera cam = new LookAtCamera(new Vector(0, 25, 10));
-
- Ray mouseRay = new Ray();
-
- Application.Start(delegate
- {
- bool rayCasted = false;
- if (Input.Mouse.RightButtonIsPressed)
- {
- // create ray from mouse position
- Point mousePosition = Input.Mouse.Position;
- mouseRay = ScreenSpace.GetRayFromScreenPoint(mousePosition);
-
- PhysicsBody catchedBody;
- Vector normal;
- float fraction;
-
- // perform ray cast
- rayCasted = Physics.FindRayCast(mouseRay, false,
- out catchedBody, out normal, out fraction);
- }
-
- if (rayCasted)
- {
- Font.Default.Draw("Hit", ScreenSpace.DrawArea);
- visualBox.DebugDrawColor = Color.Red;
- }
- else
- {
- visualBox.DebugDrawColor = Color.Green;
- }
-
- visualBox.Draw();
- });
- }
- #endregion
-
- #region TestMultiple3DObjectPicking
- /// <summary>
- /// Test 3D object picking
- /// </summary>
- private static void TestMultiple3DObjectPicking()
- {
- /*
- // create a box and a sphere
- VisualPhysicsBox visualBox = new VisualPhysicsBox(new Vector(-2, 0, 0), 4);
- VisualPhysicsSphere visualSphere = new VisualPhysicsSphere(new Vector(7, 0, 0), 3);
-
- // this line of code also sets the camera, i.e. ViewProjection matrix
- LookAtCamera cam = new LookAtCamera(new Vector(0, 25, 10));
-
- //Create the ray
- Ray mouseRay = new Ray();
-
- Application.Start(delegate
- {
- //Write some info on the screen
- //Font.Default.Write(new Point(0.5f, 0.2f), "Use the right mouse button to pick an object");
-
- IPhysicsShape3D catchedObject = null;
- if (Input.Mouse.RightButtonIsPressed)
- {
- // create ray from mouse position
- Point mousePosition = Input.Mouse.Position;
- mouseRay = ScreenSpace.GetRayFormScreenPoint(mousePosition);
-
- Vector intersection;
- Vector normal;
-
- // perform ray cast
- PhysicsManager.RayCast(mouseRay,
- out catchedObject, out intersection, out normal);
- }
-
-
- // Draw the box and sphere
- visualBox.Draw();
- visualSphere.Draw();
-
- //Reset the color of the box and the sphere
- visualBox.DebugDrawColor = Color.Green;
- visualSphere.DebugDrawColor = Color.Green;
-
-
- if (catchedObject != null)
- {
- var visualPhysicsShape = catchedObject.Owner as IVisualPhysicsShape;
- if (visualPhysicsShape != null)
- {
- visualPhysicsShape.DebugDrawColor = Color.Red;
- }
- }
- });*/
- }
- #endregion
-
- #region TestRestitution
- private static void TestRestitution()
- {
- // create 4 vertices that will define the "ground" static plane
- const int size = 250;
- const int height = -5;
-
- // add a plane to the physics world
- Physics.Instance.SetGroundPlane(true, height);
-
- // this line of code also sets the camera, i.e. ViewProjection matrix
- LookAtCamera camera = new LookAtCamera(new Vector(0, -10, 10));
-
- List<BaseVisualPhysicsShape> drawable =
- new List<BaseVisualPhysicsShape>();
-
- for (int i = 0; i < 11; i++)
- {
- Vector position = new Vector(-15 + i * 3 + 10, 0, 5);
- VisualPhysicsSphere sphere =
- new VisualPhysicsSphere(position + Vector.UnitZ * 30, 1.0f);
-
- // Set restitution.
- sphere.Body.Restitution = i / 3.0f;
-
- drawable.Add(sphere);
- }
-
- Mesh plane = Mesh.CreatePlane("GroundPlane", size, size,
- new MaterialData());
-
- Application.Start(delegate
- {
- // Draw the ground plane
- Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
- plane.Draw(ref planeHeight);
-
- foreach (BaseVisualPhysicsShape shape in drawable)
- {
- shape.Draw();
- }
- Font.DrawTopLeftInformation("Fps: " + Time.Fps);
- });
- }
- #endregion
-
- #region TestRopeSimulation
- private static void TestRopeSimulation()
- {
- // Enable debug rendering.
- Physics.DebugEnabled = true;
-
- // create 4 vertices that will define the "ground" static plane
- const int size = 50;
- const int height = -5;
-
- // add a plane to the physics world
- Physics.Instance.SetGroundPlane(true, height);
-
- // this line of code also sets the camera, i.e. ViewProjection matrix
- LookAtCamera camera = new LookAtCamera(new Vector(30, -30, 10));
-
- List<BaseVisualPhysicsShape> drawable = new List<BaseVisualPhysicsShape>();
-
- BaseVisualPhysicsShape last = null;
-
- for (int i = 0; i < 40; i++)
- {
- BaseVisualPhysicsShape shape = new VisualPhysicsSphere(
- new Vector(i * 1.5f - 20, 0, 3.5f), 0.5f);
-
- drawable.Add(shape);
-
- Vector pos2 = shape.Body.Position;
-
- if (last != null)
- {
- Vector pos3 = last.Body.Position;
-
- Vector dif = pos2 - pos3;
- dif = dif * 0.5f;
- dif = pos2 - dif;
-
- PhysicsJoint joint = Physics.CreatePointOnPoint(
- last.Body,
- shape.Body,
- dif);
- }
-
- last = shape;
- }
-
- int index = 0;
- BaseVisualPhysicsShape controllingShape = drawable[index];
- Mesh plane = Mesh.CreatePlane("GroundPlane", size, size,
- new MaterialData());
-
- Application.Start(delegate
- {
- // Draw the ground plane
- Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
- plane.Draw(ref planeHeight);
-
- if (Input.Keyboard.SpaceReleased)
- {
- if (index + 1 > drawable.Count)
- {
- index = 0;
- }
-
- controllingShape = drawable[index++];
- }
-
- // Up movement
- if (Input.Keyboard.IsPressed(InputButton.W))
- {
- controllingShape.Body.ApplyLinearImpulse(
- new Vector(0, 0, 2), controllingShape.Body.Position);
- }
-
- // Down movement
- if (Input.Keyboard.IsPressed(InputButton.S))
- {
- controllingShape.Body.ApplyLinearImpulse(
- new Vector(0, 0, -2), controllingShape.Body.Position);
- }
-
- // Left movement
- if (Input.Keyboard.IsPressed(InputButton.A))
- {
- controllingShape.Body.ApplyLinearImpulse(
- new Vector(-2, 0, 0), controllingShape.Body.Position);
- }
-
- // Right movement
- if (Input.Keyboard.IsPressed(InputButton.D))
- {
- controllingShape.Body.ApplyLinearImpulse(
- new Vector(2, 0, 0), controllingShape.Body.Position);
- }
-
- foreach (BaseVisualPhysicsShape shape in drawable)
- {
- shape.Draw();
- }
-
- // Draw info
- Font.DrawTopLeftInformation(
- "Fps: " + Time.Fps + "\n" +
- "Use W,A,S,D to translate joint\n" +
- "Use Space to select next joint");
- });
- }
- #endregion
-
- #region TestPrismaticJoint
- private static void TestPrismaticJoint()
- {
- // create 4 vertices that will define the "ground" static plane
- const int size = 50;
- const int height = -17;
-
- // add a plane to the physics world
- Physics.Instance.SetGroundPlane(true, height);
-
- // this line of code also sets the camera, i.e. ViewProjection matrix
- LookAtCamera camera = new LookAtCamera(new Vector(30, -30, 0));
-
- List<BaseVisualPhysicsShape> drawable = new List<BaseVisualPhysicsShape>();
-
- BaseVisualPhysicsShape body1 = new VisualPhysicsBox(new Vector(0, 0, 7), 1.0f);
- BaseVisualPhysicsShape body2 = new VisualPhysicsBox(new Vector(0, 0, 4), 1.0f);
- drawable.Add(body1);
- drawable.Add(body2);
-
- // Add a prismatic joint. The minimum allowed distance is 3. The maximum
- // allowed distance is also 3, the body should be fixed on the slider.
- Physics.CreatePrismatic(body1.Body, body2.Body, 3, 3, 1.0f, 0.0f);
- Mesh plane = Mesh.CreatePlane("GroundPlane", size, size,
- new MaterialData());
-
- Application.Start(delegate
- {
- // Draw the ground plane
- Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
- plane.Draw(ref planeHeight);
-
- // Up movement
- if (Input.Keyboard.IsPressed(InputButton.W))
- {
- body1.Body.ApplyAngularImpulse(new Vector(0, 0, 0.2f));
- }
-
- // Down movement
- if (Input.Keyboard.IsPressed(InputButton.S))
- {
- body1.Body.ApplyAngularImpulse(new Vector(0, 0, -0.2f));
- }
-
- // Left movement
- if (Input.Keyboard.IsPressed(InputButton.A))
- {
- body1.Body.ApplyAngularImpulse(new Vector(-0.2f, 0, 0));
- }
-
- // Right movement
- if (Input.Keyboard.IsPressed(InputButton.D))
- {
- body1.Body.ApplyAngularImpulse(new Vector(0.2f, 0, 0));
- }
-
- foreach (BaseVisualPhysicsShape shape in drawable)
- {
- shape.Draw();
- }
-
- // Draw info
- //Font.DrawTopLeftInformation(
- // "Fps: "+Time.Fps+"\n"+,
- // "Use W,A,S,D to move upper joint");
- });
- }
- #endregion
-
- #region TestCollisionEvents
- /// <summary>
- /// Performs collision check between two bodies with events.
- /// </summary>
- private static void TestCollisionEvents()
- {
- // create 4 vertices that will define the "ground" static plane
- const int size = 50;
- const int height = -5;
-
- // add a plane to the physics world
- Physics.Instance.SetGroundPlane(true, height);
- Physics.GroundBody.Name = "PlaneBody";
- Physics.GroundBody.CollisionBegin += BodyCollisionBegin;
- Physics.GroundBody.CollisionEnd += GroundBodyCollisionEnd;
-
- // this line of code also sets the camera, i.e. ViewProjection matrix
- LookAtCamera camera = new LookAtCamera(new Vector(30, -30, 10));
-
- List<BaseVisualPhysicsShape> drawable = new List<BaseVisualPhysicsShape>();
-
- BaseVisualPhysicsShape body1 =
- new VisualPhysicsBox(new Vector(0, 0, 27), 1.0f);
- BaseVisualPhysicsShape body2 =
- new VisualPhysicsBox(new Vector(0, 0, 24), 1.0f);
- drawable.Add(body1);
- drawable.Add(body2);
- body1.Body.Name = "Body1";
- body2.Body.Name = "Body2";
-
- int index = 3;
- Mesh plane = Mesh.CreatePlane("GroundPlane", size, size,
- new MaterialData());
-
- Application.Start(delegate
- {
- // Draw the ground plane
- Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
- plane.Draw(ref planeHeight);
-
- foreach (BaseVisualPhysicsShape shape in drawable)
- {
- shape.Draw();
- }
-
- if (Input.Mouse.RightButtonIsPressed)
- {
- BaseVisualPhysicsShape body = new VisualPhysicsBox(new Vector(0, 0, 27),
- 1.0f);
- body.Body.Name = "Body" + (index++);
- drawable.Add(body);
- }
- });
- }
- #endregion
-
- #region GroundBodyCollisionEnd
- private static void GroundBodyCollisionEnd(PhysicsBody other)
- {
- Log.Info(
- "Given body '" + other.Name + "' ends collision with the ground " +
- "plane body.");
- }
- #endregion
-
- #region BodyCollisionBegin
- private static void BodyCollisionBegin(PhysicsBody other)
- {
- Log.Info(
- "Given body '" + other.Name + "' collide with this plane body.");
- }
- #endregion
-
- #region TestJigLibXCreateScene1
- /// <summary>
- /// JigLibX game CreateScene1 test unit.
- /// </summary>
- /// <param name="dim">Dimension of the cube grid created.</param>
- private static void TestJigLibXCreateScene1(int dim)
- {
- // create 4 vertices that will define the "ground" static plane
- const int size = 50;
- const int height = -5;
-
- // add a plane to the physics world
- Physics.Instance.SetGroundPlane(true, height);
-
- // this line of code also sets the camera, i.e. ViewProjection matrix
- LookAtCamera camera = new LookAtCamera(new Vector(30, -30, 10));
-
- List<BaseVisualPhysicsShape> drawable = new List<BaseVisualPhysicsShape>();
-
- for (int x = 0; x < dim; x++)
- {
- for (int y = 0; y < dim; y++)
- {
- VisualPhysicsBox box = null;
- if (y % 2 == 0)
- {
- box = new VisualPhysicsBox(
- new Vector(x * 1.01f - 10.0f, y * 1.01f - 14.5f, 25), 1.0f);
- }
- else
- {
- box = new VisualPhysicsBox(
- new Vector(x * 1.01f - 10.5f, y * 1.01f - 14.5f, 25), 1.0f);
- }
-
- drawable.Add(box);
- }
- }
-
- int index = 3;
- Mesh plane = Mesh.CreatePlane("GroundPlane", size, size,
- new MaterialData());
-
- Application.Start(delegate
- {
- // Draw the ground plane
- Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
- plane.Draw(ref planeHeight);
-
- foreach (BaseVisualPhysicsShape shape in drawable)
- {
- shape.Draw();
- }
-
- if (Input.Mouse.RightButtonIsPressed)
- {
- BaseVisualPhysicsShape body = new VisualPhysicsBox(new Vector(0, 0, 27),
- 1.0f);
- body.Body.Name = "Body" + (index++);
- drawable.Add(body);
- }
- });
- }
- #endregion
-
- #region TestJigLibXCreateScene3
- /// <summary>
- /// JigLibX game CreateScene3 test unit.
- /// </summary>
- private static void TestJigLibXCreateScene3()
- {
- // create 4 vertices that will define the "ground" static plane
- const int size = 50;
- const int height = -5;
-
- // add a plane to the physics world
- Physics.Instance.SetGroundPlane(true, height);
-
- // this line of code also sets the camera, i.e. ViewProjection matrix
- LookAtCamera camera = new LookAtCamera(new Vector(30, -30, 10));
-
- List<BaseVisualPhysicsShape> chainBoxes = new List<BaseVisualPhysicsShape>();
-
- for (int i = 0; i < 25; i++)
- {
- VisualPhysicsBox boxObject =
- new VisualPhysicsBox(new Vector(i, 25 - i, 0), 1.0f);
- if (i == 0)
- {
- boxObject.IsStatic = true;
- }
- chainBoxes.Add(boxObject);
- }
-
- for (int i = 1; i < 25; i++)
- {
- // Create HingeJoint
- PhysicsJoint joint = Physics.CreateHinge(
- chainBoxes[i - 1].Body, chainBoxes[i].Body, Vector.UnitZ,
- new Vector(0.5f, -0.5f, 0.0f));
- }
-
- Mesh plane = Mesh.CreatePlane("GroundPlane", size, size,
- new MaterialData());
-
- Application.Start(delegate
- {
- // Draw the ground plane
- Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
- plane.Draw(ref planeHeight);
-
- foreach (BaseVisualPhysicsShape shape in chainBoxes)
- {
- shape.Draw();
- }
- });
- }
- #endregion
-
- #endregion
- }
- }