How do You Find the Angle a Player Hits a Wall At?

Zecheriah

Member
EDIT! After testing a lot, I've found that this doesn't work on every wall. I dunno what's up with some of the walls but they just mess everything up. I'll edit the solution if I find a fix.

SOLUTION:
Calculate the angle the player hits the wall at:
local wallNormal = R_PointToAngle2(line.v1.x, line.v1.y, line.v2.x, line.v2.y) //finds the wall's normal angle (but I think +90°) based on its left and right edges
local playerMoAngle = R_PointToAngle2(0, 0, mobj.momx, mobj.momy) //finds the player's current angle of momentum
local hitAngle = wallNormal - playerMoAngle
       
//print the angles in readable values
print('wallNormal:' + (AngleFixed(wallNormal) / FRACUNIT))
print('hitAngle:' + ((AngleFixed(hitAngle) / FRACUNIT) - 270)) //-270 so that it's from -90° to 90°

I scoured the internet, wikis and forums for hours (I started several days before I posted this). Then, I tried looking into the code of peoples' mods that do similar things. In Ray the Flying Squirrel's code, there is a wall grabbing mechanic that happened to contain 3 lines of code that did exactly what I wanted. Then I adapted them to work with my character (see code above).

What the code does is this:
  1. it calculates the normal angle of the wall (basically just the global angle the wall is facing)
  2. then it calculates the player's angle of momentum
  3. and finally it subtracts the player's angle from the wall's angle (resulting angle between 180° and 360°)
7. I then converted the number to a readable angle between -90° and 90°



ORIGINAL POST:

I'm making a character mod where the player drives a plane. The intention is that when you hit a wall head-on, you'll explode, but if you hit the wall at an angle (say, 45° or less), you should bounce off of it. So, I need a way to calculate what angle the player is hitting a wall/linedef at.

srb20001.gif

This head-on collision explodes you, as it should

srb20003.gif

Hitting a wall at an angle like this should not explode you (that'd get pretty annoying)

My current solution is to check the player's speed after they hit a wall, and if it's below a certain value, you explode. This has inconsistent results and I'd need the wall hit angle anyway to calculate how to bounce the player.
 
Last edited:

Who is viewing this thread (Total: 0, Members: 0, Guests: 0)

Back
Top