{"id":584,"date":"2012-03-22T09:44:24","date_gmt":"2012-03-22T08:44:24","guid":{"rendered":"http:\/\/www.aymericlamboley.fr\/blog\/?p=584"},"modified":"2012-03-26T13:34:35","modified_gmt":"2012-03-26T12:34:35","slug":"from-box2d-to-chipmunk","status":"publish","type":"post","link":"http:\/\/www.aymericlamboley.fr\/blog\/from-box2d-to-chipmunk\/","title":{"rendered":"From Box2D to Chipmunk"},"content":{"rendered":"<p>Hey there, I&#8217;ve been very busy this last weeks working hard on my 2nd school year project, a mobile game. In a few weeks, I will explain it, but for now let&#8217;s do some programming stuff!<br \/>\nThe game is a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Side-scrolling_video_game\" target=\"_blank\">side-scrolling<\/a> 2D game. See <a href=\"http:\/\/adamatomic.com\/canabalt\/\" target=\"_blank\">Canabalt<\/a> or <a href=\"http:\/\/itunes.apple.com\/fr\/app\/jetpack-joyride\/id457446957?mt=8\" target=\"_blank\">Jetpack Joyride<\/a>, they are great games!<br \/>\nDeveloping for iOS, I used <a href=\"http:\/\/www.sparrow-framework.org\/\" target=\"_blank\">Sparrow<\/a> framework. It is awesome, and really easy to learn coming from AS3 &amp; <a href=\"http:\/\/starling-framework.org\/\" target=\"_blank\">Starling<\/a>.<\/p>\n<p>Why did I develop in Objective-C since AIR 3.2 is out ? Refer to my <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/my-thoughts-on-flash-and-its-recent-events\/\" target=\"_blank\">previous post<\/a> about my personal preference and the future of web development ; I decided to learn Objective-C and this project is really a good opportunity! I have followed the news about HTML5 and my opinion didn&#8217;t change&#8230; Moreover, for a long range project it is preferable to have the best workflow!<\/p>\n<p>Since I used the <a href=\"http:\/\/citrusengine.com\/\" target=\"_blank\">Citrus Engine<\/a>, I felt in love with physics engine and particularly Box2D. It is very useful &amp; powerful for game development, but it has some hard constraints. With this project, I wanted to try an other physics engine. My choice was Chipmunk.<\/p>\n<p>This post will <strong>not compare features<\/strong>, there are already a great post <a href=\"http:\/\/forums.tigsource.com\/index.php?topic=9318.0\" target=\"_blank\">there<\/a>, but <strong>how to move quickly<\/strong> from Box2D to Chipmunk. Thanks Scott Lembcke (Chipmunk&#8217;s author), for your <span style=\"color: #0000ff;\">clarifications<\/span>.<\/p>\n<p><strong>QUICK OVERVIEW :<\/strong><br \/>\n&#8211; <a href=\"http:\/\/box2d.org\" target=\"_blank\">Box2D<\/a> : <em>Box2D is an open source C++ engine for simulating rigid bodies in 2D. Box2D is developed by Erin Catto and has the zlib license. While the zlib license does not require acknowledgement, we encourage you to give credit to Box2D in your product.<\/em> <a href=\"http:\/\/www.box2d.org\/manual.html\" target=\"_blank\">The manual<\/a>.<br \/>\n&#8211; <a href=\"http:\/\/chipmunk-physics.net\/\" target=\"_blank\">Chipmunk<\/a> : <em>Chipmunk is a fast and lightweight 2D rigid body physics library written in C.<\/em> <a href=\"http:\/\/chipmunk-physics.net\/release\/ChipmunkLatest-Docs\/\" target=\"_blank\">The documentation<\/a>.<\/p>\n<p><strong>UNITS :<\/strong><br \/>\nBox2D uses meter\/kilogram\/second (MKS), Chipmunk uses pixel. There is no units for the mass, you defined the value you want, but stay logical between objects. The time is not clearly mentioned in Chipmunk, it doesn&#8217;t express in seconds but floats. <em><span style=\"color: #0000ff;\">Box2D uses real world units because it has a number of tuning threshold values, and the default values are set to be sane values for life-sized objects. Chipmunk\u2019s algorithms mostly avoid the need for tuning values so that you can use whatever arbitrary units makes most sense to you (pixels, meters, inches, etc). Likewise for time and mass.<\/span><\/em><\/p>\n<p><strong>SET UP WORLD :<\/strong><br \/>\nBox2D uses the term &#8220;world&#8221; whereas Chipmunk uses &#8220;space&#8221;. Both defined gravity &amp; iteration step. Body&#8217;s gravity is difficult to manage in Box2D and Chipmunk if you want your objects to have a different one. You may set up a gravity(0, 0) to your world\/space and manage the gravity into each object using a variable and updating its velocity.<\/p>\n<p><strong>REGISTRATION POINT :<\/strong><br \/>\nBoth have body&#8217;s center as registration point.<\/p>\n<p><strong>RIGID BODIES :<\/strong><br \/>\nBox2D uses two objects to define a body : body (user data, position, velocity, forces, torques, impulse&#8230;) &amp; bodyDef (body type definitions, and init values). Chipmunk uses only one object defining mass (which is automatically calculated in Box2D) and moment which represent inertia for the body.<br \/>\nBox2D has 3 body types : static, kinematic, dynamic ; Chipmunk kinematic bodies are named rogue.<\/p>\n<p><strong>FIXTURES :<\/strong><br \/>\nBox2D fixture\/fixtureDef defined shape, density, friction, restitution and filters. There is no fixture in Chipmunk. <em><span style=\"color: #0000ff;\">Restitution is the elasticity property on shapes. It doesn\u2019t store density on a per shape basis though. You have to calculate that into the mass manually..<\/span><\/em><\/p>\n<p><strong>SHAPES :<\/strong><br \/>\nIn Chipmunk, <em>you can attach as many shapes to a single body as you need to in order to define a complex shape. Shapes contain the surface properties of an object such as how much friction or elasticity it has<\/em>. It means than you can create a simple platform body and add all the shapes to it even if they are not close (a border bottom, a wall&#8230;).<\/p>\n<p><strong>COLLISIONS :<\/strong><br \/>\nWith Box2D you can know dynamically which is the other body you collide, in Chipmunk you may use this method too. You may also defined a collision handler&#8217;s function between the typeA and the typeB with function references defining collision start\/end &amp; pre\/post solve. In Box2D you managed collision thanks to the fixture, in Chipmunk you add the &#8220;listener&#8221; to the space.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey there, I&#8217;ve been very busy this last weeks working hard on my 2nd school year project, a mobile game. In a few weeks, I will explain it, but for now let&#8217;s do some programming stuff! The game is a side-scrolling 2D game. See Canabalt or Jetpack Joyride, they are great games! Developing for iOS, &hellip; <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/from-box2d-to-chipmunk\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">From Box2D to Chipmunk<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[115,116,114],"tags":[27,118,117],"_links":{"self":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/584"}],"collection":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/comments?post=584"}],"version-history":[{"count":8,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/584\/revisions"}],"predecessor-version":[{"id":600,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/584\/revisions\/600"}],"wp:attachment":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/media?parent=584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/categories?post=584"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/tags?post=584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}