bug in cpSpaceSegmentQueryFirst

Discuss any Chipmunk bugs here.

bug in cpSpaceSegmentQueryFirst

Postby viblo » Fri Dec 25, 2009 3:36 pm

I think Ive found a bug in cpSpaceSegmentQueryFirst, it hangs sometimes depending on the start/end value.
For example, I made two calls to it in an empty space:
with start = (13,1) and end = (131.01,2) it works
with start = (0,0) and end = (131.01,0) hangs in what feels like an infinite loop

(I did the testing from python, but I don't think that should affect this problem)
http://www.pymunk.org - A python library built on top of Chipmunk to let you easily get cool 2d physics in your python game/app
viblo
 
Posts: 206
Joined: Tue Aug 21, 2007 3:12 pm

Re: bug in cpSpaceSegmentQueryFirst

Postby slembcke » Sun Dec 27, 2009 12:06 pm

I wasn't able to reproduce the issue. Did you change the spatial hash settings at all?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: bug in cpSpaceSegmentQueryFirst

Postby viblo » Sun Dec 27, 2009 8:24 pm

Oh, I should have created a little c program to test it directly... But you know how it is, I like coding in python much more than in c ;)

Anyway, after I read your post I created a little test program in c and at the first I couldn't reproduce it either, but after a lot of printf-debugging in the chipmunk code I think Ive found both the problem and why it didn't show up for me first. (Anyone know of a better way than binary-search-with-printfs to debug inside a compiled chipmunk.dll loaded from python?)

The problem is that in the cpSpaceHashSegmentQuery function in cpSpaceHash.c the next_v variable can be NaN. When you compile with -ffast-math the if-statement in the while loop select one exec path, and when you compile without the other path. That is, next_v < next_h is true with -ffast-math, and false without -ffast-math.
http://www.pymunk.org - A python library built on top of Chipmunk to let you easily get cool 2d physics in your python game/app
viblo
 
Posts: 206
Joined: Tue Aug 21, 2007 3:12 pm

Re: bug in cpSpaceSegmentQueryFirst

Postby slembcke » Sun Dec 27, 2009 9:05 pm

That's weird. I've never actually ran into an issue where -ffast-math didn't produce the correct results anyway on the PPC and Intel OS X and Linux machines that I've run on.

As for debugging, I'd normally say to just use good old GDB, but I have no idea if that would work on Windows with DLLs and all.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: bug in cpSpaceSegmentQueryFirst

Postby viblo » Tue Dec 29, 2009 10:06 am

This little code-snippet prints 1 with -ffast-math but 0 without:
Code: Select all
double b = 0;
b = b/b;
printf("%i", b == b);


Same result on both my laptop with intel cpu, win vista and mingw/gcc v.3.4.5, and on a small server I have with an amd cpu running ubuntu and gcc 4.4.1.
http://www.pymunk.org - A python library built on top of Chipmunk to let you easily get cool 2d physics in your python game/app
viblo
 
Posts: 206
Joined: Tue Aug 21, 2007 3:12 pm

Re: bug in cpSpaceSegmentQueryFirst

Postby slembcke » Tue Dec 29, 2009 11:47 am

Bah. Crap. You are right. I guess there is a fair amount of fiddling with infinity, but never NaN except for there. Thus the code attempting to fix them never actually worked when compiled as release. My debug target does not include -ffast-math I think, so I probably just never noticed.

Code: Select all
   // fix NANs in horizontal directions
   next_h = (next_h == next_h ? next_h : dt_dx);
   next_v = (next_v == next_v ? next_v : dt_dy);
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: bug in cpSpaceSegmentQueryFirst

Postby slembcke » Tue Dec 29, 2009 12:02 pm

Ok. I added a fix to detect the 0*infinity to prevent ever getting a NaN. Now it works using -ffast-math and without on processors that at least handle infinite math nicely without it.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm


Return to Bugs

Who is online

Users browsing this forum: No registered users and 1 guest