I am working on an asteroids-like arcade game using HTML's canvas and JavaScript. I currently have a bunch of randomly generated shapes ("asteroids") with a random number of points (3, 4, or 5) and a random size at random locations.
I am trying set up a collision detection system with these polygons. I have been using SAT.js (https://github.com/jriecken/sat-js) for this. However, I can't accurately seem to draw the invisible, collision polygons around the actual polygons rendered on the screen.
Here is what I have in my asteroid class where I am rendering the asteroids. I am currently only testing with 3 points in the shapes (triangles). I turned the 4 and 5 point asteroids off.
ctx.moveTo(0, 0);
ctx.lineTo(10 + size, 20);
ctx.lineTo(10 + size, 20 + size);
ctx.closePath();
Here is the corresponding SAT.js code.
/**
* @function createCollisionPolygon
* Traces the outline of the asteroid to allow it to detect collisions
* based on the number of points the shape has (3, 4, or 5)
* @param {asteroid} The asteroid to make collision detectable
* @return The traced polygon
*/
function createCollisionPolygon(asteroid)
{
var V = SAT.Vector;
var P = SAT.Polygon;
var polygon;
switch(asteroid.randomNumPoints)
{
// 3 point polygon
case 3:
polygon = new P(new V(asteroid.position.x, asteroid.position.y), [
new V(10 + asteroid.size, 0),
new V(asteroid.position.x,asteroid.position.y),
new V(10 + asteroid.size, 20 + asteroid.size)
]);
break;
}
return polygon;
}
/**
* @function checkCollision
* Checks for collisions between any two asteroids
* @param {polygon1} The first asteroid
* @param {polygon2} The next asteroid
* @return True if there was a collision, false otherwise
*/
function checkCollision(polygon1, polygon2)
{
var response = new SAT.Response();
var collided = SAT.testPolygonPolygon(polygon1, polygon2, response);
return collided;
}
Which is later being called here:
for(var i = 0; i < asteroids.length - 1; i++)
{
var asteroid1 = asteroids[i];
var asteroid2 = asteroids[i+1];
// Trace an invisible outline around each asteroid
var polygon1 = createCollisionPolygon(asteroid1);
var polygon2 = createCollisionPolygon(asteroid2);
// console.log("Polygon 1: "+ console.log(polygon1.points[0]
// + console.log(polygon1.points[1]) + console.log(polygon1.points[2])));
// console.log("Polygon 2: " + console.log(polygon2.points[0]
// + console.log(polygon2.points[1]) + console.log(polygon2.points[2])));
// Check if there is a collision
if(checkCollision(polygon1, polygon2))
{
asteroid1.color = 'red';
asteroid2.color = 'red';
console.log("Collision detected.");
}
}
Any help would be appreciated - I've been trying to figure this out for days. Thanks!