Topic: rotate the object?
hi all!
how to rotate the object in the direction of another entity only on the Y axis (lua)?
You are not logged in. Please login or register.
hi all!
how to rotate the object in the direction of another entity only on the Y axis (lua)?
You can use the look-at behavior :
- create an invisible object called "dummy"
- add a look-at behavior to your first object and set the target to the dummy
- in script, set the dummy X and Z position to be the X and Z position of the object you want to point at
- set the dummy Y position to 0 or to your first object Y position
Or use some math in pure lua, but it's more complicated (this example rotate on Z axis) :
-- get 2d vector length
function getLength2d(vec)
return math.sqrt(vec[1]*vec[1] + vec[2]*vec[2])
end
-- normalize 2d vector
function normalize2d(vec)
length = getLength2d(vec)
vec[1] = vec[1] / length
vec[2] = vec[2] / length
return vec
end
-- lookAt
function lookAt(object, dir, rotSpeed)
-- compute object X dir and Y dir
rot = getRotation(object)
zAngle = math.rad(rot[3])
sinZ = math.sin(zAngle)
cosZ = math.cos(zAngle)
YDir = normalize2d({sinZ, -cosZ})
XDir = {-YDir[2], YDir[1]}
-- dot products for orientation and angle
ori = (dir[1]*XDir[1] + dir[2]*XDir[2])
angle = math.acos(dir[1]*YDir[1] + dir[2]*YDir[2])
if ori < 0 then
angle = - angle
end
rotate(object, {0, 0, 1}, math.deg(angle) * rotSpeed)
end
Thanks anael! This is exactly what I need.
I have not function lookAt(). What am I doing wrong?
--onSceneUpdate()
dir = getPosition(object2)
lookAt(object1, dir, 1)
Last edited by ant0n (2014-07-07 09:44:16)
look-at is a behavior :
select your object in the editor > go to behavior tab > add a look-at behavior > set the name of the target
(except is you use the script I gave before, in this case copy/paste it on top of your script)
I don't use behavior, I use Lua function.
I copied all three functions:
function getLength2d(vec)
...
end
function normalize2d(vec)
...
end
function lookAt(object, dir, rotSpeed)
...
end
obj1 = getObject("obj1")
obj2 = getObject("obj2")
function onSceneUpdate()
dir = getPosition(obj2)
lookAt(obj1, dir, 1)
end
nothing happens.
obj1 = getObject("obj1")
obj2 = getObject("obj2")
function onSceneUpdate()
dir = normalize(getPosition(obj2) - getPosition(obj1))
lookAt(obj1, dir, 1)
end
Powered by PunBB, supported by Informer Technologies, Inc.
Currently installed 3 official extensions. Copyright © 2003–2009 PunBB.