math.atan2

math.atan2

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10454
Joined: 24 Jan 2006, 21:12

math.atan2

Post by PicassoCT »

Im tryint to y-turn a units leg towards a point..(both are in worldspace) .. so i assumed using math atan2 i could get the degree in rad where to turn the leg.
However this did not work out.
What was the magic dance one had to do, to get spring, to behave correct?
Did i miss out on one step?

Code: Select all

	
function resolveKinematics(x,y,z,  tPx,tPy,tPz, lorgx,lorgy,lorgz, number,targPoint)

	zvx,zvy,zvz=tPx-lorgx,tPy-lorgy,tPz-lorgz
			
		Norm=math.sqrt(zvx*zvx+ zvy*zvy)
		yaw=math.atan2(zvz, Norm)

		Turn(StriTable[number].UpLeg,y_axis,yaw,0,true)
Image
Attachments
TurnsWrong.jpg
TurnsWrong.jpg (9.66 KiB) Viewed 1144 times
User avatar
Peet
Malcontent
Posts: 4384
Joined: 27 Feb 2006, 22:04

Re: math.atan2

Post by Peet »

atan2(dz, dx), assuming the leg is aligned with the x axis
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10454
Joined: 24 Jan 2006, 21:12

Re: math.atan2

Post by PicassoCT »

Peet to the rescue.. unfortunatly its not working..

Is there something im missing.. some magic variable.. i m using rad.. so i thought everything was okay..

Image

Image

Image

Thanks to anybody investing his time.
Attachments
potter.jpg
(1.33 MiB) Downloaded 1 time
pattern.jpg
(1.27 MiB) Downloaded 1 time
this-shall-get-me-banned-fo.jpg
(1.27 MiB) Downloaded 1 time
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10454
Joined: 24 Jan 2006, 21:12

Re: math.atan2

Post by PicassoCT »

Solved:

Scholars of a future far - in order to turn a leg perfect towards the worldcoords, you need thus:

Code: Select all

	Turn(Leg,y_axis,math.rad(180)-(math.rad(currentHeadingInDeg)+math.rad(-90)+yaw),..
The +90 is to get math.atan2 to line up with the world coord system...
- currentHeadingInDeg undos the rotation of the unit
- yaw = math.atan2(worldz,worldx) (thanks to Peet)
the 180 i dont know why those are needed, but it works... its a lapsus of faith..

Thanks to everyone helpin
Post Reply

Return to “Lua Scripts”