Skip to main content
2 of 3
Clarify the image.
Zhafur
  • 169
  • 3
  • 13

Calculating wall angle and sliding in 2D

Got a new project recently where I don't use any physics engine, but it requires some basic phyiscs. Basically it's a 2d "top-down" game type. I would like to add a smooth wall collision which feels better than those "jumping" ones. So here is how bad it looks like, I easily calulated the collision point in this way:

  1. Added the velocity to the player's position.
  2. Checked whether the new position is in the wall or not.
  3. Went backwards at that vector where it "came from" until reached the first non-colliding position.

Now that I have the blue position, I would like to modify the player's vector's angle with that purple angle (I guess). Because I want the next thing to happen when the player reaches that position:

  • The player gets the wall's angle and walks next to it (of course only if the player still presses the key to move upwards) like in most AAA 3D games. For example TESV:Skyrim. If you are moving toward a wall or an object in a non 90° angle, then the player keeps moving but next to the wall/object.

I have already searched for solutions on Google and here aswell, but none of them fit my needs.
Would appreciate any help. Thanks!

EDIT:

  • Green Line: Wall (Top one, the bottom one just for help)
  • Purple Point: Player old position
  • Blue Point: Player new position (next to the wall)
  • Red Line: Vector between new and old position


![_][1]
Zhafur
  • 169
  • 3
  • 13