Note that there are some explanatory texts on larger screens.

plurals
  1. POC# 2D collision detection problem
    text
    copied!<p>I am stuck trying to figure out how to alter my collision detection to work correctly, i got all my wall objects stacked inside a List and then when the player moves i loop thru each wall object and call the DetectCollision method, this returns true or false depending on if the object is inside the wall or not.</p> <p>Wall detect collision (X- and Y-coordinate is the position of the wall)</p> <pre><code>public bool DetectCollision(float x, float y) { if ((x &gt;= this.XCoordinate &amp;&amp; x &lt;= (this.XCoordinate + this.BlockWidth)) &amp;&amp; (y &gt;= this.YCoordinate &amp;&amp; y &lt;= (this.YCoordinate + this.BlockHeight))) return true; else return false; } </code></pre> <p>So in my player function when the player tries to move, i add the movement to a temporary X,Y coordinate and check if those Collide against the wall, if they do nothing happens, otherwise i move the player.</p> <p>But i have noticed that it doesn't work as it should be, if i add a piece of wall inside of the gamefield it only checks the bottom right corner for collision detection?</p> <p>Player movement method:</p> <pre><code> float x, y; if (direction == Direction.E) { x = LiveObjects.player.XCoordinate - MovementSpeed; y = LiveObjects.player.YCoordinate; } else if (direction == Direction.W) { x = LiveObjects.player.XCoordinate + MovementSpeed; y = LiveObjects.player.YCoordinate; } else if (direction == Direction.N) { x = LiveObjects.player.XCoordinate; y = LiveObjects.player.YCoordinate - MovementSpeed; } else { x = LiveObjects.player.XCoordinate; y = LiveObjects.player.YCoordinate + MovementSpeed; } if (GameMechanics.DetectWallCollision(x, y) || GameMechanics.DetectWallCollision((x + LiveObjects.player.BlockWidth), (y + LiveObjects.player.BlockHeight)) { OnPlayerInvalidMove(null, new PlayerEventArgs()); return; } </code></pre> <p>and the loop for DetectWallCollision is just:</p> <pre><code>foreach (Wall wall in LiveObjects.walls) { if (wall.DetectCollision(x, y)) return true; } return false; </code></pre> <p>Any ideas?</p>
 

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