Note that there are some explanatory texts on larger screens.

plurals
  1. POHTML5 + JS: Collision Detection For Rotatable Character
    primarykey
    data
    text
    <p>I'm working on the Collision system for my game; which is a top down shooter, the character is always static - and everything else (Map/Level), moves around him.</p> <p>The character also rotates so it's always facing the mouse position.</p> <p>With that in mind, I can't seem to get my head around my collision system, which needs to take into account of the character rotation, right?</p> <p>I basically just want to check if the given object is at all 'touching' my character/sprite. I'm not so sure if the maths I'm using is correct.</p> <p>This is my collision detection (called every update):</p> <pre><code>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(a.x + " " + b.x + " " + b.width + " " + Player.width); //A Width /*return a.x &lt; b.x + b.width &amp;&amp; a.x + Player.width &gt; b.x &amp;&amp; a.y &lt; b.y + b.height &amp;&amp; a.y + Player.height &gt; b.y;*/ var xOffset = Math.cos(Player.characterRotation); //+ Player.width; var yOffset = Math.sin(Player.characterRotation); //+ Player.height; return Map.x + xOffset &gt; b.x &amp;&amp; Map.x + xOffset &lt; b.x + b.width &amp;&amp; Map.y + yOffset &gt; b.y &amp;&amp; Map.y + yOffset &lt; b.y + b.height; } </code></pre> <p>Also, not sure if this is relevant, but this is the transform used to move my Map Canvas:</p> <pre><code>ctxMap.setTransform(1, 0, 0, 1, -Map.x + gameWidth/2, -Map.y + gameHeight/2); </code></pre> <p>Would much appreciate it if someone helped me out here :) Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload