32_2DConstraints.cs 28 KB

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