# slembcke/Chipmunk2D

Updating some outdated example code.

1 parent d5e953a commit f5e696c548ac72fd9215bd538053a16247bbf0e8 committed Oct 3, 2016
Showing with 81 additions and 81 deletions.
1. +64 −64 doc/examples/Hello Chipmunk.html
2. +17 −17 doc/examples/JointRecipies.html
 @@ -1,64 +1,64 @@ -
#include <stdio.h>
-#include <chipmunk.h>
-
-int main(void){
-  // cpVect is a 2D vector and cpv() is a shortcut for initializing them.
-  cpVect gravity = cpv(0, -100);
-
-  // Create an empty space.
-  cpSpace *space = cpSpaceNew();
-  cpSpaceSetGravity(space, gravity);
-
-  // Add a static line segment shape for the ground.
-  // We'll make it slightly tilted so the ball will roll off.
-  // We attach it to a static body to tell Chipmunk it shouldn't be movable.
-  cpShape *ground = cpSegmentShapeNew(cpSpaceGetStaticBody(space), cpv(-20, 5), cpv(20, -5), 0);
-  cpShapeSetFriction(ground, 1);
-
-  // Now let's make a ball that falls onto the line and rolls off.
-  // First we need to make a cpBody to hold the physical properties of the object.
-  // These include the mass, position, velocity, angle, etc. of the object.
-  // Then we attach collision shapes to the cpBody to give it a size and shape.
-
-  cpFloat mass = 1;
-
-  // The moment of inertia is like mass for rotation
-  cpFloat moment = cpMomentForCircle(mass, 0, radius, cpvzero);
-
-  // The cpSpaceAdd*() functions return the thing that you are adding.
-  // It's convenient to create and add an object in one line.
-  cpBody *ballBody = cpSpaceAddBody(space, cpBodyNew(mass, moment));
-  cpBodySetPos(ballBody, cpv(0, 15));
-
-  // Now we create the collision shape for the ball.
-  // You can create multiple collision shapes that point to the same body.
-  // They will all be attached to the body and move around to follow it.
-  cpShapeSetFriction(ballShape, 0.7);
-
-  // Now that it's all set up, we simulate all the objects in the space by
-  // stepping forward through time in small increments called steps.
-  // It is *highly* recommended to use a fixed size time step.
-  cpFloat timeStep = 1.0/60.0;
-  for(cpFloat time = 0; time < 2; time += timeStep){
-    cpVect pos = cpBodyGetPos(ballBody);
-    cpVect vel = cpBodyGetVel(ballBody);
-    printf(
-      "Time is %5.2f. ballBody is at (%5.2f, %5.2f). It's velocity is (%5.2f, %5.2f)\n",
-      time, pos.x, pos.y, vel.x, vel.y
-    );
-
-    cpSpaceStep(space, timeStep);
-  }
-
-  // Clean up our objects and exit!
-  cpShapeFree(ballShape);
-  cpBodyFree(ballBody);
-  cpShapeFree(ground);
-  cpSpaceFree(space);
-
-  return 0;
-}
+
#include <stdio.h>
+#include
<chipmunk.h>
+

