Topic: look at / distance

hi,
how to rotate an object towards another?
how to calculate the distance between two objects?
I did not find lua functions for this

Re: look at / distance

Hi,

for the look-at, you can use the look-at behavior (in the behavior tab), just write the name of the target.

To find the distance between objects in lua you can do :

pos1 = getPosition(obj1) -- use getTransformedPosition instead of getPosition if the objects are parented
pos2 = getPosition(obj2)
distance = length(pos2 - pos1)

"3d math" from the wiki : http://wiki.maratis3d.org/index.php?tit … tions_list

Re: look at / distance

hi,anael thanks

which of those 3D math lua commands I use to calculate the delta yaw angle between two objects?

Last edited by marval (2013-08-06 15:56:34)

Re: look at / distance

If you want to code a 2d rotation (around the angle Z only) you can use some arithmetic :

-- 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

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

For a true 3d look-at, it's more complicated, you need to use a dot product to find the angle between the objects
and a cross product to find the rotation axis. It's better you use the look-at behavior directly.

Re: look at / distance

are you using lua 3D math in the 2D rotation example?
for true 3D using look-at behaviour directly does not show me the delta yaw angle that I need to define "aiming acurracy range"

Last edited by marval (2013-08-07 12:07:44)

Re: look at / distance

- the 2D rotation example only uses basic lua math (math.sqrt, math.sin etc).

- I don't understand what you mean by "aiming acurracy range" or delta yaw angle.

Re: look at / distance

delta yaw angle
how much (remaining amount/angle) an object should be rotated by in order to face completely the other object
and knowing this angle I can define a accuracy range so that the enemy/cpu can shoot the player

Last edited by marval (2013-08-07 16:42:58)

Re: look at / distance

You can use a dot product between two normalized vectors :
http://images.tutorvista.com/cms/images/83/dot-product-image.PNG

ex :

pos1 = getPosition(object1) -- player
pos2 = getPosition(object2) -- enemy

-- get the direction of object2 if it's face is oriented toward the Y axis
dir2 = getRotatedVector(object2, vec3(0, 1, 0))

-- get the vector between object1 and object2
vec = pos1 - pos2

-- find the angle between dir2 and vec
dotValue = dot(normalize(dir2), normalize(vec)) -- dot product returns the cosinus of the angle
angle = math.acos(dotValue) -- get the angle in radian
angle = math.deg(angle) -- convert to degree

Re: look at / distance

ok thanks
but I still need pass the current target (the closest enemy) to each turret in the lua script
the only way to do this is using the look-at behavior attached to each turret?

Last edited by marval (2013-08-07 18:13:00)

Re: look at / distance

the Look at behaviour does not work for a turret game
the behaviour does not rotate the object gradually he just points instantly
which prevents an turret just start spinning gradually by the shortest path and only when the target comes close to a distance range

Re: look at / distance

a look-at cannot operate gradually,
but the target yes :

- create an invisible box named "TurretTarget"
- attach a "Follow" behavior with a delay (for gradually moving)
- make your turret look-at target be "TurretTarget"

From script, change the target of "TurretTarget" depending on what the turret is supposed to look-at :
setBehaviorVariable(TurretTarget, 0, "target", "ObjectNameToLookAt")

Re: look at / distance

hi,
would be very useful if you include an similar command as the other game engines

angle = Mathf.DeltaAngle(turretPos, targetPos)
rotate(turret, {x, y, z}, angle, "local")

Unity
Mathf.DeltaAngle(curPos, targetPos);
Calculates the shortest difference between two given angles
leadwerks
Math::DeltaAngle
This function returns the shortest distance between the two angles, relative to the first angle.

Last edited by marval (2013-08-18 02:35:16)

Re: look at / distance

Hi, I'm trying to use that function which you have written (Anael) to make objects look only using Z-angle. but I'm not sure what to put in dir, I put in a vector, when I ran it it says: attempt to index local dir (a number value)

What's up?

Re: look at / distance

It was this one:

Z-axis rotation:

Edit: Just a tweak, http://pastebin.com/p6E1Fp3q

Last edited by Pär (2013-09-17 21:21:51)