Rotate towards 3d vector

Extex
Member

Posts: 93
Joined: Wed Mar 14, 2018 23:14
GitHub: Extex101
In-game: Extex

Rotate towards 3d vector

How would rotate an entity to face a 3d vector?
I know how to do this in 2d but I don't know how to do it in a 3d world
Player of PK, RIP. Creator of the light_tool mod. Working on another mod which is pretty cool so far Krock
Developer

Posts: 4365
Joined: Thu Oct 03, 2013 07:48
Location: Switzerland
GitHub: SmallJoker

Re: Rotate towards 3d vector

Since the X axis is flipped in Minetest, you need to inverse this axis:
Code: Select all
yaw = math.atan2(-my_vector.x, my_vector.z) -- radians!

And pitch will be probably calculated similar to this:
Code: Select all
pitch = math.atan2(-my_vector.y, math.sqrt(my_vector.x^2 + my_vector.z^2))
roll = 0

I'm not familiar with the rotation calculations, so you might not need to invert the Y axis here. At least for the player view that's inverted as well.
>> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

Termos
Member

Posts: 182
Joined: Sun Dec 16, 2018 12:50

Re: Rotate towards 3d vector

Extex wrote:How would rotate an entity to face a 3d vector?

One possible solution

Code: Select all
function dir_to_rot(v,rot)
return {x = (v.x==0 and v.y==0 and v.z==0) and rot.x or math.atan2(v.y,vector.length({x=v.x,y=0,z=v.z})),
y = (v.x==0 and v.z==0) and rot.y or minetest.dir_to_yaw(v),
z=rot.z}
end

Current rotation is passed in case some components are zero, to avoid abruptly setting default yaw and pitch.

Also there's the case of collisions to consider, when direction changes abruptly.