Is there a tutorial for Chipmunk?

Official forum for the Chipmunk2D Physics Library.
Post Reply
User avatar
gcsaba2
Posts: 4
Joined: Sun Dec 20, 2009 2:51 pm
Contact:

Is there a tutorial for Chipmunk?

Post by gcsaba2 »

I've just downloaded Chipmunk and thought I'd give it a try. After failing to run the demo on VC++ Express (although I see now there's a separate thread here, so maybe I'll read that), I started reading the code and it's a bit complicated to start with... Is there some simple tutorial, like how to write an application that has, for example, two rectangles, and that's it?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Is there a tutorial for Chipmunk?

Post by slembcke »

My intention is to make the Demo code into a nice tutorial eventually. Simple.c is simple, well commented and easy to follow. You should start there probably.

http://code.google.com/p/chipmunk-physi ... o/Simple.c
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
gcsaba2
Posts: 4
Joined: Sun Dec 20, 2009 2:51 pm
Contact:

Re: Is there a tutorial for Chipmunk?

Post by gcsaba2 »

OK I've managed to compile and run the demos on Ubuntu, so I can see that this actually does work :)
However, it's written in some obscure version of C?

Does it have to be compiled with cmake? Is there some elegant way to compiling my application with make?

I've tried copying the .h files in src and src/constraints to /usr/include, and the .a and .so files to /usr/lib... and then I just included <chipmunk.h> into my application and still get warnings and errors.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Is there a tutorial for Chipmunk?

Post by slembcke »

I actually didn't make the cmake file, but it works simply enough. It's not like CMake is very big, any reason to not install it?

Obscure version of C? Most of the code is C99 with a few compatibility bits so it can be compiled as C++ under MSVC. If you are trying to compile it yourself using GCC, you'll need to add -std=gnu99 -ffast-math and -O3.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
gcsaba2
Posts: 4
Joined: Sun Dec 20, 2009 2:51 pm
Contact:

Re: Is there a tutorial for Chipmunk?

Post by gcsaba2 »

OK thank you for your quick reply. I would really like to learn how to use this library cause from the demos it looks really cool. So if you would help me out writing my first example application, I'll write a tutorial from the experience and it'll probably be helpful to other people.

So let me explain what I have done so far. My question will be at the bottom.

I'm using Ubuntu 9.10 and would like to write my code in C++. I would use the g++ compiler.

I downloaded the latest source code, ran cmake . and then make, which generated the binary files. I copied libchipmunk.a and libchipmunk.so.4 into /usr/lib. I also made a symbolink link /usr/include/chipmunk, which points to the src directory.

I wrote a Makefile which compiles my application. It's very simple:

Code: Select all

CC = g++
CCFLAGS = -I/usr/include -I/usr/include/chipmunk
FILES = main.cc
LIBS = -lglut -lchipmunk -g -Wall

compile:
	$(CC) $(CCFLAGS) $(FILES) $(LIBS) -o hello

clean:
	@rm -f *.o
	@rm -f hello
The only source file I have is main.cc. At first, I wrote the simplest OpenGL application, which just draws two rectangles. This code compiles successfully, which means the libraries are ok.

Code: Select all

#include <iostream>
#include <GL/glut.h>
#include <chipmunk.h>

using namespace std;

#define window_width  640
#define window_height 480

// Main loop
void main_loop_function() {
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glLoadIdentity();
   glTranslatef(0, 0, -10);
   
   glTranslatef(0, 2, 0);
   
   // draw a red and a blue circle

   glBegin(GL_QUADS);
   glColor3ub(255, 0, 0); glVertex2f(-1,  1);
   glColor3ub(255, 0, 0); glVertex2f( 1,  1);
   glColor3ub(255, 0, 0); glVertex2f( 1, -1);
   glColor3ub(255, 0, 0); glVertex2f(-1, -1);
   glEnd();

   glTranslatef(0, -3, 0);
   glBegin(GL_QUADS);
   glColor3ub(0, 0, 255); glVertex2f(-1,  1);
   glColor3ub(0, 0, 255); glVertex2f( 1,  1);
   glColor3ub(0, 0, 255); glVertex2f( 1, -1);
   glColor3ub(0, 0, 255); glVertex2f(-1, -1);
   glEnd();
   
	glutSwapBuffers();
}

