Getting the angle of the ground under the player

I really suck with vectors. What I need to do is get the angle of the ground under the player, so that it returns 0 when the player is on flat ground and if the player is on a slope then the angle of the slope (preferably relative to the player, so that it returns a negative number when going down the slope)

-snip-

What I tried to do was calculate a 2d angle using **[Math.cos

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Math.cos)** and Pythagoras.

[lua]SurfaceAngle = util.Trace(LocalPlayer():GetPos(),Vector(0,0,-512),LocalPlayer()).Normal:Angle()[/lua]

[lua]
local tr = util.QuickTrace(ply:GetPos(), Vector(0,0,-512), ply)
local groundang = tr.Normal:Angle()
[/lua]
just outputs 90, 0, 0

[editline]8th November 2010[/editline]

That’s what I need

http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm should help. Kinda busy so can’t write anything down for you now D:

Maybe later.

Try this one:
[lua]
local tr = util.QuickTrace(ply:GetPos(), Vector(0,0,-512), ply)
local groundang = Angle(math.NormalizeAngle(tr.HitNormal:Angle().p), math.NormalizeAngle(tr.HitNormal:Angle().y), math.NormalizeAngle(tr.HitNormal:Angle().r))
[/lua]

what have I done!!!

[lua]
local groundangw = (Angle(math.NormalizeAngle(tr.HitNormal:Angle().p) , math.NormalizeAngle(tr.HitNormal:Angle().y), math.NormalizeAngle(tr.HitNormal:Angle().r)) + Angle(0, math.NormalizeAngle(ply:EyeAngles().y), 0)) – thanx Loures
local groundangl = ((math.abs(math.NormalizeAngle(groundangw.p))) - 90) * (math.abs((math.NormalizeAngle(groundangw.y)) / 180 * 2) - 1)
[/lua]

groundangl outputs the slope angle relative to where the player is facing, which is exactly what I wanted. It’s really complicated but it works perfect, thanks for all of the help guys!.

Assuming you have the variables eyeAngles and groundNormal
[lua]
eyeAngles.Pitch = 0
eyeAngles.Roll = 0
eyeAngles = eyeAngles:Forward()

local elevation = math.acos(eyeAngles:Dot(groundNormal))*180/math.pi-90
[/lua]
elevation is positive when going up a steep and negative when going down (stretching from 90 to -90)

Now go learn some vector math and you’ll be able to solve these problems on your own:v:

[lua]
local tr = util.QuickTrace(self:GetPos(), Vector(0,0,-512), self)
local eyeAngles = Angle(0, self:EyeAngles(), 0):Forward()
local groundNormal = tr.HitNormal
local elevation = math.acos(eyeAngles:Dot(groundNormal))*180/math.pi-90
[/lua]
outputs 0 on 50% of slopes

Take another look at your eyeAngles line.

[lua]
local tr = util.QuickTrace(self:GetPos(), Vector(0,0,-512), self)
eyeAngles = self:EyeAngles()
eyeAngles.Pitch = 0
eyeAngles.Roll = 0
eyeAngles = eyeAngles:Forward()
groundNormal = tr.HitNormal
local elevation = math.acos(eyeAngles:Dot(groundNormal))*180/math.pi-90
[/lua]

fork year

[editline]8th November 2010[/editline]

forgot the .y, I’m retarded

I think this is what I’m going to stick with:
[lua]
local tr = util.QuickTrace(self:GetPos(), Vector(0,0,-512), self)
eyeAngles = self:GetVelocity():GetNormalized():Angle()
eyeAngles.Pitch = 0
eyeAngles.Roll = 0
eyeAngles = eyeAngles:Forward()
groundNormal = tr.HitNormal
local elevation = math.acos(eyeAngles:Dot(groundNormal))*180/math.pi-90
[/lua]