Rotate towards 3d vector

Post Reply
User avatar
Extex
Member
Posts: 244
Joined: Wed Mar 14, 2018 23:14
GitHub: Extex101
In-game: Extex

Rotate towards 3d vector

by Extex » Post

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
Creator of jelys_pizzaria and motorbike, and player of persistent kingdoms. RIP

User avatar
Krock
Developer
Posts: 4649
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: Rotate towards 3d vector

by Krock » Post

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.
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

Termos
Member
Posts: 417
Joined: Sun Dec 16, 2018 12:50

Re: Rotate towards 3d vector

by Termos » Post

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests