Re: Collision detection in 2d games when turning is a thing?
Dranelement, does this mean you already have your guy walking around freely in 2D space? So just wall collision is the issue?
Since you already have code to calculate the new X Y position of the player after he takes a step (or moves from a game loop, or whatever), this might help you. Instead of updating the player's actual position, lets assume the new X and Y you calculated are stored in 2 temporary variables, tempX and tempY.
If your walls are stored in a 2D array of bools (I think I read that up above), round tempX and tempY and use those as the 2 coordinates for the 2D wall array to see if it returns true or false. That should let you know if the position your guy is about to step contains a wall or not. If it does not contain a wall, set his X and Y equal to the tempX and tempY variables and you're done.
If a wall does exist in that spot, you have 2 options.
Option 1 is that you can just play some collision sou
nd and do nothing. Because the player's X and Y were never set to anything, the guy simply stops when he hits a wall. One side effect of this is that even if the player hits a wall at a shallow angle, they will stop and stick as if the wall is sticky. There is no automatic sliding along it.
Option 2 is to check to see if the player should be sliding along a shallow angle. You'll be doing the same wall check as before, but this time you'll test (X, tempY) and then (tempX, Y). One of those 2 directions should return a wall and the other will be open land. If (X, tempY) was open land, do not update the player's X coordinate. Only update their Y coordinate to be equal to tempY. This is essentially saying that after hitting the wall, any momentum in the individual directions can still continue to act on the player. The end effect is that a player can glance against a wall and continue moving, even though they technically hit something. Perhaps a sliding sound effect or something could play during this, just to help them know they've hit something but are not actually stopped.
There are pros and cons to each method, and hopefully some of this was helpful.
_______________________________________________ Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector