Hello, been a while since I've posted anything, I know this isn't specifically code related, and mostly math related, however recently I've been looking into seperating axis theorem, I've found several tutorials/explanations online, and to be honest, I cant exactly understand how all the math works(yet), but I do understand what it's doing. I've managed to successfully implement detection with rectangles, however I'm still having difficulties with a couple things, and I was hoping someone here could help give some insight into the situation. ^_^
My first problem is, for some reason I can't get collision between a circle and a square to work properly, I've found a tutorial which seems to cover it but whenever I try to implement it the results are incorrect, the tutorial can be found
here. The math for circle/square collision is at the very bottom, I also have the same issue with polygons, I've tested with a simple rectangle and a square and again, it seems the math just isn't there, you can find the code for circles and polygons below( please note the code used is ES6 ).
Rectangle * Polygon collision.
collision( a, b ) {
var collided, corners, axes;
collided = true;
corners = [ a.points, b.points ]; //push all the normals into an array.
axes = this.getAxes( a ).concat( this.getAxes( b ) ); //get new axes based on the normals
for ( let i = 0; i < axes.length; i++ ) { //iterate through the axes we need to test.
let axis, p1, p2 = 0;
axis = axes[i];
p1 = this.getProjections( axis, corners[0] ); //projections along the new axis for our first shape.(an object with min and max value).
p2 = this.getProjections( axis, corners[1] ); //projections along the new axis for second shape.
if ( p1.max < p2.min ) collided = false;
if ( p2.max < p1.min ) collided = false;
if ( !collided ) break;
};
return collided;
};
Rectange * Circle Collision.
rectCirCollision( a, b ) {
//a = rectangle, b = circle
let circle, max, corners;
max = -Infinity;
corners = a.points; // get all the normals of the rectangle.
circle = new Vector( b.x - a.center.x, b.y - a.center.y ); //create a new vector with the angle between the rect & circle.
for ( let i = 0; i < corners.length; i++ ) { //iterate through the normals.
let v = new Vector( corners[i].x - a.center.x, corners[i].y - a.center.y ); //new vector with angle of the center of triangle to each corner.
let proj = v.dot( circle.normalize() ); //get our projection based on the circle vector as a unit vector.
max = proj > max ? proj : max; // set our max distance.
};
if ( circle.magnitude - max - b.radius > 0 && circle.magnitude > 0 ) { //not exactly sure whats going on here, got this code from the tutorial linked above. xD
return false;
};
return true;
};
My other question is I'm not entirely sure how to handle collisions once they happen. I didn't find much info on how to push the collided sprite outside of the collision area once it's collided. Anyone know a good tutorial that could help with this? or have any advice on the matter?
any help is much appreciated. thanks in advance. If you need more code just let me know, if you'd like i can post all the code i've written thus far. ^^