Page 1 of 1

Rotate towards 3d vector

Posted: Tue Jun 04, 2019 00:38
by Extex
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

Re: Rotate towards 3d vector

Posted: Tue Jun 04, 2019 19:35
by Krock
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.

Re: Rotate towards 3d vector

Posted: Thu Jun 06, 2019 19:15
by Termos
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.