Creating a "look at" node set up


#1

I am trying to figure out how to set up a “look at” with nodes. What I am trying to accomplish is the purple/blue box to point at the green box no matter where is moves. The purple/blue box should just rotate on the z axis so that it could “fire” on the green box. For this scenario the purple/blue box will be stationary. I guess I don’t understand the vector math or the “Look At” node well enough to make this work. Also, What axis (+y, -y, +x,-x) should the front (extending part of the purple/blue box) be on.

If any one can explain how the Look at node works in better detail that may be helpful also.

(edit)
I just noticed the Look node. However, I don’t understand it any better then the Look At. Would this be the way to go?

Thanks


#2

Anyone have even a guess?


#3

There’s a node for implementing haxe scripts, I’m pretty sure, so you could just once write this and then forget about the code, just drag your node in place again.
(in psuedocode because I’m remarkably lazy)

locOther=ob2.loc//the green box
locMine=ob.loc//the purple box
dif=locOther-locMine
max=math.sqrt(dif.x*dif.x+dif.y*dif.y+dif.z*dif.z)
dif.x/=max
dif.y/=max
dif.z/=max
angleX=math.ArcCos(dif.y)
angleY=math.ArcCos(dif.z)
angleZ=math.ArcCos(dif.x)
ob.setRotation(angleX,angleY,angleZ)

for example, assuming my memory of high school math is correct. This would obviously only work for one-eighth of three dimensional space, you have to use some if statements to decide when to inverse the values.

Alternatively write it in python and save it forever, you can copy it to other projects.


#4

I edited my response to be more accurate


#5

We should get this a permanent logic node


#6

If you only want to rotate around the z axis, this should work (at least accordgin to my testing):

First get the location of both objects (a cone and an icosphere in my case) and remove their z component, then subtract the position vectors and normalize the result, so you get the direction.

Then calculate the z angle. It is the angle between the x axis and our direction and can be calculated like this.


This setup might create a little hickup in the beginning, because for the perfect value of y = 0 neither floor nor ceil will provide an output. However this should not be that much of a problem when running the game, as the perfect 0 should never be created by any player driven character. If it is, one could just add a minimal value like 0.0001 to the y value before entering the floor / ceil nodes (which still would not remove the problem entirely but make it much more uncommon). A true fix would be to set it to +/-1 if the y value is exactly 0.

Finally, just apply the rotation.
image

(just to avoid any risc of misunderstanding, the output of each screenshot is the input of the next one)


#7

There is a “Look At” node, but I don’t know what it is doing exactly. From its UI looks it seems like it outputs the rotation an object that sits at the first vector looking at the second one, but its code seems like it outputs a quaternion which you can’t simply plug into a “Set Rotation” node, and also if I plug it into a print node the output doesn’t seem to be correct. I expected 4 floats, not this:

PrintNode.hx:12: {
name : ,
outputs : [[{
name : ,
outputs : [<…>],
inputs : [<…>,<…>],
tree : {
paused : <…>,
loopBreak : <…>,
_render2D : null,
_render : null,
_lateUpdate : null,
_update : <…>,
_remove : null,
init : null,
add : null,
name : <…>,
hx__closures
: <…>,
object : <…>
}
}]],
inputs : [{
node : {
name : ,
outputs : [<…>],
inputs : [<…>],
tree : {
paused : <…>,
loopBreak : <…>,
_render2D : null,
_render : null,
_lateUpdate : null,
_update : <…>,
_remove : null,
init : null,
add : null,
name : <…>,
hx__closures
: <…>,
object : <…>
}
},
from : 0
},{
node : {
name : ,
outputs : [<…>],
inputs : [<…>],
tree : {
paused : <…>,
loopBreak : <…>,
_render2D : null,
_render : null,
_lateUpdate : null,
_update : <…>,
_remove : null,
init : null,
add : null,
name : <…>,
hx__closures
: <…>,
object : <…>
}
},
from : 0
}],
tree : {
paused : false,
loopBreak : false,
_render2D : null,
_render : null,
_lateUpdate : null,
_update : [,],
_remove : null,
init : null,
add : null,
name : NodeTree_001,
hx__closures
: {
20 :
},
object : Mesh Object Plane
}
}


#8

@Simonrazer - I found the same thing, I just didn’t understand what I was looking at when I did it or why I was getting such a weird outcome of plugging the look at node into set rotation. I guess we need a quat to vector node.


#9

I was testing around a little. And yes the LookAt node does output a quat.
However there is currently no way (that I am aware of) to go from Quat -> Euler in nodes. You cannot even seperate the Quat.
In the logic pack, there is a Seperate Quat node, but

  • it seems to be incompatible with upstream armory
  • it requests a transform, not a quat.

After modifying the node, I was easily able to make a cone look at me, whereever I was using this setup

However, I think there still are some issues with this:

  • LookAt always uses the Z axis, so if you arrange your objects like this in Blender:
    image
    This will be the result:
    image
    As you can see the tip is pointing towards me, although it is the upwards facing part of the cone.
  • When walking around the cone, it will always look at me, but rotate around its local z axis.

Changing the LookAt nodes source to use the x axis improved this somewhat, so I guess adding a “facing direction” input to the LookAt node would be a good idea.
Also, if that node would directly output an Euler vector instead of / alongside a Quat, one would not even need the SeperateQuat node.

So that’s what I’ve found out for now. I will push the new SeperateQuat node to the logic_pack. As for the LookAt node, I think adding a facing input and euler output would be a good idea, but I might have just misunderstood the concept of the node.


#10

Thank you very much for your work on this. I believe this is one of those things that will be a big addition/improvement for many games. Looking at your enemy is a pretty standard part of any tower defense and many other games.


#11

Armory will need a LookAt that works for very situation,with constraints axis options also.


#12

I just created a new pull request for the logic pack, which features a new Looking at node, which

  • expects two objects
  • expects a main rotation axis (most likely the z axis)
  • expects a face vector (most likely the x axis) (This is not intended to change during the game, I do not recommend using a vector from transform node) (actually this is the face vector in local coordinates, and thus most likely the x axis)

image
image

It rotates the first object to look at the second one.
First it rotates around the main axis (like your head looking left/right) and then around a secondary axis defined by the cross product of the main axis and the position-difference vector (think of looking up/down).
Looking into constraints now.


#13

This is awesome. Can’t wait to give it a go.