PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/PhysicsEngines/Tests2D/Program.cs

#
C# | 756 lines | 418 code | 115 blank | 223 comment | 17 complexity | 6512b8c081070d00d4f4603c2fcce748 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System.Collections.Generic;
  2. using Delta.Engine;
  3. using Delta.InputSystem;
  4. using Delta.Rendering.Basics.Fonts;
  5. using Delta.Rendering.Basics.Materials;
  6. using Delta.Utilities;
  7. using Delta.Utilities.Datatypes;
  8. using Delta.Utilities.Datatypes.Advanced;
  9. using Delta.Utilities.Helpers;
  10. using NUnit.Framework;
  11. namespace Delta.PhysicsEngines.Tests2D
  12. {
  13. /// <summary>
  14. /// Physics tests
  15. /// </summary>
  16. [Category("Visual")]
  17. internal class Program
  18. {
  19. #region Main (Static)
  20. /// <summary>
  21. /// Main entry point, will just call one of the tests, uncomment the rest
  22. /// </summary>
  23. public static void Main()
  24. {
  25. // Note: In the default settings Physics modules are not used, thus none
  26. // of the physics tests will work. For this project (which has the same
  27. // content project name: Delta.PhysicsEngines.Tests2D) Farseer is already
  28. // set in the content Settings.xml and will be used. Do the same for your
  29. // own projects or use a line like this to force a specific physics
  30. // module. All required files will be copied automatically.
  31. //Settings.Modules.PhysicsModule = "Farseer";
  32. #region Tests
  33. //TestSimpleSimulation2D();
  34. //TestJoints();
  35. //TestForceAndTorque();
  36. //TestSingleBodyTwoShapes();
  37. //TestFlummySimulation2D();
  38. //TestRayCast();
  39. //TestControlledSpherePlane();
  40. //TestRotatingBall();
  41. //TestBallMovement();
  42. //TestResize();
  43. //TestCollisionEvents();
  44. //BugTest();
  45. #endregion
  46. #region Tutorials
  47. //Tutorials.Simple2DSimulation();
  48. //Tutorials.JointsSimulation();
  49. //Tutorials.BouncyBallSimulation();
  50. //Tutorials.FlummySimulation();
  51. //Tutorials.RayCasting2D();
  52. //Tutorials.CollisionEvents();
  53. Tutorials.PyramidSimulation();
  54. #endregion
  55. }
  56. #endregion
  57. #region TestSimpleSimulation2D (Static)
  58. /// <summary>
  59. /// Test a simple simulation with 2D physics
  60. /// </summary>
  61. [Test]
  62. public static void TestSimpleSimulation2D()
  63. {
  64. Physics.Gravity = new Vector(0.0f, 0.0981f, 0.0f);
  65. // Enable debug drawing
  66. Physics.DebugEnabled = true;
  67. // Create some (2 balls and 1 box) and the ground and let them
  68. // collide due to gravity
  69. List<PhysicsBody> balls = new List<PhysicsBody>();
  70. PhysicsBody box = null;
  71. PhysicsBody ground = null;
  72. float radius = 0.05f;
  73. float mass = 1.0f;
  74. Rectangle rectangle = new Rectangle(0.1f, 0.65f, 0.8f, 0.1f);
  75. Rectangle boxRect = new Rectangle(0.0f, 0.0f, 0.05f, 0.05f);
  76. for (int index = 0; index < 10; index++)
  77. {
  78. PhysicsBody ball = Physics.CreateCircle(radius, mass);
  79. ball.Position = new Vector(RandomHelper.RandomFloat(-0.5f, 0.5f),
  80. RandomHelper.RandomFloat(-0.5f, 0.5f), 0.0f);
  81. ball.Friction = 1.0f;
  82. ball.IsStatic = false;
  83. // Set debug color to red.
  84. ball.DebugColor = Color.Red;
  85. balls.Add(ball);
  86. }
  87. box = Physics.CreateRectangle(
  88. boxRect.Width, boxRect.Height, 1.0f);
  89. box.Position = new Vector(.5f, .4f, 0.0f);
  90. box.IsStatic = false;
  91. box.Friction = 1.0f;
  92. box.DebugColor = Color.Green;
  93. // set our ground
  94. ground = Physics.CreateRectangle(
  95. rectangle.Width, rectangle.Height, 1f);
  96. ground.Position = new Vector(rectangle.Center, 0.0f);
  97. ground.IsStatic = true;
  98. ground.Friction = 1.0f;
  99. ground.DebugColor = Color.Yellow;
  100. Application.Start(delegate
  101. {
  102. //Font.DrawTopLeftInformation("Fps: "+Time.Fps);
  103. });
  104. }
  105. #endregion
  106. #region TestJoints (Static)
  107. /// <summary>
  108. /// Test a simple simulation with 2D physics and joints
  109. /// </summary>
  110. [Test]
  111. public static void TestJoints()
  112. {
  113. Physics.Gravity = new Vector(0.0f, 0.0981f, 0.0f);
  114. // Enable debug drawing
  115. Physics.DebugEnabled = true;
  116. // Create some (2 balls and 1 box) and the ground and let them
  117. // collide due to gravity
  118. List<PhysicsBody> balls = new List<PhysicsBody>();
  119. PhysicsBody box = null;
  120. PhysicsBody ground = null;
  121. float radius = 0.05f;
  122. float mass = 1.0f;
  123. Rectangle rectangle = new Rectangle(0.1f, 0.65f, 0.8f, 0.1f);
  124. Rectangle boxRect = new Rectangle(0.0f, 0.0f, 0.05f, 0.05f);
  125. for (int index = 0; index < 10; index++)
  126. {
  127. PhysicsBody ball = Physics.CreateCircle(radius, mass);
  128. ball.Position = new Vector(RandomHelper.RandomFloat(-0.5f, 0.5f),
  129. RandomHelper.RandomFloat(-0.5f, 0.5f), 0.0f);
  130. ball.Friction = 1.0f;
  131. ball.IsStatic = false;
  132. // Set debug color to red.
  133. ball.DebugColor = Color.Red;
  134. balls.Add(ball);
  135. } // for
  136. box = Physics.CreateRectangle(
  137. boxRect.Width, boxRect.Height, 1.0f);
  138. box.Position = new Vector(.5f, .4f, 0.0f);
  139. box.IsStatic = false;
  140. box.Friction = 1.0f;
  141. box.DebugColor = Color.Green;
  142. PhysicsBody box2 = Physics.CreateRectangle(
  143. boxRect.Width, boxRect.Height, 1.0f);
  144. box2.Position = new Vector(.6f, .4f, 0.0f);
  145. box2.IsStatic = false;
  146. box2.Friction = 1.0f;
  147. box2.DebugColor = Color.Green;
  148. // Create angle joint
  149. PhysicsJoint joint = Physics.CreateAngleJoint(box, box2, 0.0f);
  150. // set our ground
  151. ground = Physics.CreateRectangle(
  152. rectangle.Width, rectangle.Height, 1f);
  153. ground.Position = new Vector(rectangle.Center, 0.0f);
  154. ground.IsStatic = true;
  155. ground.Friction = 1.0f;
  156. ground.DebugColor = Color.Yellow;
  157. Application.Start(delegate
  158. {
  159. Font.DrawTopLeftInformation("Fps: " + Time.Fps);
  160. });
  161. }
  162. #endregion
  163. #region TestForceAndTorque (Static)
  164. /// <summary>
  165. /// Test a simple simulation with 2D physics with no gravity and
  166. /// how to apply force and torque
  167. /// </summary>
  168. [Test]
  169. public static void TestForceAndTorque()
  170. {
  171. Physics.Gravity = new Vector(0.0f, 0.0981f, 0.0f);
  172. // Enable debug drawing
  173. Physics.DebugEnabled = true;
  174. PhysicsBody box = null;
  175. PhysicsBody ground = null;
  176. Rectangle rectangle = new Rectangle(0.1f, 0.65f, 0.8f, 0.1f);
  177. Rectangle boxRect = new Rectangle(0.0f, 0.0f, 0.05f, 0.05f);
  178. box = Physics.CreateRectangle(
  179. boxRect.Width, boxRect.Height, 1.0f);
  180. box.Position = new Vector(.5f, .4f, 0.0f);
  181. box.IsStatic = false;
  182. box.Friction = 1.0f;
  183. box.DebugColor = Color.Green;
  184. // Set our ground
  185. ground = Physics.CreateRectangle(
  186. rectangle.Width, rectangle.Height, 1f);
  187. ground.Position = new Vector(rectangle.Center, 0.0f);
  188. ground.IsStatic = true;
  189. ground.Friction = 1.0f;
  190. ground.DebugColor = Color.Yellow;
  191. // Farseer
  192. PhysicsBody rectBody = Physics.CreateRectangle(.05f, .05f, 1);
  193. rectBody.Position = new Vector(0.3f, 0.3f, 0.0f);
  194. rectBody.IsStatic = false;
  195. rectBody.DebugColor = Color.Gold;
  196. Application.Start(delegate
  197. {
  198. const float forceAmount = 60;
  199. const float torqueAmount = 40;
  200. Vector force = Vector.Zero;
  201. // Apply left force
  202. if (Input.Keyboard.IsPressed(InputButton.A))
  203. {
  204. force += new Vector(-forceAmount, 0, 0);
  205. }
  206. // Apply down force
  207. if (Input.Keyboard.IsPressed(InputButton.S))
  208. {
  209. force += new Vector(0, forceAmount, 0);
  210. }
  211. // Apply right force
  212. if (Input.Keyboard.IsPressed(InputButton.D))
  213. {
  214. force += new Vector(forceAmount, 0, 0);
  215. }
  216. // Apply up force
  217. if (Input.Keyboard.IsPressed(InputButton.W))
  218. {
  219. force += new Vector(0, -forceAmount, 0);
  220. }
  221. rectBody.ApplyForce(force);
  222. float torque = 0;
  223. if (Input.Keyboard.IsPressed(InputButton.Q))
  224. {
  225. torque += torqueAmount;
  226. }
  227. if (Input.Keyboard.IsPressed(InputButton.E))
  228. {
  229. torque -= torqueAmount;
  230. }
  231. rectBody.ApplyTorque(torque);
  232. Font.DrawTopLeftInformation("Fps: " + Time.Fps);
  233. });
  234. }
  235. #endregion
  236. #region TestSingleBodyTwoShapes (Static)
  237. /// <summary>
  238. /// Test a simple simulation with 2D physics with no gravity and
  239. /// single body with two shapes.
  240. /// </summary>
  241. [Test]
  242. public static void TestSingleBodyTwoShapes()
  243. {
  244. Physics.Gravity = new Vector(0.0f, 0.0981f, 0.0f);
  245. // Enable debug drawing
  246. Physics.DebugEnabled = true;
  247. //PhysicsBody box = null;
  248. //PhysicsBody ground = null;
  249. //Rectangle rectangle = new Rectangle(0.1f, 0.65f, 0.8f, 0.1f);
  250. //// set our ground
  251. //ground = Physics.CreateRectangle(
  252. // rectangle.Width, rectangle.Height, 1f);
  253. //ground.Position = new Vector(rectangle.Center, 0.0f);
  254. //ground.IsStatic = true;
  255. //ground.Friction = 1.0f;
  256. //ground.DebugColor = Color.Yellow;
  257. //// Farseer
  258. //PhysicsBody rectBody = Physics.Create2DBody(
  259. // new Point(0.3f, 0.3f));
  260. //rectBody.IsStatic = false;
  261. //Application.Start(delegate
  262. //{
  263. // const float forceAmount = 60;
  264. // const float torqueAmount = 40;
  265. // Vector force = Vector.Zero;
  266. // // Apply left force
  267. // if (Input.Keyboard.IsPressed(InputButton.A))
  268. // {
  269. // force += new Vector(-forceAmount, 0, 0);
  270. // }
  271. // // Apply down force
  272. // if (Input.Keyboard.IsPressed(InputButton.S))
  273. // {
  274. // force += new Vector(0, forceAmount, 0);
  275. // }
  276. // // Apply right force
  277. // if (Input.Keyboard.IsPressed(InputButton.D))
  278. // {
  279. // force += new Vector(forceAmount, 0, 0);
  280. // }
  281. // // Apply up force
  282. // if (Input.Keyboard.IsPressed(InputButton.W))
  283. // {
  284. // force += new Vector(0, -forceAmount, 0);
  285. // }
  286. // rectBody.ApplyForce(force);
  287. // float torque = 0;
  288. // if (Input.Keyboard.IsPressed(InputButton.Q))
  289. // {
  290. // torque += torqueAmount;
  291. // }
  292. // if (Input.Keyboard.IsPressed(InputButton.E))
  293. // {
  294. // torque -= torqueAmount;
  295. // }
  296. // rectBody.ApplyTorque(torque);
  297. // //Font.DrawTopLeftInformation(Time.Fps.ToString());
  298. //});
  299. }
  300. #endregion
  301. #region TestFlummySimulation2D (Static)
  302. /// <summary>
  303. /// Test flummy simulation 2D
  304. /// </summary>
  305. [Test]
  306. public static void TestFlummySimulation2D()
  307. {
  308. Physics.Gravity = new Vector(0.0f, 0.0981f, 0.0f);
  309. Physics.DebugEnabled = true;
  310. // Border properties
  311. float density = 5.0f;
  312. float friction = 1.0f;
  313. float restitution = 1.0f;
  314. // Border rectangle
  315. Rectangle leftRect = new Rectangle(0.0f, 0.0f, 0.1f, 1.0f);
  316. Rectangle topRect = new Rectangle(0.1f, 0.1f, 0.8f, 0.1f);
  317. Rectangle rightRect = new Rectangle(0.9f, 0.0f, 0.1f, 1.0f);
  318. Rectangle bottomRect = new Rectangle(0.1f, 0.8f, 0.8f, 0.1f);
  319. PhysicsBody leftBorder =
  320. Physics.CreateRectangle(leftRect.Width, leftRect.Height, density);
  321. leftBorder.Friction = friction;
  322. leftBorder.Restitution = restitution;
  323. leftBorder.Position2D = leftRect.Center;
  324. leftBorder.IsStatic = true;
  325. leftBorder.DebugColor = Color.White;
  326. PhysicsBody topBorder =
  327. Physics.CreateRectangle(topRect.Width, topRect.Height, density);
  328. topBorder.Friction = friction;
  329. topBorder.Restitution = restitution;
  330. topBorder.Position2D = topRect.Center;
  331. topBorder.IsStatic = true;
  332. topBorder.DebugColor = Color.White;
  333. PhysicsBody rightBorder =
  334. Physics.CreateRectangle(rightRect.Width, rightRect.Height, density);
  335. rightBorder.Friction = friction;
  336. rightBorder.Restitution = restitution;
  337. rightBorder.Position2D = rightRect.Center;
  338. rightBorder.IsStatic = true;
  339. rightBorder.DebugColor = Color.White;
  340. PhysicsBody bottomBorder =
  341. Physics.CreateRectangle(bottomRect.Width, bottomRect.Height, density);
  342. bottomBorder.Friction = friction;
  343. bottomBorder.Restitution = restitution;
  344. bottomBorder.Position2D = bottomRect.Center;
  345. bottomBorder.IsStatic = true;
  346. // Create a ball;
  347. PhysicsBody ball = Physics.CreateCircle(0.035f, 1.0f);
  348. ball.Friction = 0.3f;
  349. ball.Restitution = 1.0f;
  350. ball.IsStatic = false;
  351. ball.Position2D = Point.Half;
  352. ball.DebugColor = Color.CornflowerBlue;
  353. // The Physics engine performs debug shape draw for us.
  354. Application.Start();
  355. }
  356. #endregion
  357. #region TestRotatingBall (Static)
  358. [Test]
  359. public static void TestRotatingBall()
  360. {
  361. const float BallRadius = 0.02f;
  362. const float BallMass = 1.0f;
  363. Point startPosition = new Point(0.1f, 0.5f);
  364. float setRotationVelocity = 180.0f;
  365. // Disable gravity
  366. Physics.Gravity = Vector.Zero;
  367. // Create ball
  368. var ball = Physics.CreateCircle(BallRadius, BallMass);
  369. ball.Position2D = startPosition;
  370. // No friction or restitution, fly ball, fly ;)
  371. ball.Friction = 0.0f;
  372. //not longer used: ball.Restitution = 0.0f;
  373. // Set the body type (the ball should fly, default is static)
  374. ball.IsStatic = false;
  375. Material2DColored ballMaterial = new Material2DColored(
  376. //"SmokeBrightTransparency");
  377. //"SmokeAdditive");
  378. "DeltaEngineLogo");
  379. Application.Start(delegate
  380. {
  381. // When we press the mouse or touch the screen, rotate!
  382. if (Input.Mouse.LeftButtonReleased ||
  383. Input.Touch.TouchReleased)
  384. {
  385. ball.AngularVelocity2D = setRotationVelocity;
  386. }
  387. // Draw the physics shape as a line circle
  388. //Circle.DrawOutline(ball.Position, ball.Radius, Color.Red);
  389. ballMaterial.Draw(Rectangle.FromCenter(ball.Position2D,
  390. ballMaterial.Size), ball.Rotation);
  391. Font.DrawTopLeftInformation(
  392. "AngularVelocity=" + ball.AngularVelocity2D +
  393. ", Rotation=" + ball.Rotation);
  394. });
  395. }
  396. #endregion
  397. #region TestBallMovement (Static)
  398. /// <summary>
  399. /// Test ball movement
  400. /// </summary>
  401. [Test]
  402. public static void TestBallMovement()
  403. {
  404. const float BallRadius = 0.02f;
  405. const float BallMass = 1.0f;
  406. Point startPosition = new Point(0.1f, 0.5f);
  407. Point setVelocity = new Point(50.0f, 0.0f);
  408. // Disable gravity
  409. Physics.Gravity = Vector.Zero;
  410. // Create ball
  411. var ball = Physics.CreateCircle(BallRadius, BallMass);
  412. ball.Position2D = startPosition;
  413. // No friction or restitution, fly ball, fly ;)
  414. ball.Friction = 0.0f;
  415. //not longer used: ball.Restitution = 0.0f;
  416. // Set the body type (the ball should fly, default is static)
  417. ball.IsStatic = false;
  418. Material2D ballMaterial = new Material2D(
  419. //"SmokeBrightTransparency");
  420. //"SmokeAdditive");
  421. "DeltaEngineLogo");
  422. //Second ball without physics
  423. Point ballPosition = startPosition + new Point(0, 0.2f);
  424. Point ballVelocity = Point.Zero;
  425. Application.Start(delegate
  426. {
  427. // When we press, give a velocity to the ball
  428. if (Input.Mouse.LeftButtonReleased ||
  429. Input.Touch.TouchReleased)
  430. {
  431. ball.LinearVelocity = new Vector(setVelocity, 0.0f);
  432. ballVelocity = setVelocity / 1000.0f;
  433. }
  434. else if (Input.Mouse.RightButtonReleased)
  435. {
  436. ball.Position2D = startPosition;
  437. ballPosition = startPosition + new Point(0, 0.2f);
  438. }
  439. else if (Input.Mouse.MiddleButtonReleased)
  440. {
  441. ball.Stop();
  442. }
  443. // Draw the physics shape as a line circle
  444. //Circle.DrawOutline(ball.Position, ball.Radius, Color.Red);
  445. ballMaterial.Draw(Rectangle.FromCenter(ball.Position2D,
  446. ballMaterial.Size));
  447. // And a second ball without physics
  448. ballMaterial.Draw(Rectangle.FromCenter(ballPosition,
  449. ballMaterial.Size));
  450. ballPosition += ballVelocity * Time.Delta;
  451. if (Input.Keyboard.ShiftIsPressed)
  452. {
  453. Font.DrawTopLeftInformation("ballPosition=" + ballPosition);
  454. }
  455. });
  456. }
  457. #endregion
  458. #region BugTest (Static)
  459. /// <summary>
  460. /// Test method for bug #3496 to check if 2 balls and a box collide properly
  461. /// </summary>
  462. [Test]
  463. public static void BugTest()
  464. {
  465. Physics.Gravity = new Vector(0.0f, 0.0981f, 0.0f);
  466. // Enable debug drawing
  467. Physics.DebugEnabled = true;
  468. // Create some (2 balls and 1 box) and the ground and let them
  469. // collide due to gravity
  470. List<PhysicsBody> balls = new List<PhysicsBody>();
  471. PhysicsBody box = null;
  472. PhysicsBody ground = null;
  473. float radius = 0.05f;
  474. float mass = 1.0f;
  475. Rectangle rectangle = new Rectangle(0.1f, 0.65f, 0.8f, 0.1f);
  476. Rectangle boxRect = new Rectangle(0.0f, 0.0f, 0.05f, 0.05f);
  477. for (int index = 0; index < 10; index++)
  478. {
  479. PhysicsBody ball = Physics.CreateCircle(radius, mass);
  480. ball.Position = new Vector(RandomHelper.RandomFloat(-0.5f, 0.5f),
  481. RandomHelper.RandomFloat(-0.5f, 0.5f), 0.0f);
  482. ball.Friction = 1.0f;
  483. ball.IsStatic = false;
  484. // Set debug color to red.
  485. ball.DebugColor = Color.Red;
  486. balls.Add(ball);
  487. }
  488. box = Physics.CreateRectangle(
  489. boxRect.Width, boxRect.Height, 1.0f);
  490. box.Position = new Vector(.5f, .4f, 0.0f);
  491. box.IsStatic = false;
  492. box.Friction = 1.0f;
  493. box.DebugColor = Color.Green;
  494. // set our ground
  495. ground = Physics.CreateRectangle(
  496. rectangle.Width, rectangle.Height, 1f);
  497. ground.Position = new Vector(rectangle.Center, 0.0f);
  498. ground.IsStatic = true;
  499. ground.Friction = 1.0f;
  500. ground.DebugColor = Color.Yellow;
  501. Application.Start();
  502. }
  503. #endregion
  504. #region TestRayCast (Static)
  505. /// <summary>
  506. /// Test ray cast
  507. /// </summary>
  508. [Test]
  509. public static void TestRayCast()
  510. {
  511. Physics.Gravity = new Vector(0.0f, 0.0981f, 0.0f);
  512. // create a plane so that every ray that doesnt hit an object will hit
  513. // the plane
  514. //const int size = 24;
  515. //const int height = -5;
  516. //// add a plane to the physics world
  517. //VisualPhysicsPlane visualPhysicsPlane = new VisualPhysicsPlane(size, size, new Vector(0, 0, height));
  518. //// create a box and a sphere
  519. //VisualPhysicsBox visualBox = new VisualPhysicsBox(new Vector(-2, 0, 0), 4);
  520. //VisualPhysicsSphere visualSphere = new VisualPhysicsSphere(new Vector(5, 0, 0), 2);
  521. //// this line of code also sets the camera, i.e. ViewProjection matrix
  522. //LookAtCamera cam = new LookAtCamera(new Vector(0, 25, 10));
  523. //// create a moving ray
  524. //Vector rayStart = new Vector(0, 0, 10);
  525. //Vector lookTarget = new Vector();
  526. //Ray testRay = new Ray(rayStart, lookTarget - rayStart);
  527. //Application.Start(delegate
  528. //{
  529. // IPhysicsShape3D catchedObject;
  530. // Vector intersection;
  531. // Vector normal;
  532. // //Draw the sphere and box
  533. // visualBox.Draw();
  534. // visualSphere.Draw();
  535. // // perform ray cast
  536. // bool rayCasted = PhysicsManager.RayCast(testRay,
  537. // out catchedObject, out intersection, out normal);
  538. // // update the objects color according to the result of the ray cast
  539. // if (catchedObject == visualBox.PhysicsBox)
  540. // visualBox.DebugDrawColor = Color.Red;
  541. // else
  542. // visualBox.DebugDrawColor = Color.Green;
  543. // if (catchedObject == visualSphere.PhysicsSphere)
  544. // visualSphere.DebugDrawColor = Color.Red;
  545. // else
  546. // visualSphere.DebugDrawColor = Color.Green;
  547. // //Draw the physics plane
  548. // visualPhysicsPlane.Draw();
  549. // // render the line according to the deleted intersection
  550. // if (rayCasted)
  551. // {
  552. // // render up to the intersection point
  553. // Line.Draw(testRay.Position, intersection, Color.Yellow);
  554. // // render the surface normal at this point
  555. // Line.Draw(intersection, intersection + normal, Color.White);
  556. // }
  557. // else
  558. // {
  559. // Line.Draw(testRay.Position,
  560. // testRay.Position + 15.0f * testRay.Direction, Color.Yellow);
  561. // }
  562. // // update the direction of the ray
  563. // lookTarget.X = 8 * (float)Math.Cos(0.3f * Time.CurrentExactTime); ;
  564. // lookTarget.Y = 2.0f * (float)Math.Sin(5.0f * Time.CurrentExactTime);
  565. // testRay.Direction = Vector.Normalize(lookTarget - testRay.Position);
  566. //});
  567. }
  568. #endregion
  569. #region TestCollisionEvents (Static)
  570. /// <summary>
  571. /// Performs collision check between two bodies with events.
  572. /// </summary>
  573. [Test]
  574. public static void TestCollisionEvents()
  575. {
  576. Physics.Gravity = new Vector(0.0f, 0.0981f, 0.0f);
  577. // Enable debug drawing
  578. Physics.DebugEnabled = true;
  579. // Create some (2 balls and 1 box) and the ground and let them
  580. // collide due to gravity
  581. List<PhysicsBody> balls = new List<PhysicsBody>();
  582. PhysicsBody box = null;
  583. PhysicsBody ground = null;
  584. float radius = 0.05f;
  585. float mass = 1.0f;
  586. Rectangle rectangle = new Rectangle(0.1f, 0.65f, 0.8f, 0.1f);
  587. Rectangle boxRect = new Rectangle(0.0f, 0.0f, 0.05f, 0.05f);
  588. for (int index = 0; index < 10; index++)
  589. {
  590. PhysicsBody ball = Physics.CreateCircle(radius, mass);
  591. ball.Position = new Vector(RandomHelper.RandomFloat(-0.5f, 0.5f),
  592. RandomHelper.RandomFloat(-0.5f, 0.5f), 0.0f);
  593. ball.Friction = 1.0f;
  594. ball.IsStatic = false;
  595. // Set debug color to red.
  596. ball.DebugColor = Color.Red;
  597. ball.Name = "Ball_" + index;
  598. balls.Add(ball);
  599. } // for
  600. box = Physics.CreateRectangle(
  601. boxRect.Width, boxRect.Height, 1.0f);
  602. box.Position = new Vector(.5f, .4f, 0.0f);
  603. box.IsStatic = false;
  604. box.Friction = 1.0f;
  605. box.DebugColor = Color.Green;
  606. box.Name = "Box";
  607. // set our ground
  608. ground = Physics.CreateRectangle(
  609. rectangle.Width, rectangle.Height, 1f);
  610. ground.Position = new Vector(rectangle.Center, 0.0f);
  611. ground.IsStatic = true;
  612. ground.Friction = 1.0f;
  613. ground.DebugColor = Color.Yellow;
  614. ground.Name = "PlaneBody";
  615. ground.CollisionBegin += ground_CollisionBegin;
  616. ground.CollisionEnd += ground_CollisionEnd;
  617. Application.Start();
  618. }
  619. #endregion
  620. #region Methods (Private)
  621. #region ground_CollisionBegin
  622. private static void ground_CollisionBegin(PhysicsBody other)
  623. {
  624. Log.Info(
  625. string.Format("Given body '{0}' ends collision with this plane body.",
  626. other.Name)
  627. );
  628. }
  629. #endregion
  630. #region ground_CollisionEnd
  631. private static void ground_CollisionEnd(PhysicsBody other)
  632. {
  633. Log.Info(
  634. string.Format("Given body '{0}' collide with this plane body.",
  635. other.Name)
  636. );
  637. }
  638. #endregion
  639. #endregion
  640. }
  641. }