This is kind of a programming and physics question I have here.
So I’ve been trying to perform some trigonometry to obtain an angle that a player is facing in relation to a wall based on the normal from the wall collision (allowing for far more flexibility than just comparing their global rotations since that would assume that the wall would be only one fixed angle)
In order to obtain the position needed to make the angle calculation however, I have tried to make use of Geometry2D.line_intersects_line taking the player’s position and forward vector and comparing it against the collision position and the wall normal rotated 90 degrees. The problem is however is that rotating the wall normal 90 degrees does not result in a vector that can be used accurately as a direction in creating a line as it appears to create a decent offset from where I’d assume a “rotated right” normal would be (or maybe I’m just doing it wrong).
Basically what I’m asking is how do I rotate a wall normal by 90 degrees, can this be used as a direction vector in a line calculation and is the method I’m currently using the best way to do this given that this is technically using functions meant for 2D? (though the 3D Y positional axis is unnecessary for these calculations in my case)
I’m currently doing it like this:
var WallNormalRotation1 = Vector2(get_wall_normal().x, get_wall_normal().z).rotated(90)
var PlayerForwardPlusWallIntersection1 = Geometry2D.line_intersects_line(Vector2(PlayerModelParent.global_position.x, PlayerModelParent.global_position.z), Vector2(ForwardVector.x, ForwardVector.z), Vector2(Collision.get_position().x, Collision.get_position().z), WallNormalRotation1)
Then PlayerForwardPlusWallIntersection1 is put into a function because there’s two different checks, one for -90 and one for 90 which was a kinda cobbled together solution to the player being able to face left or right while approaching the wall, therefore effecting their forward vector in a way that would impact whether or not the lines intersect. Either way it then does the thing below to get the angle.
var WallAngle = abs(rad_to_deg(Vector2(PlayerForwardPlusWallIntersection.x, PlayerForwardPlusWallIntersection.y).angle_to_point(Vector2(PlayerModelParent.global_position.x, PlayerModelParent.global_position.z))))
I am working in 3D. The 2D line_intersects_line function is just used to help obtain the angle I need.
My end goal is to just obtain how much of an angle from the wall the player is when they collide with it which must be done in a way that uses wall normals as directly comparing global rotations limits the functionality too much.
So for instance this would be about 0 degrees since the player is moving along the wall meanwhile the image below would be sorta 30 degrees-ish from the wall.
The issue I’m having is calculating a line along the wall that would end up intercepting with the line the player’s forward vector is creating because although I tried rotating the wall’s normal to get a directional vector to use in the calculation it ends up offsetting it to a spot much further away from the wall.
Can you walk me through an example calculation with real numbers from your game in a situation where the answer is far from what you are expecting? (Using print statements?)
Like break it down, step by step. Can you show all three numbers from the 3d vectors involved, not just the two getting oassed forward?
Alright so this test case, the player runs towards a wall at an angle which would result in the final angle being close to 90 degrees since the player is approaching the wall directly. This means that the forward vector will be going directly into the wall therefore meaning that an interception would happen basically right where the collision point is.
The math behind what I expect to work is one line would start from the player’s position and move along the player’s forward vector (which starts at the player and continues for about 25 units in that direction, the actual line is below)
var ForwardVector = PlayerModelParent.global_position + (PlayerModelParent.transform.basis * Vector3.FORWARD * 25)
while the other starts at the collision position and moves along the wall itself (imagine the wall face is a line and that line keeps going even if the wall face stops, that’s what I want here). The resulting Vector2 from that could then be used in combination with the player position to get an angle which would be the player’s rotation in relation to the wall.
With this in mind I don’t know how on earth the intersection point managed to go over THERE because if the forward vector is going directly towards the wall even if the wall normal was rotated and going in some weird angle it would either be instantly caught by the forward vector or just not intersect at all, but instead it somehow ended up several position units away in a spot which shouldn’t even be possible based on the vectors provided.