Collision Callback Function and normal_coef

Official forum for the Chipmunk2D Physics Library.
Post Reply
trevor
Posts: 4
Joined: Thu Feb 26, 2009 1:58 am
Contact:

Collision Callback Function and normal_coef

Post by trevor »

In my project I am setting a default collision pair function with cpSpaceSetDefaultCollisionPairFunc. I noticed that normal_coef is always 1.0. I am also trying to put a thin object-orientated layer on top of chipmunk for my project.

First question: Why is normal_coef always 1.0 in this case?

Second: Will the absolute value of normal_coef always equal 1?

Third: If I were doing something like:

Code: Select all

CPEntity* const entityA = (CPEntity*)a->data;
CPEntity* const entityB = (CPEntity*)b->data;

[entityA collidedWith:entityB withContacts:contacts count:numContacts normalCoef:normal_coef];
[entityB collidedWith:entityA withContacts:contacts count:numContacts normalCoef:normal_coef];
Would it make sense to negate the normal_coef in the second message line ([entityB...])? From what I gathered searching the forum, if I set up a pair function where the previous B was the pair functions A and the previous A was the pair functions B then the pair functions normal_coef would be opposite the previous normal_coef. So I think the answer is yes, but I'd like to know for sure.

Thanks in advance, and please point out anything I need to clarify.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Collision Callback Function and normal_coef

Post by slembcke »

The normal coef will always be either 1.0 or -1.0. The reason it exists is that Chipmunk calculates collision normals in whatever order it's given shapes from the collision detection routines. This might be backwards from the order defined by the collision callbacks. The coefficient let's you know that is the case. The idea is that if you want to use the collision normals to figure out if a character is sitting on the floor or bumping into a wall, you need to know which object the normal points away from. If Chipmunk has to swap the order of the shapes passed to your function to satisfy the order specified by the collision pair functions, then all the normals will be backwards.

It's entirely possible that you will always get 1.0 or -1.0 almost exclusively. There are a number of very probable situations that would cause this. (order items are added to a space, if they are static/dynamic, depending on their shape type) If you use a default collision function and don't use collision types at all, then you would end up always getting 1.0 as the normal_coef. I would still recommend multiplying by the normal_coef to avoid confusion as there are a lot of subtle things that will break that assumption.

To answer your last question, that would make sense because then each shape could know that the normals after applying the normal_coef passed to it will always point inwards or away.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
trevor
Posts: 4
Joined: Thu Feb 26, 2009 1:58 am
Contact:

Re: Collision Callback Function and normal_coef

Post by trevor »

Thanks for the explanation. After reading that and taking a look at chipmunks internals, it's all starting to make sense. Negating normal_coef in the second message is having the effect I thought it would.
Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests