PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1. using System;
  2. using System.Collections.Generic;
  3. using Delta.ContentSystem.Rendering;
  4. using Delta.ContentSystem.Rendering.Helpers;
  5. using Delta.Engine;
  6. using Delta.InputSystem;
  7. using Delta.PhysicsEngines.Advanced;
  8. using Delta.PhysicsEngines.Effects;
  9. using Delta.PhysicsEngines.VisualShapes;
  10. using Delta.Rendering;
  11. using Delta.Rendering.Basics.Drawing;
  12. using Delta.Rendering.Basics.Fonts;
  13. using Delta.Rendering.Cameras;
  14. using Delta.Rendering.Effects;
  15. using Delta.Rendering.Effects.Modifiers;
  16. using Delta.Rendering.Enums;
  17. using Delta.Rendering.Models;
  18. using Delta.Utilities;
  19. using Delta.Utilities.Datatypes;
  20. using Delta.Utilities.Datatypes.Advanced;
  21. using Delta.Utilities.Math;
  22. using NUnit.Framework;
  23. namespace Delta.PhysicsEngines.Tests3D
  24. {
  25. /// <summary>
  26. /// Physics tests
  27. /// </summary>
  28. internal class Program
  29. {
  30. #region Main
  31. /// <summary>
  32. /// Main entry point, will just call one of the tests, uncomment the rest
  33. /// </summary>
  34. private static void Main()
  35. {
  36. // Note: In the settings for this project Jitter is used. You can switch
  37. // to other physics engines here (required files are copied automatically,
  38. // but for your release you should set this to the content project).
  39. // Warning: Farseer has no 3D Physics support (just good for 2D)
  40. //Settings.Modules.PhysicsModule = "Bullet";
  41. //Settings.Modules.PhysicsModule = "JigLib";
  42. //Settings.Modules.PhysicsModule = "Jitter";
  43. //coming soon: Settings.Modules.PhysicsModule = "Havok";
  44. //coming soon: Settings.Modules.PhysicsModule = "PhysX";
  45. #region Tests
  46. //TestSimpleSimulation3D();
  47. //TestRayCast();
  48. //Test3DObjectPicking();
  49. //TestMultiple3DObjectPicking();
  50. //TestControlledSpherePlane();
  51. //does not work: TestBallMovement();
  52. //TestSimpleRagdoll();
  53. //TestRestitution();
  54. //TestRopeSimulation();
  55. //TestPrismaticJoint();
  56. //TestCollisionEvents();
  57. //FountainEffectPhysics3D();
  58. //FountainPhysicsEffectWithTornado3D();
  59. //TestJigLibXCreateScene1(9);
  60. //TestJigLibXCreateScene3();
  61. //Jitter.JitterPhysics.JitterPhysicsTests.TestCreate3DBodyWithShape();
  62. #endregion
  63. #region Tutorials
  64. //Tutorials.Simple3DSimulation();
  65. //Tutorials.RopeSimulation();
  66. //Tutorials.CollisionEvents();
  67. //Tutorials.ControlledSphere();
  68. //Tutorials.RayCasting3D();
  69. Tutorials.PyramidSimulation();
  70. //Tutorials.BodyRestitution();
  71. #endregion
  72. }
  73. #endregion
  74. #region TestSimpleSimulation3D (Static)
  75. /// <summary>
  76. /// Test a simple simulation with 3D physics
  77. /// </summary>
  78. [Test, Category("Visual")]
  79. public static void TestSimpleSimulation3D()
  80. {
  81. // create 4 vertices that will define the "ground" static plane
  82. const int planeSize = 200;
  83. const int height = -5;
  84. // Enable plane to physics world.
  85. Physics.Instance.SetGroundPlane(true, height);
  86. Physics.DebugEnabled = true;
  87. // set the params of the spheres
  88. float radius = 2.0f;
  89. Vector spherePosition1 = new Vector(2.0f, 0.0f, 5.0f);
  90. Vector spherePosition2 = new Vector(12.0f, 0.0f, 5.0f);
  91. // create two sphere and add them to the physics world
  92. VisualPhysicsSphere visualSphere1 = new VisualPhysicsSphere(
  93. spherePosition1, radius, Color.Red);
  94. VisualPhysicsSphere visualSphere2 = new VisualPhysicsSphere(
  95. spherePosition2, radius, Color.Green);
  96. // create two boxes
  97. VisualPhysicsBox visualBox1 = new VisualPhysicsBox(
  98. spherePosition1 + 15.0f * Vector.UnitZ, radius * 2);
  99. VisualPhysicsBox visualBox2 = new VisualPhysicsBox(
  100. visualBox1.Body.Position + new Vector(3.0f, 1.0f, 7.0f) +
  101. 5.0f * Vector.UnitZ + 1.0f * Vector.UnitX, radius * 2);
  102. // this line of code also sets the camera, i.e. ViewProjection matrix
  103. LookAtCamera camera = new LookAtCamera(new Vector(0, -10, 10));
  104. Font fpsFont = new Font(Font.Default,
  105. HorizontalAlignment.Left, VerticalAlignment.Top);
  106. Mesh plane = Mesh.CreatePlane("GroundPlane", planeSize, planeSize,
  107. new MaterialData());
  108. Application.Start(delegate
  109. {
  110. fpsFont.Draw("Fps: " + Time.Fps, ScreenSpace.DrawArea);
  111. // Draw the spheres
  112. visualSphere1.Draw();
  113. visualSphere2.Draw();
  114. // Draw the boxes
  115. visualBox1.Draw();
  116. visualBox2.Draw();
  117. // Draw the ground plane
  118. Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
  119. plane.Draw(ref planeHeight);
  120. });
  121. }
  122. #endregion
  123. #region TestSimpleRagdoll (Static)
  124. /// <summary>
  125. /// Test a simple simulation with ragdoll
  126. /// </summary>
  127. [Test, Category("Visual")]
  128. public static void TestSimpleRagdoll()
  129. {
  130. // create 4 vertices that will define the "ground" static plane
  131. const int size = 50;
  132. const int height = -5;
  133. // add a plane to the physics world
  134. Physics.Instance.SetGroundPlane(true, height);
  135. List<Ragdoll> ragdolls = new List<Ragdoll>();
  136. for (int i = 3; i < 8; i++)
  137. {
  138. for (int e = 3; e < 8; e++)
  139. {
  140. Vector position = new Vector(i * 6 - 25, e * 6 - 25, 5);
  141. ragdolls.Add(new Ragdoll(position));
  142. }
  143. }
  144. // this line of code also sets the camera, i.e. ViewProjection matrix
  145. LookAtCamera camera = new LookAtCamera(new Vector(0, -10, 10));
  146. Mesh plane = Mesh.CreatePlane("GroundPlane", size, size,
  147. new MaterialData());
  148. Application.Start(delegate
  149. {
  150. // Draw the ground plane
  151. Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
  152. plane.Draw(ref planeHeight);
  153. if (Input.Mouse.LeftButtonIsPressed)
  154. {
  155. Point mousePosition = Input.Mouse.Position;
  156. Ray ray = ScreenSpace.GetRayFromScreenPoint(mousePosition);
  157. PhysicsBody grabBody;
  158. Vector hitNormal;
  159. float fraction;
  160. bool result = Physics.FindRayCast(ray, false, out grabBody,
  161. out hitNormal, out fraction);
  162. if (result)
  163. {
  164. grabBody.IsActive = true;
  165. }
  166. }
  167. foreach (Ragdoll ragdoll in ragdolls)
  168. {
  169. ragdoll.Draw();
  170. }
  171. });
  172. }
  173. #endregion
  174. #region TestControlledSpherePlane (Static)
  175. /// <summary>
  176. /// Test a Sphere controlled by the user in a plane
  177. /// </summary>
  178. [Test, Category("Visual")]
  179. public static void TestControlledSpherePlane()
  180. {
  181. /*TODO
  182. // create 4 vertices that will define the "ground" static plane
  183. Vector v0 = new Vector(16, 16, -5);
  184. Vector v1 = new Vector(16, -16, -5);
  185. Vector v2 = new Vector(-16, -16, -5);
  186. Vector v3 = new Vector(-16, 16, -5);
  187. // add a plane to the physics world
  188. VisualPhysicsPlane groundPlane = new VisualPhysicsPlane(80, 80, new Vector(0, 0, -5), new MaterialData());//"NormalMap", "RocksHighDiffuse", "None", "None", "None", "None"));
  189. //Mesh sphereMesh = Mesh.CreateSphere("SomeName", 5, new MaterialData());
  190. // create the sphere and add it to the physics world
  191. VisualPhysicsSphere sphere = new VisualPhysicsSphere(new Vector(0, 0, 10), 3.0f, new MaterialData());//"NormalMap", "RocksHighDiffuse", "None", "None", "None", "None"));
  192. // set a controller for the sphere
  193. IController3D ctrler =
  194. PhysicsManager.CreateController(sphere.PhysicsSphere, 1.0f, 2.0f, 0.0f);
  195. // Manipulate the sphere with the help of the controller.
  196. Vector direction = new Vector(RandomHelper.RandomFloat(-1000, +1000),
  197. RandomHelper.RandomFloat(-1000, +1000),
  198. RandomHelper.RandomFloat(-1000, +1000));
  199. direction.Normalize();
  200. ctrler.MovingDirection = direction;
  201. ctrler.MoveSpeed = RandomHelper.RandomFloat(5, 30);
  202. // set the gravity
  203. PhysicsManager.Gravity = new Vector(0, 0, -9.8f);
  204. LookAtCamera camera = new LookAtCamera(new Vector(0, 25, 10));
  205. Application.Start(delegate
  206. {
  207. //Draw our sphere and groundPlane
  208. sphere.Draw();
  209. groundPlane.Draw();
  210. //Font.Default.WriteTopLeft(sphere.PhysicsSphere.Position.ToString());
  211. });*/
  212. }
  213. #endregion
  214. #region FountainEffectPhysics3D (Static)
  215. /// <summary>
  216. /// Tests that simulates usage of physics particles
  217. /// </summary>
  218. [Test, Category("Visual")]
  219. public static void FountainEffectPhysics3D()
  220. {
  221. // Set up camera
  222. LookAtCamera cam = new LookAtCamera(new Vector(1, -5, 3));
  223. float maxLifeTime = 30.0f;
  224. // Create a new template.
  225. EffectData data = EffectData.Get("<SmokeEffect3D>");
  226. // Add an emitter and modifiers.
  227. EmitterData emitter = new EmitterData();
  228. emitter.Modifiers.AddRange(
  229. new IEffectModifier[]
  230. {
  231. new AlwaysSpawnModifier(),
  232. new SpawnIntervalModifier
  233. {
  234. SpawnInterval = 0.4f,
  235. },
  236. new PhysicsModifier
  237. {
  238. DebugDisplay = true,
  239. PhysicsShapeType = ParticlePhysicsShape.Sphere,
  240. Mass = 10.0f,
  241. },
  242. new SizeModifier
  243. {
  244. WidthRange = 20f,
  245. HeightRange = 20f,
  246. },
  247. new ParticleLifetimeModifier
  248. {
  249. ParticleLifetime = maxLifeTime,
  250. },
  251. new RotationModifier
  252. {
  253. Rotation = new Range(0f, 360f),
  254. },
  255. new MaterialModifier
  256. {
  257. Material = new MaterialColored("SmokeSmallAdditive")
  258. {
  259. BlendColor = new Color(0.3f, 0.4f, 0.8f),
  260. },
  261. BillboardMode = BillboardMode.CameraFacingPrecise,
  262. },
  263. });
  264. data.Emitters.Add(emitter);
  265. int effectId = Effect.Spawn(data, new Vector(0.0f, 0.0f, 8.5f));
  266. // Setup Physics
  267. Physics.Gravity = new Vector(0f, 0f, -9.81f);
  268. // Create invisile Plane
  269. Physics.Instance.SetGroundPlane(true, 0.0f);
  270. // Start the application.
  271. Application.Start(delegate
  272. {
  273. // Draw a grid for orientation.
  274. Grid.Draw();
  275. if (Input.Keyboard.GetState(InputButton.M) == InputState.Released)
  276. {
  277. Physics.Multithreading = !Physics.Multithreading;
  278. }
  279. Font.DrawTopLeftInformation(
  280. "Fps: " + Time.Fps + "\n" +
  281. Effect.NumberOfActiveParticles(effectId) +
  282. " currently active particles\n" +
  283. (Physics.Multithreading
  284. ? "Multithreaded "
  285. : "Single Threaded"));
  286. });
  287. }
  288. #endregion
  289. #region FountainPhysicsEffectWithTornado3D (Static)
  290. /// <summary>
  291. /// Tests that simulates usage of physics particles with apply
  292. /// of tornado, press N to activated, U to remove it.
  293. /// </summary>
  294. [Test, Category("Visual")]
  295. public static void FountainPhysicsEffectWithTornado3D()
  296. {
  297. // Set up camera
  298. LookAtCamera cam = new LookAtCamera(new Vector(1, -5, 3));
  299. float maxLifeTime = 10.0f;
  300. // Create a new template.
  301. EffectData data = EffectData.Get("<SmokeEffect3D>");
  302. // Add an emitter and modifiers.
  303. EmitterData emitter = new EmitterData();
  304. emitter.Modifiers.AddRange(
  305. new IEffectModifier[]
  306. {
  307. new AlwaysSpawnModifier(),
  308. new SpawnIntervalModifier
  309. {
  310. SpawnInterval = 1.0f,
  311. },
  312. new PhysicsModifier
  313. {
  314. DebugDisplay = true,
  315. PhysicsShapeType = ParticlePhysicsShape.Sphere,
  316. Mass = 10.0f,
  317. },
  318. new SizeModifier
  319. {
  320. WidthRange = 20f,
  321. HeightRange = 20f,
  322. },
  323. new ParticleLifetimeModifier
  324. {
  325. ParticleLifetime = maxLifeTime,
  326. },
  327. new RotationModifier
  328. {
  329. Rotation = new Range(0f, 360f),
  330. },
  331. new MaterialModifier
  332. {
  333. Material = new MaterialColored("SmokeSmallAdditive")
  334. {
  335. BlendColor = new Color(0.3f, 0.4f, 0.8f),
  336. },
  337. BillboardMode = BillboardMode.CameraFacingPrecise,
  338. },
  339. });
  340. data.Emitters.Add(emitter);
  341. int effectId = Effect.Spawn(data, new Vector(0.0f, 0.0f, 8.5f));
  342. // Setup Physics
  343. Physics.Gravity = new Vector(0f, 0f, -9.81f);
  344. // Create invisile Plane
  345. Physics.Instance.SetGroundPlane(true, 0.0f);
  346. Tornado tornado = Physics.CreateTornado(Vector.Zero);
  347. tornado.IsEnabled = false;
  348. // Start the application.
  349. Application.Start(delegate
  350. {
  351. // Draw a grid for orientation.
  352. Grid.Draw();
  353. if (Input.Keyboard.GetState(InputButton.N) == InputState.Released)
  354. {
  355. tornado.IsEnabled = !tornado.IsEnabled;
  356. tornado.Position = new Vector(0, 0, -5.0f);
  357. if (tornado.IsEnabled &&
  358. tornado.IsRemoved)
  359. {
  360. // Trigger to add to simulation if removed with U button.
  361. tornado.AddToSimulation();
  362. }
  363. }
  364. if (Input.Keyboard.GetState(InputButton.U) == InputState.Released)
  365. {
  366. Physics.RemoveForceField(tornado);
  367. }
  368. if (Input.Keyboard.GetState(InputButton.M) == InputState.Released)
  369. {
  370. Physics.Multithreading = !Physics.Multithreading;
  371. }
  372. Font.DrawTopLeftInformation(
  373. "Fps: " + Time.Fps + "\n" +
  374. "Currently active particles: " +
  375. Effect.NumberOfActiveParticles(effectId) + "\n" +
  376. (Physics.Multithreading
  377. ? "Multithreaded "
  378. : "Single Threaded"));
  379. });
  380. }
  381. #endregion
  382. #region Methods (Private)
  383. #region TestRayCast
  384. /// <summary>
  385. /// Test ray cast
  386. /// </summary>
  387. private static void TestRayCast()
  388. {
  389. // create a plane so that every ray that doesn't hit an object will hit
  390. // the plane
  391. const int size = 50;
  392. const int height = -5;
  393. // add a plane to the physics world
  394. Physics.Instance.SetGroundPlane(true, height);
  395. // create a box and a sphere
  396. VisualPhysicsBox visualBox = new VisualPhysicsBox(new Vector(-2, 0, 0), 4);
  397. VisualPhysicsSphere visualSphere =
  398. new VisualPhysicsSphere(new Vector(5, 0, 0), 2);
  399. // this line of code also sets the camera, i.e. ViewProjection matrix
  400. LookAtCamera cam = new LookAtCamera(new Vector(0, 25, 10));
  401. // create a moving ray
  402. Vector rayStart = new Vector(0, 0, 10);
  403. Vector lookTarget = new Vector();
  404. Ray testRay = new Ray(rayStart, lookTarget - rayStart);
  405. Mesh plane = Mesh.CreatePlane("GroundPlane", size, size, new MaterialData());
  406. Application.Start(delegate
  407. {
  408. // Draw the ground plane
  409. Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
  410. plane.Draw(ref planeHeight);
  411. PhysicsBody catchedBody;
  412. Vector normal;
  413. float fraction;
  414. //Draw the sphere and box
  415. visualBox.Draw();
  416. visualSphere.Draw();
  417. // perform ray cast
  418. bool rayCasted = Physics.FindRayCast(testRay, false,
  419. out catchedBody, out normal, out fraction);
  420. // update the objects color according to the result of the ray cast
  421. if (catchedBody == visualBox.Body)
  422. {
  423. visualBox.DebugDrawColor = Color.Red;
  424. }
  425. else
  426. {
  427. visualBox.DebugDrawColor = Color.Green;
  428. }
  429. if (catchedBody == visualSphere.Body)
  430. {
  431. visualSphere.DebugDrawColor = Color.Red;
  432. }
  433. else
  434. {
  435. visualSphere.DebugDrawColor = Color.Green;
  436. }
  437. // render the line according to the deleted intersection
  438. if (rayCasted)
  439. {
  440. // render up to the intersection point
  441. //DrawManager.Line3D(testRay.Position, testRay.Direction, Color.Yellow);
  442. // render the surface normal at this point
  443. //DrawManager.Line3D(testRay.Position, testRay.Direction + normal, Color.White);
  444. }
  445. else
  446. {
  447. //DrawManager.Line3D(testRay.Position,
  448. // testRay.Position + 15.0f * testRay.Direction, Color.Yellow);
  449. }
  450. // update the direction of the ray
  451. lookTarget.X = 8 * (float)Math.Cos(0.3f * Time.CurrentExactTime);
  452. lookTarget.Y = 2.0f * (float)Math.Sin(5.0f * Time.CurrentExactTime);
  453. testRay.Direction = Vector.Normalize(lookTarget - testRay.Position);
  454. });
  455. }
  456. #endregion
  457. #region Test3DObjectPicking
  458. /// <summary>
  459. /// Test 3D object picking
  460. /// </summary>
  461. private static void Test3DObjectPicking()
  462. {
  463. // create a box
  464. VisualPhysicsBox visualBox = new VisualPhysicsBox(Vector.Zero, 4);
  465. // this line of code also sets the camera, i.e. ViewProjection matrix
  466. LookAtCamera cam = new LookAtCamera(new Vector(0, 25, 10));
  467. Ray mouseRay = new Ray();
  468. Application.Start(delegate
  469. {
  470. bool rayCasted = false;
  471. if (Input.Mouse.RightButtonIsPressed)
  472. {
  473. // create ray from mouse position
  474. Point mousePosition = Input.Mouse.Position;
  475. mouseRay = ScreenSpace.GetRayFromScreenPoint(mousePosition);
  476. PhysicsBody catchedBody;
  477. Vector normal;
  478. float fraction;
  479. // perform ray cast
  480. rayCasted = Physics.FindRayCast(mouseRay, false,
  481. out catchedBody, out normal, out fraction);
  482. }
  483. if (rayCasted)
  484. {
  485. Font.Default.Draw("Hit", ScreenSpace.DrawArea);
  486. visualBox.DebugDrawColor = Color.Red;
  487. }
  488. else
  489. {
  490. visualBox.DebugDrawColor = Color.Green;
  491. }
  492. visualBox.Draw();
  493. });
  494. }
  495. #endregion
  496. #region TestMultiple3DObjectPicking
  497. /// <summary>
  498. /// Test 3D object picking
  499. /// </summary>
  500. private static void TestMultiple3DObjectPicking()
  501. {
  502. /*
  503. // create a box and a sphere
  504. VisualPhysicsBox visualBox = new VisualPhysicsBox(new Vector(-2, 0, 0), 4);
  505. VisualPhysicsSphere visualSphere = new VisualPhysicsSphere(new Vector(7, 0, 0), 3);
  506. // this line of code also sets the camera, i.e. ViewProjection matrix
  507. LookAtCamera cam = new LookAtCamera(new Vector(0, 25, 10));
  508. //Create the ray
  509. Ray mouseRay = new Ray();
  510. Application.Start(delegate
  511. {
  512. //Write some info on the screen
  513. //Font.Default.Write(new Point(0.5f, 0.2f), "Use the right mouse button to pick an object");
  514. IPhysicsShape3D catchedObject = null;
  515. if (Input.Mouse.RightButtonIsPressed)
  516. {
  517. // create ray from mouse position
  518. Point mousePosition = Input.Mouse.Position;
  519. mouseRay = ScreenSpace.GetRayFormScreenPoint(mousePosition);
  520. Vector intersection;
  521. Vector normal;
  522. // perform ray cast
  523. PhysicsManager.RayCast(mouseRay,
  524. out catchedObject, out intersection, out normal);
  525. }
  526. // Draw the box and sphere
  527. visualBox.Draw();
  528. visualSphere.Draw();
  529. //Reset the color of the box and the sphere
  530. visualBox.DebugDrawColor = Color.Green;
  531. visualSphere.DebugDrawColor = Color.Green;
  532. if (catchedObject != null)
  533. {
  534. var visualPhysicsShape = catchedObject.Owner as IVisualPhysicsShape;
  535. if (visualPhysicsShape != null)
  536. {
  537. visualPhysicsShape.DebugDrawColor = Color.Red;
  538. }
  539. }
  540. });*/
  541. }
  542. #endregion
  543. #region TestRestitution
  544. private static void TestRestitution()
  545. {
  546. // create 4 vertices that will define the "ground" static plane
  547. const int size = 250;
  548. const int height = -5;
  549. // add a plane to the physics world
  550. Physics.Instance.SetGroundPlane(true, height);
  551. // this line of code also sets the camera, i.e. ViewProjection matrix
  552. LookAtCamera camera = new LookAtCamera(new Vector(0, -10, 10));
  553. List<BaseVisualPhysicsShape> drawable =
  554. new List<BaseVisualPhysicsShape>();
  555. for (int i = 0; i < 11; i++)
  556. {
  557. Vector position = new Vector(-15 + i * 3 + 10, 0, 5);
  558. VisualPhysicsSphere sphere =
  559. new VisualPhysicsSphere(position + Vector.UnitZ * 30, 1.0f);
  560. // Set restitution.
  561. sphere.Body.Restitution = i / 3.0f;
  562. drawable.Add(sphere);
  563. }
  564. Mesh plane = Mesh.CreatePlane("GroundPlane", size, size,
  565. new MaterialData());
  566. Application.Start(delegate
  567. {
  568. // Draw the ground plane
  569. Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
  570. plane.Draw(ref planeHeight);
  571. foreach (BaseVisualPhysicsShape shape in drawable)
  572. {
  573. shape.Draw();
  574. }
  575. Font.DrawTopLeftInformation("Fps: " + Time.Fps);
  576. });
  577. }
  578. #endregion
  579. #region TestRopeSimulation
  580. private static void TestRopeSimulation()
  581. {
  582. // Enable debug rendering.
  583. Physics.DebugEnabled = true;
  584. // create 4 vertices that will define the "ground" static plane
  585. const int size = 50;
  586. const int height = -5;
  587. // add a plane to the physics world
  588. Physics.Instance.SetGroundPlane(true, height);
  589. // this line of code also sets the camera, i.e. ViewProjection matrix
  590. LookAtCamera camera = new LookAtCamera(new Vector(30, -30, 10));
  591. List<BaseVisualPhysicsShape> drawable = new List<BaseVisualPhysicsShape>();
  592. BaseVisualPhysicsShape last = null;
  593. for (int i = 0; i < 40; i++)
  594. {
  595. BaseVisualPhysicsShape shape = new VisualPhysicsSphere(
  596. new Vector(i * 1.5f - 20, 0, 3.5f), 0.5f);
  597. drawable.Add(shape);
  598. Vector pos2 = shape.Body.Position;
  599. if (last != null)
  600. {
  601. Vector pos3 = last.Body.Position;
  602. Vector dif = pos2 - pos3;
  603. dif = dif * 0.5f;
  604. dif = pos2 - dif;
  605. PhysicsJoint joint = Physics.CreatePointOnPoint(
  606. last.Body,
  607. shape.Body,
  608. dif);
  609. }
  610. last = shape;
  611. }
  612. int index = 0;
  613. BaseVisualPhysicsShape controllingShape = drawable[index];
  614. Mesh plane = Mesh.CreatePlane("GroundPlane", size, size,
  615. new MaterialData());
  616. Application.Start(delegate
  617. {
  618. // Draw the ground plane
  619. Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
  620. plane.Draw(ref planeHeight);
  621. if (Input.Keyboard.SpaceReleased)
  622. {
  623. if (index + 1 > drawable.Count)
  624. {
  625. index = 0;
  626. }
  627. controllingShape = drawable[index++];
  628. }
  629. // Up movement
  630. if (Input.Keyboard.IsPressed(InputButton.W))
  631. {
  632. controllingShape.Body.ApplyLinearImpulse(
  633. new Vector(0, 0, 2), controllingShape.Body.Position);
  634. }
  635. // Down movement
  636. if (Input.Keyboard.IsPressed(InputButton.S))
  637. {
  638. controllingShape.Body.ApplyLinearImpulse(
  639. new Vector(0, 0, -2), controllingShape.Body.Position);
  640. }
  641. // Left movement
  642. if (Input.Keyboard.IsPressed(InputButton.A))
  643. {
  644. controllingShape.Body.ApplyLinearImpulse(
  645. new Vector(-2, 0, 0), controllingShape.Body.Position);
  646. }
  647. // Right movement
  648. if (Input.Keyboard.IsPressed(InputButton.D))
  649. {
  650. controllingShape.Body.ApplyLinearImpulse(
  651. new Vector(2, 0, 0), controllingShape.Body.Position);
  652. }
  653. foreach (BaseVisualPhysicsShape shape in drawable)
  654. {
  655. shape.Draw();
  656. }
  657. // Draw info
  658. Font.DrawTopLeftInformation(
  659. "Fps: " + Time.Fps + "\n" +
  660. "Use W,A,S,D to translate joint\n" +
  661. "Use Space to select next joint");
  662. });
  663. }
  664. #endregion
  665. #region TestPrismaticJoint
  666. private static void TestPrismaticJoint()
  667. {
  668. // create 4 vertices that will define the "ground" static plane
  669. const int size = 50;
  670. const int height = -17;
  671. // add a plane to the physics world
  672. Physics.Instance.SetGroundPlane(true, height);
  673. // this line of code also sets the camera, i.e. ViewProjection matrix
  674. LookAtCamera camera = new LookAtCamera(new Vector(30, -30, 0));
  675. List<BaseVisualPhysicsShape> drawable = new List<BaseVisualPhysicsShape>();
  676. BaseVisualPhysicsShape body1 = new VisualPhysicsBox(new Vector(0, 0, 7), 1.0f);
  677. BaseVisualPhysicsShape body2 = new VisualPhysicsBox(new Vector(0, 0, 4), 1.0f);
  678. drawable.Add(body1);
  679. drawable.Add(body2);
  680. // Add a prismatic joint. The minimum allowed distance is 3. The maximum
  681. // allowed distance is also 3, the body should be fixed on the slider.
  682. Physics.CreatePrismatic(body1.Body, body2.Body, 3, 3, 1.0f, 0.0f);
  683. Mesh plane = Mesh.CreatePlane("GroundPlane", size, size,
  684. new MaterialData());
  685. Application.Start(delegate
  686. {
  687. // Draw the ground plane
  688. Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
  689. plane.Draw(ref planeHeight);
  690. // Up movement
  691. if (Input.Keyboard.IsPressed(InputButton.W))
  692. {
  693. body1.Body.ApplyAngularImpulse(new Vector(0, 0, 0.2f));
  694. }
  695. // Down movement
  696. if (Input.Keyboard.IsPressed(InputButton.S))
  697. {
  698. body1.Body.ApplyAngularImpulse(new Vector(0, 0, -0.2f));
  699. }
  700. // Left movement
  701. if (Input.Keyboard.IsPressed(InputButton.A))
  702. {
  703. body1.Body.ApplyAngularImpulse(new Vector(-0.2f, 0, 0));
  704. }
  705. // Right movement
  706. if (Input.Keyboard.IsPressed(InputButton.D))
  707. {
  708. body1.Body.ApplyAngularImpulse(new Vector(0.2f, 0, 0));
  709. }
  710. foreach (BaseVisualPhysicsShape shape in drawable)
  711. {
  712. shape.Draw();
  713. }
  714. // Draw info
  715. //Font.DrawTopLeftInformation(
  716. // "Fps: "+Time.Fps+"\n"+,
  717. // "Use W,A,S,D to move upper joint");
  718. });
  719. }
  720. #endregion
  721. #region TestCollisionEvents
  722. /// <summary>
  723. /// Performs collision check between two bodies with events.
  724. /// </summary>
  725. private static void TestCollisionEvents()
  726. {
  727. // create 4 vertices that will define the "ground" static plane
  728. const int size = 50;
  729. const int height = -5;
  730. // add a plane to the physics world
  731. Physics.Instance.SetGroundPlane(true, height);
  732. Physics.GroundBody.Name = "PlaneBody";
  733. Physics.GroundBody.CollisionBegin += BodyCollisionBegin;
  734. Physics.GroundBody.CollisionEnd += GroundBodyCollisionEnd;
  735. // this line of code also sets the camera, i.e. ViewProjection matrix
  736. LookAtCamera camera = new LookAtCamera(new Vector(30, -30, 10));
  737. List<BaseVisualPhysicsShape> drawable = new List<BaseVisualPhysicsShape>();
  738. BaseVisualPhysicsShape body1 =
  739. new VisualPhysicsBox(new Vector(0, 0, 27), 1.0f);
  740. BaseVisualPhysicsShape body2 =
  741. new VisualPhysicsBox(new Vector(0, 0, 24), 1.0f);
  742. drawable.Add(body1);
  743. drawable.Add(body2);
  744. body1.Body.Name = "Body1";
  745. body2.Body.Name = "Body2";
  746. int index = 3;
  747. Mesh plane = Mesh.CreatePlane("GroundPlane", size, size,
  748. new MaterialData());
  749. Application.Start(delegate
  750. {
  751. // Draw the ground plane
  752. Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
  753. plane.Draw(ref planeHeight);
  754. foreach (BaseVisualPhysicsShape shape in drawable)
  755. {
  756. shape.Draw();
  757. }
  758. if (Input.Mouse.RightButtonIsPressed)
  759. {
  760. BaseVisualPhysicsShape body = new VisualPhysicsBox(new Vector(0, 0, 27),
  761. 1.0f);
  762. body.Body.Name = "Body" + (index++);
  763. drawable.Add(body);
  764. }
  765. });
  766. }
  767. #endregion
  768. #region GroundBodyCollisionEnd
  769. private static void GroundBodyCollisionEnd(PhysicsBody other)
  770. {
  771. Log.Info(
  772. "Given body '" + other.Name + "' ends collision with the ground " +
  773. "plane body.");
  774. }
  775. #endregion
  776. #region BodyCollisionBegin
  777. private static void BodyCollisionBegin(PhysicsBody other)
  778. {
  779. Log.Info(
  780. "Given body '" + other.Name + "' collide with this plane body.");
  781. }
  782. #endregion
  783. #region TestJigLibXCreateScene1
  784. /// <summary>
  785. /// JigLibX game CreateScene1 test unit.
  786. /// </summary>
  787. /// <param name="dim">Dimension of the cube grid created.</param>
  788. private static void TestJigLibXCreateScene1(int dim)
  789. {
  790. // create 4 vertices that will define the "ground" static plane
  791. const int size = 50;
  792. const int height = -5;
  793. // add a plane to the physics world
  794. Physics.Instance.SetGroundPlane(true, height);
  795. // this line of code also sets the camera, i.e. ViewProjection matrix
  796. LookAtCamera camera = new LookAtCamera(new Vector(30, -30, 10));
  797. List<BaseVisualPhysicsShape> drawable = new List<BaseVisualPhysicsShape>();
  798. for (int x = 0; x < dim; x++)
  799. {
  800. for (int y = 0; y < dim; y++)
  801. {
  802. VisualPhysicsBox box = null;
  803. if (y % 2 == 0)
  804. {
  805. box = new VisualPhysicsBox(
  806. new Vector(x * 1.01f - 10.0f, y * 1.01f - 14.5f, 25), 1.0f);
  807. }
  808. else
  809. {
  810. box = new VisualPhysicsBox(
  811. new Vector(x * 1.01f - 10.5f, y * 1.01f - 14.5f, 25), 1.0f);
  812. }
  813. drawable.Add(box);
  814. }
  815. }
  816. int index = 3;
  817. Mesh plane = Mesh.CreatePlane("GroundPlane", size, size,
  818. new MaterialData());
  819. Application.Start(delegate
  820. {
  821. // Draw the ground plane
  822. Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
  823. plane.Draw(ref planeHeight);
  824. foreach (BaseVisualPhysicsShape shape in drawable)
  825. {
  826. shape.Draw();
  827. }
  828. if (Input.Mouse.RightButtonIsPressed)
  829. {
  830. BaseVisualPhysicsShape body = new VisualPhysicsBox(new Vector(0, 0, 27),
  831. 1.0f);
  832. body.Body.Name = "Body" + (index++);
  833. drawable.Add(body);
  834. }
  835. });
  836. }
  837. #endregion
  838. #region TestJigLibXCreateScene3
  839. /// <summary>
  840. /// JigLibX game CreateScene3 test unit.
  841. /// </summary>
  842. private static void TestJigLibXCreateScene3()
  843. {
  844. // create 4 vertices that will define the "ground" static plane
  845. const int size = 50;
  846. const int height = -5;
  847. // add a plane to the physics world
  848. Physics.Instance.SetGroundPlane(true, height);
  849. // this line of code also sets the camera, i.e. ViewProjection matrix
  850. LookAtCamera camera = new LookAtCamera(new Vector(30, -30, 10));
  851. List<BaseVisualPhysicsShape> chainBoxes = new List<BaseVisualPhysicsShape>();
  852. for (int i = 0; i < 25; i++)
  853. {
  854. VisualPhysicsBox boxObject =
  855. new VisualPhysicsBox(new Vector(i, 25 - i, 0), 1.0f);
  856. if (i == 0)
  857. {
  858. boxObject.IsStatic = true;
  859. }
  860. chainBoxes.Add(boxObject);
  861. }
  862. for (int i = 1; i < 25; i++)
  863. {
  864. // Create HingeJoint
  865. PhysicsJoint joint = Physics.CreateHinge(
  866. chainBoxes[i - 1].Body, chainBoxes[i].Body, Vector.UnitZ,
  867. new Vector(0.5f, -0.5f, 0.0f));
  868. }
  869. Mesh plane = Mesh.CreatePlane("GroundPlane", size, size,
  870. new MaterialData());
  871. Application.Start(delegate
  872. {
  873. // Draw the ground plane
  874. Matrix planeHeight = Matrix.CreateTranslation(0, 0, height);
  875. plane.Draw(ref planeHeight);
  876. foreach (BaseVisualPhysicsShape shape in chainBoxes)
  877. {
  878. shape.Draw();
  879. }
  880. });
  881. }
  882. #endregion
  883. #endregion
  884. }
  885. }