Separating Axis Theorem Collision Detection

Started by
3 comments, last by oliii 18 years, 4 months ago
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.
~Joseph Duchesnejd@staronesw.comwww.staronesw.com
Advertisement
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.
I wouldn't use that code though. I would first read this and this, and use that code.

and a physics demo here.

Everything is better with Metal.

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
~Joseph Duchesnejd@staronesw.comwww.staronesw.com
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++.

Everything is better with Metal.

This topic is closed to new replies.

Advertisement