1 /* 2 * Copyright (c) 2007-2013 Scott Lembcke and Howling Moon Software 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a copy 5 * of this software and associated documentation files (the "Software"), to deal 6 * in the Software without restriction, including without limitation the rights 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 * copies of the Software, and to permit persons to whom the Software is 9 * furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 * SOFTWARE. 21 */ 22 module demo.RollBall; 23 24 /** 25 Taken from the Chipmunk manual, the hello-world with a ball rolling down a slope. 26 */ 27 28 import demo.dchip; 29 30 import demo.ChipmunkDebugDraw; 31 import demo.ChipmunkDemo; 32 import demo.types; 33 34 void update(cpSpace* space, double dt) 35 { 36 cpSpaceStep(space, dt); 37 } 38 39 cpSpace* init() 40 { 41 // cpVect is a 2D vector and cpv() is a shortcut for initializing them. 42 cpVect gravity = cpv(0, -100); 43 44 // Create an empty space. 45 cpSpace *space = cpSpaceNew(); 46 cpSpaceSetGravity(space, gravity); 47 48 // Add a static line segment shape for the ground. 49 // We'll make it slightly tilted so the ball will roll off. 50 // We attach it to space.staticBody to tell Chipmunk it shouldn't be movable. 51 cpShape *ground = cpSegmentShapeNew(space.staticBody, cpv(-200, 0), cpv(200, -100), 0); 52 cpShapeSetFriction(ground, 1); 53 cpSpaceAddShape(space, ground); 54 55 // Now let's make a ball that falls onto the line and rolls off. 56 // First we need to make a cpBody to hold the physical properties of the object. 57 // These include the mass, position, velocity, angle, etc. of the object. 58 // Then we attach collision shapes to the cpBody to give it a size and shape. 59 60 cpFloat radius = 20; 61 cpFloat mass = 1; 62 63 // The moment of inertia is like mass for rotation 64 // Use the cpMomentFor*() functions to help you approximate it. 65 cpFloat moment = cpMomentForCircle(mass, 0, radius, cpvzero); 66 67 // The cpSpaceAdd*() functions return the thing that you are adding. 68 // It's convenient to create and add an object in one line. 69 cpBody *ballBody = cpSpaceAddBody(space, cpBodyNew(mass, moment)); 70 cpBodySetPos(ballBody, cpv(0, 15)); 71 72 // Now we create the collision shape for the ball. 73 // You can create multiple collision shapes that point to the same body. 74 // They will all be attached to the body and move around to follow it. 75 cpShape *ballShape = cpSpaceAddShape(space, cpCircleShapeNew(ballBody, radius, cpvzero)); 76 cpShapeSetFriction(ballShape, 0.7); 77 78 return space; 79 } 80 81 void destroy(cpSpace* space) 82 { 83 ChipmunkDemoFreeSpaceChildren(space); 84 cpSpaceFree(space); 85 } 86 87 ChipmunkDemo RollBall = { 88 "RollBall", 89 1.0 / 180.0, 90 &init, 91 &update, 92 &ChipmunkDemoDefaultDrawImpl, 93 &destroy, 94 };