For this class all we need to add a constructor to add values to the constants that we declared at the beginning of the code.
class Collision {
constructor(game_obj1, game_obj2) {
this.obj1 = game_obj1;
this.obj2 = game_obj2;
this.has_collided = false;
this.direction = new Vector2d(0, 0); // Direction of penetration
this.magnitude = Number.NEGATIVE_INFINITY; // Shortest distance of penetration
}
}
Uses Separating Axis Test to get the direction and magnitude of any possible collision between given two objects. https://en.wikipedia.org/wiki/Hyperplane_separation_theorem Objects can have any convex shape. Circles are a special case. The next thing that we want to add are 2 functions, these are not in a class because we want the ability use these from anywhere in the code without having a constructor.
To create the function:
function getSATCollision(game_obj1, game_obj2) {
// code
}
We will add some constants to this function that we will use in some of the if statements.
var obj1 = game_obj1;
var obj2 = game_obj2;
let temp_pos1 = obj1.pos.clone();
var d_origins = temp_pos1.subtract(obj2.pos).get_magnitude();
var collision = new Collision(game_obj1, game_obj2);
collision.has_collided = false;
var verts1 = game_obj1.get_verts();
var verts2 = game_obj2.get_verts();
var pos1 = game_obj1.pos.clone();
var pos2 = game_obj2.pos.clone();
This first if-statement is for optimization to make the game run smoothly, don’t edit it please.
if (d_origins > MAX_DIST_FOR_COLLISIONS * CELL_SIZE) {
// Optimization: Skip further checks for distant objects
return collision;
}
We will add a for loop that our code will run though all the if statement if we call this function.
for (var i = 0; i < verts1.length + verts2.length; i++) {
// code
}
This will calculate next axis by taking the normal of a side of one object
if (i < verts1.length) {
var vert = verts1[i];
if (i < verts1.length-1) var next_vert = verts1[i+1];
else var next_vert = verts1[0];
}
else {
var vert = verts2[i - verts1.length];
if (i < verts1.length+verts2.length-1) var next_vert = verts2[i+1 - verts1.length];
else var next_vert = verts2[0];
}
var side = next_vert.clone().subtract(vert).get_unit_vector();
var axis = side.get_right_normal();