Hi, I’m trying to have an agent face the direction of movement. This should be a piece of cake with any good vector library. Any good vector library should have a function to create a rotation (quaternion) from a vector (say the velocity). The vector library in Defold doesn’t seem to have sucha function which is ridiculous. Or am I missing something?
go.set(".", “euler.z”, direction). Well then I have to make all the calculatations manually which is what I wanted to avoid with a simple function that creates a quaternion from a vector direction…
I’ve tried using vmath.quat_from_to() but I get weird results (sprite scales up and turns invisible):
Maybe this function doesn’t work like I expect it to. Note that sprite is facing up along the y-axis with 0 rotation (and thus has forward vector (0,1,0)).
I’m only setting the rotation if vmath.length(self.velocity) is greater than some small threshold but still getting that weird result. Not sure what you mean by “direction of the the object”.
Ok. What I meant was that I’d try using two vectors of length=1 as input to that function, I’m not sure if the function normalises the input vectors (I haven’t looked at the actual implementation).
Oh thanks that actually solved the issue. Bad of me to not normalize the vectors (the function is probably using a dot product or something).
The final working code:
if vmath.length(self.velocity) > threshold then
local rotation = vmath.quat_from_to(vmath.vector3(0,1,0), vmath.normalize(self.velocity))
go.set_rotation(rotation)
end
I’m glad it worked! I think we should clarify this in the documentation (we have some good changes to this docs coming up in the next release, not sure if this case is one of them though).