Page 1 of 2

Body disappearing upon collision

Posted: Fri Sep 21, 2007 12:06 pm
by milliams
I have a cpBody with INFINITY mass and moment of inertia. The Body isn't added to the space but the cpPolyShape describing it, is (as a Static Shape).

The problem I'm having is that when a circle (mass 1, radius 1, moment of inertia from cpMomentForCircle()) is added to the Space above it, it falls correctly but upon contacting the immovable body, its x and y coords suddenly become 'NaN'. Just before the collision the y-velocity of the circle=-24 and immovablebody=0, but after the collision (at the same time that the circle's position becomes NaN) the y-velocity of the circle=-12 and the immovablebody=-12. After this, the circle continues to accelerate downwards (according to it's velocity value) but the velocity of the immovablebody remains at -12.

Does anyone have any idea why:
  1. The position of the circle suddenly becomes NaN (when it should probably just bounce)
  2. The immovable body suddenly acquires a velocity
By the way, I am using the SVN version (could that cause a problem?). Any help would be greatly appreciated

Re: Body disappearing upon collision

Posted: Fri Sep 21, 2007 1:14 pm
by Michael Buckley
I had the same problem with the non-SVN version when I was developing. It was caused by using integers instead of floats for some values. Because of some of the calculations Chipmunk does, if you have any integers in there, it will give back a NaN value. This might not be the cause of the problem you're having, but you might want to double-check and make sure that everything having to do with Chipmunk is in a float value.

Re: Body disappearing upon collision

Posted: Fri Sep 21, 2007 1:33 pm
by milliams
The problem I had with the immovable body attaining a velocity was caused by me not actually setting its mass and moment of inertia to INFINITY (it was a bug in the Ogre3D wrapper code I was writing). As for the NaN problem, I think you may be right about the use of ints, I've explicitly set all of mine to floats now to try to avoid the problem. Thanks for the help.

Re: Body disappearing upon collision

Posted: Sat Sep 22, 2007 2:14 am
by slembcke
Could you explain what you mean by using ints? Do you mean simply changing a 0 to a 0.0f or something? What compiler?

Re: Body disappearing upon collision

Posted: Sat Sep 22, 2007 7:08 am
by milliams
Yes, that was all I meant. For some reason it seemed to help. I'm using gcc 4.2.

Now that I'm creating a more complex scene, I'm seeing objects disappearing (position or velocity going to NaN) more frequently. At the moment, whenever I attach two bodies together with a cpPinJoint, one of the objects immediately goes to NaN (if it's a static, non-space body then just its velocity goes NaN. But if it's a normal shape then velocity, position and angle all go NaN) and upon a collision between two other bodies in the scene, they might disappear too. Even if the two bodies should be unaffected by the joint (but they only disappear if the joint is added to the space).

Have you experienced bodies going NaN at all before and if so, what was the cause?

Re: Body disappearing upon collision

Posted: Sat Sep 22, 2007 4:52 pm
by slembcke
I haven't had NaN problems for quite a while myself. At one point I went through and made sure that none of the divide ops could cause NaNs. I think the only issue I've had since is accidentally letting infinite mass objects collide.

What version, Release 3? Have you tried the version in SVN? I've fixed a number of minor issues. (Unfortunately I've been rather bad about keeping a change log.) Is it possible to make a simple example that causes the bug?

To quote Shawn Hargreaves, the guy who wrote the Allegro lib:
If you can send me a 10 line program, I will fix it. 100 lines, and I can probably fix it. 1000 lines, and I don't have a chance :-)

Re: Body disappearing upon collision

Posted: Sat Sep 22, 2007 8:03 pm
by milliams
slembcke wrote:What version, Release 3? Have you tried the version in SVN? I've fixed a number of minor issues. (Unfortunately I've been rather bad about keeping a change log.) Is it possible to make a simple example that causes the bug?
I'm currently using the version in SVN. I'll have a go tomorrow at creating a test case (I'll have to rip out all my C++ convenience functions etc. but it shouldn't be too hard). The problem is most likely caused by me doing something I shouldn't with the library :D

Re: Body disappearing upon collision

Posted: Sun Sep 23, 2007 8:36 am
by milliams
Okay, I recreated my scene in the Demo application and it worked correctly so it would seem that the bug is caused by my C++ "convenience" functions and not a problem with Chipmunk at all :D Now I've just got to work out where the bug is coming from...

Re: Body disappearing upon collision

Posted: Sun Sep 23, 2007 9:28 am
by milliams
(I know I'm replying to myself again but meh)
The cause of the problem was that within OGRE (the renderer I was using to draw stuff) there's an option to execute code every time a frame is drawn. It was here that I would call cpSpaceStep() with dt being equal to the time passed since the last frame was drawn. Since I wasn't drawing anything very intensive, this timeSinceLastFrame variable would sometimes equal zero (or at least something very, very small). This, when passed to cpSpaceStep() would cause these NaNs.

Re: Body disappearing upon collision

Posted: Sun Sep 23, 2007 2:40 pm
by slembcke
Ah, I was accidentally passing negative dt values once. That was really wierd... not at all you would expect. I suppose I should at least make cpSpaceStep() short circuit when passed a dt of 0.

Also, while Chipmunk does allow you to pass a non-constant dt value, I mention in the docs that you should probably avoid doing so. It prevents the contact persistence from working as well as it could. Using the same dt value allows Chipmunk to more effectively reuse the calculations from the frame before.