// Initialze OpenGL 
void GL_Setup(int width, int height) {
	glViewport( 0, 0, width, height );
	glMatrixMode( GL_PROJECTION );
	glEnable( GL_DEPTH_TEST );
	gluPerspective( 45, (float)width/height, .1, 100 );
	glMatrixMode( GL_MODELVIEW );
  glClearColor(1.0f, 1.0f, 1.0f, 1.0f);  // background color
}

int main(int argc, char** argv) {
  cout << "Hello world" << endl;

  glutInit(&argc, argv);
  glutInitWindowSize(window_width, window_height);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  glutCreateWindow("Hello world");
  glutIdleFunc(main_loop_function);
  GL_Setup(window_width, window_height);

  glutMainLoop();

  return 0;
}
What I'm trying to do is have the two rectangles collide, ideally by having them both fall down (some kind of gravity), and then one falls on another.

So first I add the cpInitChipmunk(); line to main().

Then I add space and two static bodies. I also need to add an infinite static body, although I'm not sure why.

Code: Select all

cpSpace *space;
cpBody *rect1, *rect2;
cpBody *staticBody;
Now I create a new function where I separate the code which will handle the definition of the space, gravity etc. This function will be called right before the glutMainLoop().

Code: Select all

void initObjects() {
  staticBody = cpBodyNew(INFINITY, INFINITY);
  space = cpSpaceNew();
  space->iterations = 10;
  cpSpaceResizeStaticHash(space, 30.0f, 1000);
  cpSpaceResizeActiveHash(space, 30.0f, 1000);
  space->gravity = cpv(0, -100);
  cpShape *ground = cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f);
  ground->e = 1.0f; ground->u = 1.0f;
  cpSpaceAddStaticShape(space, ground);
}
The code compiles, but it doesn't do anything yet. I guess now I need to define the two rectangles.

Code: Select all

  cpFloat mass = 10.0f;
  cpFloat moment = 1.0f;
  rect1 = cpBodyNew(mass, moment);
  rect2 = cpBodyNew(mass, moment);
  
  // position
  rect1->p = cpv(2, 0);
  cpSpaceAddBody(space, rect1);
  rect2->p = cpv(5, 0);
  cpSpaceAddBody(space, rect2);
OK so this is done... although I'm not sure how to make the position of rect1 and rect2 to be the same as the rectangles I've defined earlier in opengl...
How am I supposed to make this thing animate now?

I'm also attaching the whole code to this post.
User avatar
gcsaba2
Posts: 4
Joined: Sun Dec 20, 2009 2:51 pm
Contact:

Re: Is there a tutorial for Chipmunk?

Post by gcsaba2 »

Hmm I probably can't attach files... we'll here it is copy-pasted then:

Code: Select all

#include <iostream>
#include <GL/glut.h>
#include <chipmunk.h>

using namespace std;

#define window_width  640
#define window_height 480

cpSpace *space;
cpBody *rect1, *rect2;
cpBody *staticBody;

// Main loop
void main_loop_function() {
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glLoadIdentity();
   glTranslatef(0, 0, -10);
   
   glTranslatef(0, 2, 0);
   
   // draw a red and a blue circle

   glBegin(GL_QUADS);
   glColor3ub(255, 0, 0); glVertex2f(-1,  1);
   glColor3ub(255, 0, 0); glVertex2f( 1,  1);
   glColor3ub(255, 0, 0); glVertex2f( 1, -1);
   glColor3ub(255, 0, 0); glVertex2f(-1, -1);
   glEnd();

   glTranslatef(0, -3, 0);
   glBegin(GL_QUADS);
   glColor3ub(0, 0, 255); glVertex2f(-1,  1);
   glColor3ub(0, 0, 255); glVertex2f( 1,  1);
   glColor3ub(0, 0, 255); glVertex2f( 1, -1);
   glColor3ub(0, 0, 255); glVertex2f(-1, -1);
   glEnd();
   
	glutSwapBuffers();
}

