Rotate towards 3d vector

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

Rotate towards 3d vector

by Extex » Tue Jun 04, 2019 00:38

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

User avatar
Posts: 4258
Joined: Thu Oct 03, 2013 07:48
Location: Switzerland
GitHub: SmallJoker

Re: Rotate towards 3d vector

by Krock » Tue Jun 04, 2019 19:35

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>

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

Re: Rotate towards 3d vector

by Termos » Thu Jun 06, 2019 19:15

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

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.

Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 1 guest