Welcome! Log In Create A New Profile

Advanced

3D collision detection

Posted by wilco2009 
Re: 3D collision detection
July 11, 2011 01:03AM
html parsed out my less than sign. here is it again: [codepad.org]
Re: 3D collision detection
July 11, 2011 11:16AM
I like the way you have written this Owen, nice piece of code.

Looking at the code the check_diff, has x4 compares.
Check_collision calls check_diff, so that 24 compares in total.

Try these things out...

(1) put the 'check_diff' definition into a header file to help maximize the chance of it becoming inline.

(2) To optimise 'check_collision' use logical OR '||' not bitwise OR '|', now the second part is only evaluated when the first evaluates to false.
Worst case is still the same, but you can still try swapping the order as this may help if say the second part is true more often. In my example, since check_diff returns 'bool' I've use logical AND '&&' . (I undertand that with some CPU's you can get the compile to do nice things in some cases by avoiding logical compares)

(3) Check_diff same thing as step 2

(4) If your calling code uses structures, pass in pointer of guVector (guVector structure is defined in ogc\gu.h)

(5) If its possible add new box check routine that takes four edges, no need to add/sub the length, width & depth.


guVector Ship1 = {10,10,10};
guVector Ship2 = {15,15,11}; ...
bState CheckBoxCollision( &Ship1, &Ship2... 

CheckBoxCollision( 

bool CheckBoxCollision( guVector* PointA, guVector* PointB, guVector* SizeA, guVector* SizeB) 
{
return (check_diff(PointA->x, PointB->x, SizeA->x) && 
	check_diff(PointA->y, PointB->y, SizeA->y) && 
	check_diff(PointA->z, PointB->z, SizeA->z) 
	|| 
	check_diff(PointB->x, PointA->x, SizeB->x) &&
	check_diff(PointB->y, PointA->y, SizeB->y) && 
	check_diff(PointB->z, PointA->z, SizeB->z) ) ; 
}

Extra note: The Sphere test has far less overhead - but its a case of swings and roundabouts.
If you get the chance try using a sphere test out on things like small projectiles.
(I find box checking not worth the effort, projectiles can still pass through the box edge depending and the type of math used to track motion)
Re: 3D collision detection
July 11, 2011 11:56PM
I thought about converting everything to guVector once but I eventually went crazy and changed it back to single variables.

I added a z collision check ahead of the final check so that should help it down to 8x checks if I am lucky.

The sphere check would have to be modified to operate in 3 dimensions because my game has cubes which are often elongated in the X,Y or Z direction - so we would probably end up with the same amount of code in the end.
Sorry, only registered users may post in this forum.

Click here to login