// Initialze OpenGL 
void GL_Setup(int width, int height) {
	glViewport( 0, 0, width, height );
	glMatrixMode( GL_PROJECTION );
	glEnable( GL_DEPTH_TEST );
	gluPerspective( 45, (float)width/height, .1, 100 );
	glMatrixMode( GL_MODELVIEW );
  glClearColor(1.0f, 1.0f, 1.0f, 1.0f);  // background color
}

void initObjects() {
  staticBody = cpBodyNew(INFINITY, INFINITY);
  space = cpSpaceNew();
	space->iterations = 10;
	cpSpaceResizeStaticHash(space, 30.0f, 1000);
	cpSpaceResizeActiveHash(space, 30.0f, 1000);
	space->gravity = cpv(0, -100);
	cpShape *ground = cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f);
	ground->e = 1.0f; ground->u = 1.0f;
  cpSpaceAddStaticShape(space, ground);
  
  cpFloat mass = 10.0f;
  cpFloat moment = 1.0f;
  rect1 = cpBodyNew(mass, moment);
  rect2 = cpBodyNew(mass, moment);
  
  // position
  rect1->p = cpv(2, 0);
  cpSpaceAddBody(space, rect1);
  rect2->p = cpv(5, 0);
  cpSpaceAddBody(space, rect2);
}

int main(int argc, char** argv) {
  cout << "Hello world" << endl;
  cpInitChipmunk();

  glutInit(&argc, argv);
  glutInitWindowSize(window_width, window_height);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  glutCreateWindow("Hello world");
  glutIdleFunc(main_loop_function);
  GL_Setup(window_width, window_height);

  initObjects();
  glutMainLoop();

  return 0;
}
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Is there a tutorial for Chipmunk?

Post by slembcke »

Hrm. Attachments are supposed to work. I think the forum software has an annoying whitelist of allowed extension types or something. I'll have to look into that.

If you haven't read the documentation yet, you might want to do that first: http://code.google.com/p/chipmunk-physi ... umentation
gcsaba2 wrote:Then I add space and two static bodies. I also need to add an infinite static body, although I'm not sure why.
"Static body" isn't really a technical term, but it usually means an infinite mass body that never moves. You have two dynamic bodies and one (infinite mass) static body. The static body represents the mass and movement (in this case never moving) state of the world. Solid objects like the ground or walls would be attached to the static body so that you will have collisions with a solid and unmovable object. When you add a body to the space, it simulates the movement of the body including things like falling under the effects of gravity. This is not something that you want for your static body, so that is why it is not added to the space. Static collision shapes are shapes that are attached to static bodies. Because they never move, Chipmunk can treat them specially. They don't have to be checked against collisions with other static shapes, and they never need to have their collision detection data updated.

You still need to attach collision shapes to your rect bodies. Take a look at http://code.google.com/p/chipmunk-physi ... midStack.c to see a simple example of how to create boxes.

I see you did this in your code.

Code: Select all

  cpFloat moment = 1.0f;
  rect1 = cpBodyNew(mass, moment);
It's rarely a good idea to guess the moment of inertia. The moment of inertia is like the mass of an object, but in a rotational sense. The function cpMomentForPoly() will help you calculate the moment of inertia for your polygons. Guessing the moment of inertia usually leads to being off by a factor of more than 100x and usually makes objects really sluggish feeling or very jittery/twitchy.

Lastly, to animate things you call the cpSpaceStep() function which steps a space forward through time.

For drawing things with OpenGL, you will probably find this useful: http://code.google.com/p/chipmunk-physi ... ulSnippets
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 31 guests