Sign in to follow this  
Joseph Duchesne

Separating Axis Theorem Collision Detection

Recommended Posts

Hi, I've been trying to write a simple separating axis collision detection function between two polygons in C and I can't find any working example code. I don't really care what language it's in, as long as I can run it on my mac and can then port it to C. I'm also simultaneously working on the same thing in Processing, just for testing purposes. You can view the Processing Java Applet here: http://magneon.ath.cx/Test (source file linked from the page) to see what my problem is (the collision is just a little bit off on one side). If anyone has any links or example code lying around I would greatly appreciate it.

Share this post


Link to post
Share on other sites
jyk    2094
Hm, I wish my code were cleaned up enough that I could give it to you, but as is I'll just direct you to oliii's demo code for polygon collision detection in 2d. If you need specific help, I, oliii, or others can probably help you with whatever aspects of the SAT you might be having problems with.

You might also tell us what intersection info you need. I'm assuming you need the time of intersection, and probably the normal as well for collision response. What about contact points? If you can do without these (and if you're not doing a full-on physics sim I'm guessing you can) it will simplify the code somewhat.

Share this post


Link to post
Share on other sites
Thanks for the great replies guys, I've read through everything :). I still can't get my code to work :(. I'm using C, and I can't get your C++ code to run (I'm compiling from a mac). I have a method that works fine for stationary objects (checking vertex incusion and edge intersection) but this doesn't give me anything to work with for collision response. I would like my end function to look something like this:

int polyCollide(float polyA[][32], int numVertexA,float polyB[][32], int numVertexB, float &polyAXSpeed,
float &polyAYSpeed,float &polyAX, float &polyAY, float polyBX,float polyBY)
{
//here be the little bit I'm missing
if(polygonsCollide){ return 1; }else{ return 0;}
}


where polyAX/YSpeed are pointers to the speed of the first polygon's speed. If a collision takes place the pointers for polygon a's speed and position get updated to reflect the collision. Polygon B is immobile. I would really rather know what to do and code this myself rather than borrow someone elses code (although after a few days of smashing my brain against this problem I'm not sure if it's worth the offort).

Does anyone have examples/tutorials anything in C?

Thanks,
Joseph

Share this post


Link to post
Share on other sites
oliii    2196
I dunno what to say more... in the thread I presented, I posted a method to do what you asked, as well as C source code. Well, it's C done with C++, but it is relatively straight forward.

If you don;t understand the code, your problem is not with the collision stuff, but with c/c++.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this