Archived

This topic is now archived and is closed to further replies.

Aurora

Collision detection problem

Recommended Posts

Hi guys, I have a problem with the collision detection in my 3D engine. It is working fine if i set the size of the bounding sphere around the user to 0, but if I set it to say 0.5, it will work ok for the middle of walls but let you walk straight through the corners. The collision code is called for each triangle and then recursively called again if a collision occurs. Any idea as to what I am doing wrong? Code for collision detection: bool collision(TRIANGLE *triangle, float *newPos) { float intersect[3], destPos[3], temp[3], normal[3]; float lineLength, percentage, distance; const float bound = 0.5f; SCALAR(normal, triangle->normal[0], bound); SUB(temp, currentPos, normal); ADD(destPos, temp, velocity); distance = distFromPlane(destPos, triangle->vertices[0], triangle->normal[0]); if (distance < 0.0f) { lineLength = DOT(velocity, triangle->normal[0]); if (lineLength != 0.0f) { percentage = -distance / lineLength; if (percentage > -1.001f && percentage < 0.001f) { SCALAR(temp, velocity, percentage); ADD(intersect, destPos, temp); if (hitPolygon(intersect, triangle->vertices)) { SCALAR(temp, triangle->normal[0], lineLength); SUB(velocity, velocity, temp); ADD3(newPos, intersect, velocity, normal); return true; } } } } return false; } Code to test if a point is in a triangle: bool hitPolygon (float *intersection, float vertices[3][3]) { float lines[3][3], totalAngles; SUB(lines[0], intersection, vertices[0]); SUB(lines[1], intersection, vertices[1]); SUB(lines[2], intersection, vertices[2]); reduceToUnit(lines[0]); reduceToUnit(lines[1]); reduceToUnit(lines[2]); totalAngles = acos(DOT(lines[0], lines[1])) + acos(DOT(lines[1], lines[2])) + acos(DOT(lines[2], lines[0])); if (fabsf(totalAngles-6.283f) < 0.13f) return true; else return false; }

Share this post


Link to post
Share on other sites
Thats the exact same problem im having with my collision detection, i can walk into the outer corners of my cubes! What i was intending to do was use about 6 points about my player to test to see if anyone of them actually made a collision..

Share this post


Link to post
Share on other sites
In this line

if (fabsf(totalAngles-6.283f) < 0.13f) return true;

have you tried to reduce the 0.13 to, for example 0.5 ?
Are you using clockwise or counterclockwise triangles ?

Bruno

Share this post


Link to post
Share on other sites
Yeah I tried changing the value to larger and small numbers, if it is set to low then you can walk through all of the walls but if I set it higher then it partly solves the problem but generates loads of false collisions with planes which aren''t being collided with. All my triangles are specified anti-clockwise.

Share this post


Link to post
Share on other sites
Another thing I noticed is that in my world I have a wall with a 90 degree corner in it like this: (from above)

---------|
-------| |
| |
| |

If i walk into the corner from the inside the collision detection is fine but if i walk into the pointy bit from the outside it walk straight through.

Share this post


Link to post
Share on other sites
Hi

If you are using counter-clockwise triangles, i think you have a problem in your hitPolygon function.

Try changing from :

SUB(lines[0], intersection, vertices[0]);
SUB(lines[1], intersection, vertices[1]);
SUB(lines[2], intersection, vertices[2]);

to

SUB(lines[0], intersection, vertices[0]);
SUB(lines[1], intersection, vertices[2]);
SUB(lines[2], intersection, vertices[1]);


Bruno

Share this post


Link to post
Share on other sites
Hmmm, thanks for the suggestion, that seems to reduce the amount of false plane collisions, although I still need to set the 0.13 value to something really high to avoid being able to walk through corners. If I do this then it is really noticeable for example if you walk up a slope, as you are forced to walk high into the air to get over the top of the slope.

Share this post


Link to post
Share on other sites
Tell me your e-mail, and i will sent you my collision routines.., they are very similar, and simpler to understand..,

Bruno

Share this post


Link to post
Share on other sites
Erm, I''m having the exact same problem, only I work with poly''s, not triangles for the collision detection code (portal engine).
I don''t want to bother you, but Bruno, could you send me your collision detection code too, I promise I won''t copy/paste it, it''s just for reference.
I would be forever gratefull, my e-mail is Flous@hotmail.com

cu

Share this post


Link to post
Share on other sites
Hi Bruno, my email address is mikeyv1000@yahoo.com, I would appreciate it very much if you could send me your code.

Thanks

Share this post


Link to post
Share on other sites
can u post it to me too.
I had a similar problem in my last comp sc project and I would like to fix it.
andrewchiu@zerg.com

Share this post


Link to post
Share on other sites