I am working on a Collision system for my game; which is a top-down arrow, the character is always static, and everything else (Map / Level) moves around him.
The symbol also rotates so that it is always in the mouse position.
With that in mind, I can't seem to lower my head around my collision system, which should take into account the rotation of the character, right?
I just want to check if this object really βtouchesβ my character / sprite. I am not sure that the math I use is correct.
This is my collision detection (called each update):
function detectCollisions(){ //Detect for game props if(collides(Map, TestProp)){ console.debug("Touching..."); } } function collides(a, b){ //ctxMap.setTransform(1, 0, 0, 1, -Map.x + gameWidth/2, -Map.y + gameHeight/2); //var transX = -Map.x + gameWidth/2; //var transY = -Map.y + gameHeight/2; //need to take player rotation into account too! //console.debug(ax + " " + bx + " " + b.width + " " + Player.width); //A Width /*return ax < bx + b.width && ax + Player.width > bx && ay < by + b.height && ay + Player.height > by;*/ var xOffset = Math.cos(Player.characterRotation); //+ Player.width; var yOffset = Math.sin(Player.characterRotation); //+ Player.height; return Map.x + xOffset > bx && Map.x + xOffset < bx + b.width && Map.y + yOffset > by && Map.y + yOffset < by + b.height; }
Also, not sure if this is relevant, but this is the transformation used to move my map canvas:
ctxMap.setTransform(1, 0, 0, 1, -Map.x + gameWidth/2, -Map.y + gameHeight/2);
It would be very nice if someone helped me here :) Thank you!
javascript html5 rotation collision transform
Oliver jones
source share