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.