Bug in cpSpaceSegmentQueryFirst

Official forum for the Chipmunk2D Physics Library.
Beta Carotene
Posts: 123
Joined: Sat Aug 04, 2012 6:34 pm
Contact:

Re: Bug in cpSpaceSegmentQueryFirst

Post by Beta Carotene »

Im still messing around with various things. Nothing seems to be responding at all except modifying that function
Beta Carotene
Posts: 123
Joined: Sat Aug 04, 2012 6:34 pm
Contact:

Re: Bug in cpSpaceSegmentQueryFirst

Post by Beta Carotene »

so I've been thinking, and I think it might actually not be an issue with Visual Studio.

I was doing some experiments and noticed something very interesting.

So, my engine supports free-form geometry for platforms, but its default mode is to snap squares to a grid (of variable sizes) for quick prototyping. I was playing around with the tiles in the world, moving them up and down while the character was on it. I quickly noticed that the errors and glitch behavior ONLY occurred when the tile border was dead-smack on an integer. Like if the floor level was at -38 or at -39, the glitches happened, but 38.000001 - 39.999999, they were gone.

To me, this seems like it actually might be an issue with a missing corner case in then algorithm.

I know there's been extensive testing with your engine, but it seems like the case of ray testing from such exact distances on such exact borders may have slipped through the cracks. I was doing some searching online and I found at least one other post of a person with a very similar issue. The thread went nowhere though
Beta Carotene
Posts: 123
Joined: Sat Aug 04, 2012 6:34 pm
Contact:

Re: Bug in cpSpaceSegmentQueryFirst

Post by Beta Carotene »

yeah see.... and the bug also goes away when I rotate the platform by 0.00001
ShiftZ
Posts: 114
Joined: Sat Mar 07, 2009 7:23 am
Contact:

Re: Bug in cpSpaceSegmentQueryFirst

Post by ShiftZ »

I dont want to interrupt you, but i have just updated chipmunk and suddenly discovered that cpSpaceSegmentQueryFirst cast sometime returns flipped normal. It's directed "inside" the shape. Is that common behaviour?

EDIT Oh, well, that was querying inside-out.
Last edited by ShiftZ on Tue Apr 30, 2013 1:55 pm, edited 2 times in total.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Bug in cpSpaceSegmentQueryFirst

Post by slembcke »

Blah. I haven't been able to reproduce the issue at all as a unit test. I'm just going to make a blind rewrite of cpBBSegmentQuery() I guess.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Bug in cpSpaceSegmentQueryFirst

Post by slembcke »

Ok. Try this as a replacement cpBBSegmentQuery()

Code: Select all

static inline cpFloat cpBBSegmentQuery(cpBB bb, cpVect a, cpVect b)
{
	cpFloat txmin = -INFINITY, txmax = INFINITY;
	cpFloat dx = b.x - a.x;
	if(dx != 0.0f){
		cpFloat idx = 1.0f/dx;
		cpFloat tx1 = (bb.l - a.x)*idx;
		cpFloat tx2 = (bb.r - a.x)*idx;
		txmin = cpfmin(tx1, tx2);
		txmax = cpfmax(tx1, tx2);
	}
	
	cpFloat tymin = -INFINITY, tymax = INFINITY;
	cpFloat dy = b.y - a.y;
	if(dy != 0.0f){
		cpFloat idy = 1.0f/dy;
		cpFloat ty1 = (bb.b - a.y)*idy;
		cpFloat ty2 = (bb.t - a.y)*idy;
		tymin = cpfmin(ty1, ty2);
		tymax = cpfmax(ty1, ty2);
	}
	
	if(tymin <= txmax && txmin <= tymax){
		cpFloat min = cpfmax(txmin, tymin);
		cpFloat max = cpfmin(txmax, tymax);
		
		if(0.0 <= max && min <= 1.0) return cpfmax(min, 0.0);
	}
	
	return INFINITY;
}
It's a more verbose than the original, but removes the reliance on infinite math except for comparisons.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Beta Carotene
Posts: 123
Joined: Sat Aug 04, 2012 6:34 pm
Contact:

Re: Bug in cpSpaceSegmentQueryFirst

Post by Beta Carotene »

So I tried your new version. I don't know if the error is gone, but I can no longer get it to happen and my speed is pretty much back to normal. I'll contact again if I find anything else wrong. Thanks much!

Perhaps you should include this as a "ifdef MSVC" pre-processor statement. Or perhaps if this new function is just as fast (or good enough), you can just replace the old version. I'll leave that one up to you though ;)

Thanks so much for your help!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Bug in cpSpaceSegmentQueryFirst

Post by slembcke »

I think the performance of the new one should be pretty similar. It's really just more verbose.

Though even using integer coordinates I'm not sure how this one worked and the old one didn't unless it's a compiler bug. :-\ It was really only relying on 1.0/(+-)0.0 to produce +-infinity and multiplying by infinity. That was really the only part I took out. Not really like it matters. Even if it's verified to be a compiler bug, it would need a workaround in Chipmunk anyway.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Beta Carotene
Posts: 123
Joined: Sat Aug 04, 2012 6:34 pm
Contact:

Re: Bug in cpSpaceSegmentQueryFirst

Post by Beta Carotene »

ok.... so while that error has not popped up again, now I've got all kinds of weird shit happening in my program. This is without question some kind of memory corruption issue happening somewhere. I'll keep you updated if I find anything.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Bug in cpSpaceSegmentQueryFirst

Post by slembcke »

Sounds like it's time for some valgrind. :-\
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests