32_2DConstraints.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. // Copyright (c) 2015 Xamarin Inc
  4. // Copyright (c) 2016 THUNDERBEAST GAMES LLC
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. using System;
  25. using AtomicEngine;
  26. namespace FeatureExamples
  27. {
  28. public class Constraints2DSample : Sample
  29. {
  30. Scene scene;
  31. bool drawDebug;
  32. Camera camera;
  33. RigidBody2D dummyBody;
  34. Node pickedNode;
  35. public Constraints2DSample() : base() { }
  36. public override void Start()
  37. {
  38. base.Start();
  39. CreateScene();
  40. SimpleCreateInstructionsWithWasd(", Use PageUp PageDown to zoom.\n Space to toggle debug geometry and joints - F5 to save the scene.");
  41. SetupViewport();
  42. SubscribeToEvents();
  43. }
  44. void SubscribeToEvents()
  45. {
  46. SubscribeToEvent<PostRenderUpdateEvent>(e =>
  47. {
  48. // If draw debug mode is enabled, draw viewport debug geometry, which will show eg. drawable bounding boxes and skeleton
  49. // bones. Note that debug geometry has to be separately requested each frame. Disable depth test so that we can see the
  50. // bones properly
  51. if (drawDebug)
  52. scene.GetComponent<PhysicsWorld2D>().DrawDebugGeometry();
  53. });
  54. SubscribeToEvent<MouseButtonDownEvent>(HandleMouseButtonDown);
  55. if (TouchEnabled)
  56. {
  57. SubscribeToEvent<TouchBeginEvent>(HandleTouchBegin3);
  58. }
  59. }
  60. protected override void Update(float timeStep)
  61. {
  62. SimpleMoveCamera2D(timeStep);
  63. var input = GetSubsystem<Input>();
  64. if (input.GetKeyDown(Constants.KEY_PAGEUP))
  65. camera.Zoom = (camera.Zoom * 1.01f);
  66. if (input.GetKeyDown(Constants.KEY_PAGEDOWN))
  67. camera.Zoom = (camera.Zoom * 0.99f);
  68. // Toggle physics debug geometry with space
  69. if (input.GetKeyPress(Constants.KEY_SPACE))
  70. drawDebug = !drawDebug;
  71. // Save scene
  72. if (input.GetKeyPress(Constants.KEY_F5))
  73. {
  74. // scene.SaveXml(FileSystem.ProgramDir + "Data/Scenes/Constraints.xml", "\t");
  75. }
  76. }
  77. void HandleTouchBegin3(TouchBeginEvent args)
  78. {
  79. var graphics = GetSubsystem<Graphics>();
  80. PhysicsWorld2D physicsWorld = scene.GetComponent<PhysicsWorld2D>();
  81. RigidBody2D rigidBody = physicsWorld.GetRigidBody(new Vector2(args.X, args.Y), uint.MaxValue); // Raycast for RigidBody2Ds to pick
  82. if (rigidBody != null)
  83. {
  84. pickedNode = rigidBody.Node;
  85. StaticSprite2D staticSprite = pickedNode.GetComponent<StaticSprite2D>();
  86. staticSprite.Color = (new Color(1.0f, 0.0f, 0.0f, 1.0f)); // Temporary modify color of the picked sprite
  87. rigidBody = pickedNode.GetComponent<RigidBody2D>();
  88. // Create a ConstraintMouse2D - Temporary apply this constraint to the pickedNode to allow grasping and moving with touch
  89. ConstraintMouse2D constraintMouse = pickedNode.CreateComponent<ConstraintMouse2D>();
  90. Vector3 pos = camera.ScreenToWorldPoint(new Vector3((float)args.X / graphics.Width, (float)args.Y / graphics.Height, 0.0f));
  91. constraintMouse.Target = new Vector2(pos.X, pos.Y);
  92. constraintMouse.MaxForce = 1000 * rigidBody.Mass;
  93. constraintMouse.CollideConnected = true;
  94. constraintMouse.OtherBody = dummyBody; // Use dummy body instead of rigidBody. It's better to create a dummy body automatically in ConstraintMouse2D
  95. constraintMouse.DampingRatio = 0;
  96. }
  97. SubscribeToEvent<TouchMoveEvent>(HandleTouchMove3);
  98. SubscribeToEvent<TouchEndEvent>(HandleTouchEnd3);
  99. }
  100. void HandleTouchEnd3(TouchEndEvent args)
  101. {
  102. if (pickedNode != null)
  103. {
  104. StaticSprite2D staticSprite = pickedNode.GetComponent<StaticSprite2D>();
  105. staticSprite.Color = (new Color(1.0f, 1.0f, 1.0f, 1.0f)); // Restore picked sprite color
  106. pickedNode.RemoveComponent<ConstraintMouse2D>(); // Remove temporary constraint
  107. pickedNode = null;
  108. }
  109. UnsubscribeFromEvent<TouchMoveEvent>();
  110. UnsubscribeFromEvent<TouchEndEvent>();
  111. }
  112. void HandleTouchMove3(TouchMoveEvent args)
  113. {
  114. if (pickedNode != null)
  115. {
  116. var graphics = GetSubsystem<Graphics>();
  117. ConstraintMouse2D constraintMouse = pickedNode.GetComponent<ConstraintMouse2D>();
  118. Vector3 pos = camera.ScreenToWorldPoint(new Vector3((float)args.X / graphics.Width, (float)args.Y / graphics.Height, 0.0f));
  119. constraintMouse.Target = new Vector2(pos.X, pos.Y);
  120. }
  121. }
  122. void HandleMouseButtonDown(MouseButtonDownEvent args)
  123. {
  124. Input input = GetSubsystem<Input>();
  125. PhysicsWorld2D physicsWorld = scene.GetComponent<PhysicsWorld2D>();
  126. RigidBody2D rigidBody = physicsWorld.GetRigidBody(input.MousePosition.X, input.MousePosition.Y, uint.MaxValue); // Raycast for RigidBody2Ds to pick
  127. if (rigidBody != null)
  128. {
  129. pickedNode = rigidBody.Node;
  130. //log.Info(pickedNode.name);
  131. StaticSprite2D staticSprite = pickedNode.GetComponent<StaticSprite2D>();
  132. staticSprite.Color = (new Color(1.0f, 0.0f, 0.0f, 1.0f)); // Temporary modify color of the picked sprite
  133. // Create a ConstraintMouse2D - Temporary apply this constraint to the pickedNode to allow grasping and moving with the mouse
  134. ConstraintMouse2D constraintMouse = pickedNode.CreateComponent<ConstraintMouse2D>();
  135. constraintMouse.Target = GetMousePositionXY();
  136. constraintMouse.MaxForce = 1000 * rigidBody.Mass;
  137. constraintMouse.CollideConnected = true;
  138. constraintMouse.OtherBody = dummyBody; // Use dummy body instead of rigidBody. It's better to create a dummy body automatically in ConstraintMouse2D
  139. constraintMouse.DampingRatio = 0.0f;
  140. }
  141. SubscribeToEvent<MouseMoveEvent>(HandleMouseMove);
  142. SubscribeToEvent<MouseButtonUpEvent>(HandleMouseButtonUp);
  143. }
  144. Vector2 GetMousePositionXY()
  145. {
  146. Input input = GetSubsystem<Input>();
  147. var graphics = GetSubsystem<Graphics>();
  148. Vector3 screenPoint = new Vector3((float)input.MousePosition.X / graphics.Width, (float)input.MousePosition.Y / graphics.Height, 0.0f);
  149. Vector3 worldPoint = camera.ScreenToWorldPoint(screenPoint);
  150. return new Vector2(worldPoint.X, worldPoint.Y);
  151. }
  152. void HandleMouseMove(MouseMoveEvent args)
  153. {
  154. if (pickedNode != null)
  155. {
  156. ConstraintMouse2D constraintMouse = pickedNode.GetComponent<ConstraintMouse2D>();
  157. constraintMouse.Target = GetMousePositionXY();
  158. }
  159. }
  160. void HandleMouseButtonUp(MouseButtonUpEvent args)
  161. {
  162. if (pickedNode != null)
  163. {
  164. StaticSprite2D staticSprite = pickedNode.GetComponent<StaticSprite2D>();
  165. staticSprite.Color = (new Color(1.0f, 1.0f, 1.0f, 1.0f)); // Restore picked sprite color
  166. pickedNode.RemoveComponent<ConstraintMouse2D>();
  167. pickedNode = null;
  168. }
  169. UnsubscribeFromEvent<MouseMoveEvent>();
  170. UnsubscribeFromEvent<MouseButtonUpEvent>();
  171. }
  172. void SetupViewport()
  173. {
  174. var renderer = GetSubsystem<Renderer>();
  175. renderer.SetViewport(0, new Viewport(scene, CameraNode.GetComponent<Camera>()));
  176. }
  177. void CreateScene()
  178. {
  179. scene = new Scene();
  180. scene.CreateComponent<Octree>();
  181. scene.CreateComponent<DebugRenderer>();
  182. PhysicsWorld2D physicsWorld = scene.CreateComponent<PhysicsWorld2D>(); // Create 2D physics world component
  183. physicsWorld.DrawJoint = true; // Display the joints (Note that DrawDebugGeometry() must be set to true to acually draw the joints)
  184. drawDebug = true; // Set DrawDebugGeometry() to true
  185. // Create camera
  186. CameraNode = scene.CreateChild("Camera");
  187. // Set camera's position
  188. CameraNode.Position = (new Vector3(0.0f, 0.0f, 0.0f)); // Note that Z setting is discarded; use camera.zoom instead (see MoveCamera() below for example)
  189. camera = CameraNode.CreateComponent<Camera>();
  190. camera.Orthographic = true;
  191. var graphics = GetSubsystem<Graphics>();
  192. camera.OrthoSize = (float)graphics.Height * PixelSize;
  193. camera.Zoom = 1.2f * Math.Min((float)graphics.Width / 1280.0f, (float)graphics.Height / 800.0f); // Set zoom according to user's resolution to ensure full visibility (initial zoom (1.2) is set for full visibility at 1280x800 resolution)
  194. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  195. Viewport viewport = new Viewport(scene, camera);
  196. Renderer renderer = GetSubsystem<Renderer>();
  197. renderer.SetViewport(0, viewport);
  198. Zone zone = renderer.DefaultZone;
  199. zone.FogColor = (new Color(0.1f, 0.1f, 0.1f)); // Set background color for the scene
  200. // Create 4x3 grid
  201. for (uint i = 0; i < 5; ++i)
  202. {
  203. Node edgeNode = scene.CreateChild("VerticalEdge");
  204. RigidBody2D edgeBody = edgeNode.CreateComponent<RigidBody2D>();
  205. if (dummyBody == null)
  206. dummyBody = edgeBody; // Mark first edge as dummy body (used by mouse pick)
  207. CollisionEdge2D edgeShape = edgeNode.CreateComponent<CollisionEdge2D>();
  208. edgeShape.SetVertices(new Vector2(i * 2.5f - 5.0f, -3.0f), new Vector2(i * 2.5f - 5.0f, 3.0f));
  209. edgeShape.Friction = 0.5f; // Set friction
  210. }
  211. for (uint j = 0; j < 4; ++j)
  212. {
  213. Node edgeNode = scene.CreateChild("HorizontalEdge");
  214. /*RigidBody2D edgeBody = */
  215. edgeNode.CreateComponent<RigidBody2D>();
  216. CollisionEdge2D edgeShape = edgeNode.CreateComponent<CollisionEdge2D>();
  217. edgeShape.SetVertices(new Vector2(-5.0f, j * 2.0f - 3.0f), new Vector2(5.0f, j * 2.0f - 3.0f));
  218. edgeShape.Friction = 0.5f; // Set friction
  219. }
  220. var cache = GetSubsystem<ResourceCache>();
  221. // Create a box (will be cloned later)
  222. Node box = scene.CreateChild("Box");
  223. box.Position = (new Vector3(0.8f, -2.0f, 0.0f));
  224. StaticSprite2D boxSprite = box.CreateComponent<StaticSprite2D>();
  225. boxSprite.Sprite = cache.Get<Sprite2D>("Urho2D/Box.png");
  226. RigidBody2D boxBody = box.CreateComponent<RigidBody2D>();
  227. boxBody.BodyType = BodyType2D.BT_DYNAMIC;
  228. boxBody.LinearDamping = 0.0f;
  229. boxBody.AngularDamping = 0.0f;
  230. CollisionBox2D shape = box.CreateComponent<CollisionBox2D>(); // Create box shape
  231. shape.Size = new Vector2(0.32f, 0.32f); // Set size
  232. shape.Density = 1.0f; // Set shape density (kilograms per meter squared)
  233. shape.Friction = 0.5f; // Set friction
  234. shape.Restitution = 0.1f; // Set restitution (slight bounce)
  235. // Create a ball (will be cloned later)
  236. Node ball = scene.CreateChild("Ball");
  237. ball.Position = (new Vector3(1.8f, -2.0f, 0.0f));
  238. StaticSprite2D ballSprite = ball.CreateComponent<StaticSprite2D>();
  239. ballSprite.Sprite = cache.Get<Sprite2D>("Urho2D/Ball.png");
  240. RigidBody2D ballBody = ball.CreateComponent<RigidBody2D>();
  241. ballBody.BodyType = BodyType2D.BT_DYNAMIC;
  242. ballBody.LinearDamping = 0.0f;
  243. ballBody.AngularDamping = 0.0f;
  244. CollisionCircle2D ballShape = ball.CreateComponent<CollisionCircle2D>(); // Create circle shape
  245. ballShape.Radius = 0.16f; // Set radius
  246. ballShape.Density = 1.0f; // Set shape density (kilograms per meter squared)
  247. ballShape.Friction = 0.5f; // Set friction
  248. ballShape.Restitution = 0.6f; // Set restitution: make it bounce
  249. // Create a polygon
  250. Node polygon = scene.CreateChild("Polygon");
  251. polygon.Position = (new Vector3(1.6f, -2.0f, 0.0f));
  252. polygon.SetScale(0.7f);
  253. StaticSprite2D polygonSprite = polygon.CreateComponent<StaticSprite2D>();
  254. polygonSprite.Sprite = cache.Get<Sprite2D>("Urho2D/Aster.png");
  255. RigidBody2D polygonBody = polygon.CreateComponent<RigidBody2D>();
  256. polygonBody.BodyType = BodyType2D.BT_DYNAMIC;
  257. CollisionPolygon2D polygonShape = polygon.CreateComponent<CollisionPolygon2D>();
  258. polygonShape.VertexCount = 6; // Set number of vertices (mandatory when using SetVertex())
  259. polygonShape.SetVertex(0, new Vector2(-0.8f, -0.3f));
  260. polygonShape.SetVertex(1, new Vector2(0.5f, -0.8f));
  261. polygonShape.SetVertex(2, new Vector2(0.8f, -0.3f));
  262. polygonShape.SetVertex(3, new Vector2(0.8f, 0.5f));
  263. polygonShape.SetVertex(4, new Vector2(0.5f, 0.9f));
  264. polygonShape.SetVertex(5, new Vector2(-0.5f, 0.7f));
  265. polygonShape.Density = 1.0f; // Set shape density (kilograms per meter squared)
  266. polygonShape.Friction = 0.3f; // Set friction
  267. polygonShape.Restitution = 0.0f; // Set restitution (no bounce)
  268. // Create a ConstraintDistance2D
  269. CreateFlag("ConstraintDistance2D", -4.97f, 3.0f); // Display Text3D flag
  270. Node boxDistanceNode = box.Clone(CreateMode.REPLICATED);
  271. Node ballDistanceNode = ball.Clone(CreateMode.REPLICATED);
  272. RigidBody2D ballDistanceBody = ballDistanceNode.GetComponent<RigidBody2D>();
  273. boxDistanceNode.Position = (new Vector3(-4.5f, 2.0f, 0.0f));
  274. ballDistanceNode.Position = (new Vector3(-3.0f, 2.0f, 0.0f));
  275. ConstraintDistance2D constraintDistance = boxDistanceNode.CreateComponent<ConstraintDistance2D>(); // Apply ConstraintDistance2D to box
  276. constraintDistance.OtherBody = ballDistanceBody; // Constrain ball to box
  277. constraintDistance.OwnerBodyAnchor = boxDistanceNode.Position2D;
  278. constraintDistance.OtherBodyAnchor = ballDistanceNode.Position2D;
  279. // Make the constraint soft (comment to make it rigid, which is its basic behavior)
  280. constraintDistance.FrequencyHz = 4.0f;
  281. constraintDistance.DampingRatio = 0.5f;
  282. // Create a ConstraintFriction2D ********** Not functional. From Box2d samples it seems that 2 anchors are required, Urho2D only provides 1, needs investigation ***********
  283. CreateFlag("ConstraintFriction2D", 0.03f, 1.0f); // Display Text3D flag
  284. Node boxFrictionNode = box.Clone(CreateMode.REPLICATED);
  285. Node ballFrictionNode = ball.Clone(CreateMode.REPLICATED);
  286. boxFrictionNode.Position = (new Vector3(0.5f, 0.0f, 0.0f));
  287. ballFrictionNode.Position = (new Vector3(1.5f, 0.0f, 0.0f));
  288. ConstraintFriction2D constraintFriction = boxFrictionNode.CreateComponent<ConstraintFriction2D>(); // Apply ConstraintDistance2D to box
  289. constraintFriction.OtherBody = ballFrictionNode.GetComponent<RigidBody2D>(); // Constraint ball to box
  290. // Create a ConstraintGear2D
  291. CreateFlag("ConstraintGear2D", -4.97f, -1.0f); // Display Text3D flag
  292. Node baseNode = box.Clone(CreateMode.REPLICATED);
  293. RigidBody2D tempBody = baseNode.GetComponent<RigidBody2D>(); // Get body to make it static
  294. tempBody.BodyType = BodyType2D.BT_STATIC;
  295. baseNode.Position = (new Vector3(-3.7f, -2.5f, 0.0f));
  296. Node ball1Node = ball.Clone(CreateMode.REPLICATED);
  297. ball1Node.Position = (new Vector3(-4.5f, -2.0f, 0.0f));
  298. RigidBody2D ball1Body = ball1Node.GetComponent<RigidBody2D>();
  299. Node ball2Node = ball.Clone(CreateMode.REPLICATED);
  300. ball2Node.Position = (new Vector3(-3.0f, -2.0f, 0.0f));
  301. RigidBody2D ball2Body = ball2Node.GetComponent<RigidBody2D>();
  302. ConstraintRevolute2D gear1 = baseNode.CreateComponent<ConstraintRevolute2D>(); // Apply constraint to baseBox
  303. gear1.OtherBody = ball1Body; // Constrain ball1 to baseBox
  304. gear1.Anchor = ball1Node.Position2D;
  305. ConstraintRevolute2D gear2 = baseNode.CreateComponent<ConstraintRevolute2D>(); // Apply constraint to baseBox
  306. gear2.OtherBody = ball2Body; // Constrain ball2 to baseBox
  307. gear2.Anchor = ball2Node.Position2D;
  308. ConstraintGear2D constraintGear = ball1Node.CreateComponent<ConstraintGear2D>(); // Apply constraint to ball1
  309. constraintGear.OtherBody = ball2Body; // Constrain ball2 to ball1
  310. constraintGear.OwnerConstraint = gear1;
  311. constraintGear.OtherConstraint = gear2;
  312. constraintGear.Ratio = 1.0f;
  313. ball1Body.ApplyAngularImpulse(0.015f, true); // Animate
  314. // Create a vehicle from a compound of 2 ConstraintWheel2Ds
  315. CreateFlag("ConstraintWheel2Ds compound", -2.45f, -1.0f); // Display Text3D flag
  316. Node car = box.Clone(CreateMode.REPLICATED);
  317. car.Scale = new Vector3(4.0f, 1.0f, 0.0f);
  318. car.Position = (new Vector3(-1.2f, -2.3f, 0.0f));
  319. StaticSprite2D tempSprite = car.GetComponent<StaticSprite2D>(); // Get car Sprite in order to draw it on top
  320. tempSprite.OrderInLayer = 0; // Draw car on top of the wheels (set to -1 to draw below)
  321. Node ball1WheelNode = ball.Clone(CreateMode.REPLICATED);
  322. ball1WheelNode.Position = (new Vector3(-1.6f, -2.5f, 0.0f));
  323. Node ball2WheelNode = ball.Clone(CreateMode.REPLICATED);
  324. ball2WheelNode.Position = (new Vector3(-0.8f, -2.5f, 0.0f));
  325. ConstraintWheel2D wheel1 = car.CreateComponent<ConstraintWheel2D>();
  326. wheel1.OtherBody = ball1WheelNode.GetComponent<RigidBody2D>();
  327. wheel1.Anchor = ball1WheelNode.Position2D;
  328. wheel1.Axis = new Vector2(0.0f, 1.0f);
  329. wheel1.MaxMotorTorque = 20.0f;
  330. wheel1.FrequencyHz = 4.0f;
  331. wheel1.DampingRatio = 0.4f;
  332. ConstraintWheel2D wheel2 = car.CreateComponent<ConstraintWheel2D>();
  333. wheel2.OtherBody = ball2WheelNode.GetComponent<RigidBody2D>();
  334. wheel2.Anchor = ball2WheelNode.Position2D;
  335. wheel2.Axis = new Vector2(0.0f, 1.0f);
  336. wheel2.MaxMotorTorque = 10.0f;
  337. wheel2.FrequencyHz = 4.0f;
  338. wheel2.DampingRatio = 0.4f;
  339. // ConstraintMotor2D
  340. CreateFlag("ConstraintMotor2D", 2.53f, -1.0f); // Display Text3D flag
  341. Node boxMotorNode = box.Clone(CreateMode.REPLICATED);
  342. tempBody = boxMotorNode.GetComponent<RigidBody2D>(); // Get body to make it static
  343. tempBody.BodyType = BodyType2D.BT_STATIC;
  344. Node ballMotorNode = ball.Clone(CreateMode.REPLICATED);
  345. boxMotorNode.Position = (new Vector3(3.8f, -2.1f, 0.0f));
  346. ballMotorNode.Position = (new Vector3(3.8f, -1.5f, 0.0f));
  347. ConstraintMotor2D constraintMotor = boxMotorNode.CreateComponent<ConstraintMotor2D>();
  348. constraintMotor.OtherBody = ballMotorNode.GetComponent<RigidBody2D>(); // Constrain ball to box
  349. constraintMotor.LinearOffset = new Vector2(0.0f, 0.8f); // Set ballNode position relative to boxNode position = (0,0)
  350. constraintMotor.AngularOffset = 0.1f;
  351. constraintMotor.MaxForce = 5.0f;
  352. constraintMotor.MaxTorque = 10.0f;
  353. constraintMotor.CorrectionFactor = 1.0f;
  354. constraintMotor.CollideConnected = true; // doesn't work
  355. // ConstraintMouse2D is demonstrated in HandleMouseButtonDown() function. It is used to "grasp" the sprites with the mouse.
  356. CreateFlag("ConstraintMouse2D", 0.03f, -1.0f); // Display Text3D flag
  357. // Create a ConstraintPrismatic2D
  358. CreateFlag("ConstraintPrismatic2D", 2.53f, 3.0f); // Display Text3D flag
  359. Node boxPrismaticNode = box.Clone(CreateMode.REPLICATED);
  360. tempBody = boxPrismaticNode.GetComponent<RigidBody2D>(); // Get body to make it static
  361. tempBody.BodyType = BodyType2D.BT_STATIC;
  362. Node ballPrismaticNode = ball.Clone(CreateMode.REPLICATED);
  363. boxPrismaticNode.Position = new Vector3(3.3f, 2.5f, 0.0f);
  364. ballPrismaticNode.Position = new Vector3(4.3f, 2.0f, 0.0f);
  365. ConstraintPrismatic2D constraintPrismatic = boxPrismaticNode.CreateComponent<ConstraintPrismatic2D>();
  366. constraintPrismatic.OtherBody = ballPrismaticNode.GetComponent<RigidBody2D>(); // Constrain ball to box
  367. constraintPrismatic.Axis = new Vector2(1.0f, 1.0f); // Slide from [0,0] to [1,1]
  368. constraintPrismatic.Anchor = new Vector2(4.0f, 2.0f);
  369. constraintPrismatic.LowerTranslation = -1.0f;
  370. constraintPrismatic.UpperTranslation = 0.5f;
  371. constraintPrismatic.EnableLimit = true;
  372. constraintPrismatic.MaxMotorForce = 1.0f;
  373. constraintPrismatic.MotorSpeed = 0.0f;
  374. // ConstraintPulley2D
  375. CreateFlag("ConstraintPulley2D", 0.03f, 3.0f); // Display Text3D flag
  376. Node boxPulleyNode = box.Clone(CreateMode.REPLICATED);
  377. Node ballPulleyNode = ball.Clone(CreateMode.REPLICATED);
  378. boxPulleyNode.Position = (new Vector3(0.5f, 2.0f, 0.0f));
  379. ballPulleyNode.Position = (new Vector3(2.0f, 2.0f, 0.0f));
  380. ConstraintPulley2D constraintPulley = boxPulleyNode.CreateComponent<ConstraintPulley2D>(); // Apply constraint to box
  381. constraintPulley.OtherBody = ballPulleyNode.GetComponent<RigidBody2D>(); // Constrain ball to box
  382. constraintPulley.OwnerBodyAnchor = boxPulleyNode.Position2D;
  383. constraintPulley.OtherBodyAnchor = ballPulleyNode.Position2D;
  384. constraintPulley.OwnerBodyGroundAnchor = boxPulleyNode.Position2D + new Vector2(0.0f, 1.0f);
  385. constraintPulley.OtherBodyGroundAnchor = ballPulleyNode.Position2D + new Vector2(0.0f, 1.0f);
  386. constraintPulley.Ratio = 1.0f; // Weight ratio between ownerBody and otherBody
  387. // Create a ConstraintRevolute2D
  388. CreateFlag("ConstraintRevolute2D", -2.45f, 3.0f); // Display Text3D flag
  389. Node boxRevoluteNode = box.Clone(CreateMode.REPLICATED);
  390. tempBody = boxRevoluteNode.GetComponent<RigidBody2D>(); // Get body to make it static
  391. tempBody.BodyType = BodyType2D.BT_STATIC;
  392. Node ballRevoluteNode = ball.Clone(CreateMode.REPLICATED);
  393. boxRevoluteNode.Position = (new Vector3(-2.0f, 1.5f, 0.0f));
  394. ballRevoluteNode.Position = (new Vector3(-1.0f, 2.0f, 0.0f));
  395. ConstraintRevolute2D constraintRevolute = boxRevoluteNode.CreateComponent<ConstraintRevolute2D>(); // Apply constraint to box
  396. constraintRevolute.OtherBody = ballRevoluteNode.GetComponent<RigidBody2D>(); // Constrain ball to box
  397. constraintRevolute.Anchor = new Vector2(-1.0f, 1.5f);
  398. constraintRevolute.LowerAngle = -1.0f; // In radians
  399. constraintRevolute.UpperAngle = 0.5f; // In radians
  400. constraintRevolute.EnableLimit = true;
  401. constraintRevolute.MaxMotorTorque = 10.0f;
  402. constraintRevolute.MotorSpeed = 0.0f;
  403. constraintRevolute.EnableMotor = true;
  404. // Create a ConstraintRope2D
  405. CreateFlag("ConstraintRope2D", -4.97f, 1.0f); // Display Text3D flag
  406. Node boxRopeNode = box.Clone(CreateMode.REPLICATED);
  407. tempBody = boxRopeNode.GetComponent<RigidBody2D>();
  408. tempBody.BodyType = BodyType2D.BT_STATIC;
  409. Node ballRopeNode = ball.Clone(CreateMode.REPLICATED);
  410. boxRopeNode.Position = (new Vector3(-3.7f, 0.7f, 0.0f));
  411. ballRopeNode.Position = (new Vector3(-4.5f, 0.0f, 0.0f));
  412. ConstraintRope2D constraintRope = boxRopeNode.CreateComponent<ConstraintRope2D>();
  413. constraintRope.OtherBody = ballRopeNode.GetComponent<RigidBody2D>(); // Constrain ball to box
  414. constraintRope.OwnerBodyAnchor = new Vector2(0.0f, -0.5f); // Offset from box (OwnerBody) : the rope is rigid from OwnerBody center to this ownerBodyAnchor
  415. constraintRope.MaxLength = 0.9f; // Rope length
  416. constraintRope.CollideConnected = true;
  417. // Create a ConstraintWeld2D
  418. CreateFlag("ConstraintWeld2D", -2.45f, 1.0f); // Display Text3D flag
  419. Node boxWeldNode = box.Clone(CreateMode.REPLICATED);
  420. Node ballWeldNode = ball.Clone(CreateMode.REPLICATED);
  421. boxWeldNode.Position = (new Vector3(-0.5f, 0.0f, 0.0f));
  422. ballWeldNode.Position = (new Vector3(-2.0f, 0.0f, 0.0f));
  423. ConstraintWeld2D constraintWeld = boxWeldNode.CreateComponent<ConstraintWeld2D>();
  424. constraintWeld.OtherBody = ballWeldNode.GetComponent<RigidBody2D>(); // Constrain ball to box
  425. constraintWeld.Anchor = boxWeldNode.Position2D;
  426. constraintWeld.FrequencyHz = 4.0f;
  427. constraintWeld.DampingRatio = 0.5f;
  428. // Create a ConstraintWheel2D
  429. CreateFlag("ConstraintWheel2D", 2.53f, 1.0f); // Display Text3D flag
  430. Node boxWheelNode = box.Clone(CreateMode.REPLICATED);
  431. Node ballWheelNode = ball.Clone(CreateMode.REPLICATED);
  432. boxWheelNode.Position = (new Vector3(3.8f, 0.0f, 0.0f));
  433. ballWheelNode.Position = (new Vector3(3.8f, 0.9f, 0.0f));
  434. ConstraintWheel2D constraintWheel = boxWheelNode.CreateComponent<ConstraintWheel2D>();
  435. constraintWheel.OtherBody = ballWheelNode.GetComponent<RigidBody2D>(); // Constrain ball to box
  436. constraintWheel.Anchor = ballWheelNode.Position2D;
  437. constraintWheel.Axis = new Vector2(0.0f, 1.0f);
  438. constraintWheel.EnableMotor = true;
  439. constraintWheel.MaxMotorTorque = 1.0f;
  440. constraintWheel.MotorSpeed = 0.0f;
  441. constraintWheel.FrequencyHz = 4.0f;
  442. constraintWheel.DampingRatio = 0.5f;
  443. constraintWheel.CollideConnected = true; // doesn't work
  444. }
  445. void CreateFlag(string text, float x, float y) // Used to create Tex3D flags
  446. {
  447. /*
  448. Node flagNode = scene.CreateChild("Flag");
  449. flagNode.Position = (new Vector3(x, y, 0.0f));
  450. Text3D flag3D = flagNode.CreateComponent<Text3D>(); // We use Text3D in order to make the text affected by zoom (so that it sticks to 2D)
  451. flag3D.Text = text;
  452. var cache = ResourceCache;
  453. flag3D.SetFont(cache.GetFont("Fonts/Anonymous Pro.ttf"), 15);
  454. */
  455. }
  456. }
  457. }