Page 1 of 1

My bodies fly through each other in Objective Chipmunk (

PostPosted: Sun Jul 19, 2015 8:03 am
by p1p1
Hello, everybody!

I recently decided to switch my in-game physics from Box2d to Chipmunk (I am using Objective Chipmunk, because it allows to easily integrate all gravity forces into a resulting vector if multiple gravity sources are needed, which is exactly what I want for my game). For now I'm stuck in the very beginning of switching process. I am trying to create a couple of round bodies and to make them collide.

Here's a quickstart pseudocode for box2d:
1. create the world
2. create bodies and define their positions and shapes (also size and other properties like mass, etc)
From that moment the bodies are 'collideable', that means if I apply some forces or impulses and they actually touch – bodies collide and then fly apart, without any additional collision callback setup. Callbacks are not needed for basic collisions, and are only needed if you want to customize collision handling somehow.
I got that working with box2d, the bodies are created, then they are moved with a little force towards each other and collide with each other.

Now, I suppose, the process is similar with Chipmunk:
1. create the scene
2. create bodies and define their positions and shapes (also size and other properties)
From that moment I suppose those bodies to be 'collideable'. The bodies get created normally, they receive proper sizes and masses, and when I apply initial velocity to them, they move towards each other as I step my physics scene frame by frame. But they go through each other without colliding at all. I tried watching source code of demos, but they all don't seem to do any additional setup for anything that looks like activating collisions. Demos work ok, demo bodies collide in demos, but not my bodies in my game, for some reason. Everything else works as it should, except collisions.
I googled for any examples, also tried the Objective Chipmunk tutorial with its source code, but it also does not seem to do anything extra, except for the usual setup of scene, bodies and shapes...
Most of the questions out there on stackoverflow relate to cocos2d, while in my game I am not using any kind of such a framework, so all those cocos2d examples are really irrelevant to my case.
Here's how I do it in my ObjectiveC code (excerpt):

Code: Select all
// space is created, no suprise here
ChipmunkSpace * space = [[ChipmunkSpace alloc] init];
space.iterations = PHYSICS_ITERATIONS;

// i do the following twice for two different bodies (each body will have its own shape)

ChipmunkBody * body1  = [ChipmunkBody bodyWithMass:mass andMoment:moment];
body1.position = position1; // its a cpVect
body1.userData = self; // this will be useful for me later

ChipmunkShape * shape1 = [ChipmunkCircleShape circleWithBody:body1 radius:radius offset:cpvzero];
shape1.elasticity = 1.f; // tried 0.0f 1.0f, no luck
shape1.friction = 1.f;

// i don't know when this is appropriate, so i do it after each body is defined
[space add:body1];

ChipmunkBody * body2  = [ChipmunkBody bodyWithMass:mass andMoment:moment];
body2.position = position2; // its a cpVect
body2.userData = self; // this will be useful for me later

ChipmunkShape * shape2 = [ChipmunkCircleShape circleWithBody:body2 radius:radius offset:cpvzero];
shape2.elasticity = 1.f; // tried 0.0f 1.0f, no luck
shape2.friction = 1.f;

// shape1.collisionType = [SomeClass class];
// shape2.collisionType = [SomeClass class];

[space add:body2];

// Now, with everything set up
// I only need to set their velocities towards each other
// and begin stepping and watching them collide
body1.velocity = cpvmult (cpvsub (position1, position2), 0.1f);

// and then I do
[space step:timeFrame]; // once in a timeFrame

The bodies go towards each other, but when it's time to collide, they simply fly through each other and continue flying as if nothing was on their way. Therefore their coordinates change over time, so physics takes place here, but their shapes are completely ignored when it somes to colliding.
I am not using any collision callbacks here, because I don't need to customize collision handling here, I just want them to bang and fly apart if possible, please )
I really don't think there's something more to that to make my bodies collideable, or am I really missing something here?

Re: My bodies fly through each other in Objective Chipmunk (

PostPosted: Sun Jul 19, 2015 9:33 am
by p1p1
Ok, I got the answer myself through experimenting.
For anybody who will experience the same problem, the reason was that i added the bodies to space, but forgot to add shapes to my space along with the bodies.

Don't forget [space add:body... ] then [space add:shape...] all of you Objective Chipmunk users.