Question

I'm wondering what the best way to go about advanced collision detection in AS2 would be. I don't mean bounding boxes, because that's fine. Specifically how do I stop the player from moving in one direction when colliding with something.

Imagine a top-down shooter. Basically I'm looking for how the collision detections were achieved in a game similar to this: http://ninjakiwi.com/Games/Action/Play/SAS-Zombie-Assault-2.html

I have an array that keeps track of the direction the player is currently moving(up, down, left, right). I also have four variables allowing the player to move in each direction if the corresponding variable is true. When the player collides with a movieclip in the collision array I set the variables relating to the directions he is moving to false, so he can't keep moving in the direction he was going when he collided with the object. The problem arises when the player is moving diagonally(two directions) and collides with something that he should be able to continue moving in one direction in. Example:

enter image description here

The play should be able to move in the up direction, but cannot.

Another problem is that the variables determining wether or not the player can move are set back to true when the opposite direction's key is pressed(ie. able to move right is set to true when left is pressed, since we've moved in the opposite direction). So in the previous example if you were not to move diagonally and simply move right, then hold right down while moving up until you aren't touching the object anymore you wouldn't be able to move right because the left key wasn't pressed.

It seems like I'm over complicating things, is there any way besides individually placing all walls that stop the player from moving in one direction?(Ie. If the player touches this wall, stop moving right). <--- This way doesn't allow for randomly generated maps when objects are rotated. Or is there some way I can get around this using math depending on the object rotation?

What's the easiest way to hitTest? :D

Was it helpful?

Solution

You haven't posted any code so it's hard to tell, but you're probably going about reacting to a collision the wrong way.

Rather than disabling movement if you collide with your wall, you should instead move the player to the outside of the wall if he collides with it.

Basically what you want to do is:

  1. Keep track of where the player was last frame.
  2. Check if the player is colliding with a wall this frame.
  3. If he is, check where he was last frame to get an idea of which side of the wall he was on before the collision occurred. Use this information to reposition the player so that he is touching that face of the wall.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top