나는 HTML의 캔버스와 자바 스크립트를 사용하여 소행성과 유사한 아케이드 게임을 연구 중이다. 현재 무작위로 포인트 (3, 4 또는 5)와 무작위로 무작위로 임의의 위치에 무작위로 생성 된 모양 ("소행성")이 있습니다.JavaScript 다각형 충돌 감지
이러한 다각형으로 충돌 감지 시스템을 설정하려고합니다. 나는 이것을 위해 SAT.js (https://github.com/jriecken/sat-js)를 사용 해왔다. 그러나 화면에 렌더링 된 실제 다각형 주위에 보이지 않는 충돌 다각형을 정확하게 그려 낼 수는 없습니다.
여기 내 소행성 클래스에서 내가 소행성을 렌더링하고있는 것입니다. 현재 도형 (삼각형)에서 3 점만 테스트하고 있습니다. 나는 4 점과 5 점의 소행성을 꺼냈다. 여기
ctx.moveTo(0, 0);
ctx.lineTo(10 + size, 20);
ctx.lineTo(10 + size, 20 + size);
ctx.closePath();
/**
* @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;
}
:
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.");
}
}
어떤 도움을 주시면 감사하겠습니다 - 나는 일이를 알아 내기 위해 노력했습니다 . 감사!