+int main(void){
+  // cpVect is a 2D vector and cpv() is a shortcut for initializing them.
+
cpVect gravity = cpv(0, -100);
+
+  // Create an empty space.
+
cpSpace *space = cpSpaceNew();
+  cpSpaceSetGravity(space, gravity);
+
+  // Add a static line segment shape for the ground.
+
// We'll make it slightly tilted so the ball will roll off.
+
// We attach it to a static body to tell Chipmunk it shouldn't be movable.
+
cpShape *ground = cpSegmentShapeNew(cpSpaceGetStaticBody(space), cpv(-20, 5), cpv(20, -5), 0);
+  cpShapeSetFriction(ground, 1);
+
+  // Now let's make a ball that falls onto the line and rolls off.
+
// First we need to make a cpBody to hold the physical properties of the object.
+
// These include the mass, position, velocity, angle, etc. of the object.
+
// Then we attach collision shapes to the cpBody to give it a size and shape.
+

+  cpFloat mass = 1;
+
+  // The moment of inertia is like mass for rotation
+
+
cpFloat moment = cpMomentForCircle(mass, 0, radius, cpvzero);
+
+  // The cpSpaceAdd*() functions return the thing that you are adding.
+
// It's convenient to create and add an object in one line.
+
cpBody *ballBody = cpSpaceAddBody(space, cpBodyNew(mass, moment));
+  cpBodySetPos(ballBody, cpv(0, 15));
+
+  // Now we create the collision shape for the ball.
+
// You can create multiple collision shapes that point to the same body.
+
// They will all be attached to the body and move around to follow it.
+
+  cpShapeSetFriction(ballShape, 0.7);
+
+  // Now that it's all set up, we simulate all the objects in the space by
+
// stepping forward through time in small increments called steps.
+
// It is *highly* recommended to use a fixed size time step.
+
cpFloat timeStep = 1.0/60.0;
+  for(cpFloat time = 0; time < 2; time += timeStep){
+    cpVect pos = cpBodyGetPosition(ballBody);
+    cpVect vel = cpBodyGetVelocity(ballBody);
+    printf(
+      "Time is %5.2f. ballBody is at (%5.2f, %5.2f). It's velocity is (%5.2f, %5.2f)\n",
+      time, pos.x, pos.y, vel.x, vel.y
+    );
+
+    cpSpaceStep(space, timeStep);
+  }
+
+  // Clean up our objects and exit!
+
cpShapeFree(ballShape);
+  cpBodyFree(ballBody);
+  cpShapeFree(ground);
+  cpSpaceFree(space);
+
+  return 0;
+}
 @@ -1,17 +1,17 @@ -
// Faked top down friction.
-
-// A pivot joint configured this way will calculate friction against the ground for games with a top down perspective.
-// Because the joint correction is disabled, the joint will not recenter itself and only apply to the velocity.
-// The force the joint applies when changing the velocity will be clamped by the max force
-// and this causes it to work exactly like friction!
-cpConstraint *pivot = cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, body, cpvzero, cpvzero));
-pivot->maxBias = 0.0f; // disable joint correction
-pivot->maxForce = 1000.0f;
-
-// The pivot joint doesn't apply rotational forces, use a gear joint with a ratio of 1.0 for that.
-cpConstraint *gear = cpSpaceAddConstraint(space, cpGearJointNew(staticBody, body, 0.0f, 1.0f));
-gear->maxBias = 0.0f; // disable joint correction
-gear->maxForce = 5000.0f;
-
-// Also, instead of connecting the joints to a static body, you can connect them to an infinite mass rogue body.
-// You can then use the rogue body as a control body to the connected body. See the Tank demo as an example.
+
// Faked top down friction.
+

+// A pivot joint configured this way will calculate friction against the ground for games with a top down perspective.
+// Because the joint correction is disabled, the joint will not recenter itself and only apply to the velocity.
+// The force the joint applies when changing the velocity will be clamped by the max force
+// and this causes it to work exactly like friction!
+
cpConstraint *pivot = cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, body, cpvzero, cpvzero));
+cpConstraintSetMaxBias(pivot, 0.0f); // disable joint correction
+
cpConstraintSetMaxForce(pivot, 1000.0f);
+
+// The pivot joint doesn't apply rotational forces, use a gear joint with a ratio of 1.0 for that.
+
cpConstraint *gear = cpSpaceAddConstraint(space, cpGearJointNew(staticBody, body, 0.0f, 1.0f));
+cpConstraintSetMaxBias(gear, 0.0f); // disable joint correction
+
cpConstraintSetMaxForce(gear, 5000.0f);
+
+// Also, instead of connecting the joints to a static body, you can connect them to an infinite mass rogue body.
+// You can then use the rogue body as a control body to the connected body. See the Tank demo as an example.