Page 1 of 1

unit scripting -> relative rotation and movement

Posted: 08 May 2011, 17:26
by MergeNine
For lua unit scripting, should piece movement/rotation be relative to their local rotation rather than the 'world' axes? Or is there a separate way to script units to rotate or move relative to a normal determined within the script?

So you can see what I mean I've created a small experiment with a bit of script below, modifying a turret script (the armllt) so that it rotates along the y axis and the x axis then moves its barrel. I'd expect the barrel to move straight relative to it's new found rotation rather than along the z axis.

function script.Create()

c = 0
while ( c < 200) do --for testing

Turn(turret, y_axis, math.rad(-45), 0.5)
Turn(barrel, x_axis, -math.rad(-30), 0.25)
WaitForTurn(turret, y_axis)
WaitForTurn(barrel, x_axis)
Turn(turret, y_axis, math.rad(45), 0.5)
Turn(barrel, x_axis, -math.rad(30), 0.25)
WaitForTurn(turret, y_axis)
WaitForTurn(barrel, x_axis)
Sleep(100)
Move(barrel,z_axis, -5, 100)
WaitForMove(barrel, z_axis)
Move(barrel,z_axis, 5, 60)
WaitForMove(barrel, z_axis)
Sleep(500)
c = c +1
end
end

Re: unit scripting -> relative rotation and movement

Posted: 09 May 2011, 11:42
by Tobi
Use code tags for code! Also I'm moving this to content creation/games & mods.
---

Movement of each piece is relative to its own coordinate system. (Which is, in turn, relative to its parents coordinate system, etc.)

Further, if I recall correctly, translation is applied before rotation, which would explain why your barrel moves along its original z-axis, rather than its rotated z-axis.

Rotation always happens around the origin of the local coordinate system, so if you translate first the piece may rotate around a point outside the piece.

To fix your issue introduce a (invisible) piece between barrel and turret. Then, rotating this piece will rotate the whole coordinate system for the barrel, so that moving the barrel along its z-axis will look fine.

I hope I got this correct as I can't check right now :-)

Re: unit scripting -> relative rotation and movement

Posted: 09 May 2011, 19:04
by FLOZi
Sounds right to me Tobi.

Re: unit scripting -> relative rotation and movement

Posted: 10 May 2011, 00:16
by MergeNine
Brilliant, thanks I'll give that a shot.

:)