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

## Rotate towards 3d vector

### Rotate towards 3d vector

Player of PK, RIP. Creator of the light_tool and motorbike mods. Working on another mod which should be coming soon!

- Krock
- Developer
**Posts:**4564**Joined:**Thu Oct 03, 2013 07:48**GitHub:**SmallJoker**Location:**Switzerland-
**Contact:**

### Re: Rotate towards 3d vector

Since the X axis is flipped in Minetest, you need to inverse this axis:
And pitch will be probably calculated similar to this:
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.

Code: Select all

`yaw = math.atan2(-my_vector.x, my_vector.z) -- radians!`

Code: Select all

```
pitch = math.atan2(-my_vector.y, math.sqrt(my_vector.x^2 + my_vector.z^2))
roll = 0
```

Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang:

*!mtmod <keyword here>*### Re: Rotate towards 3d vector

One possible solutionExtex wrote:How would rotate an entity to face a 3d vector?

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
```

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

### Who is online

Users browsing this forum: No registered users and 1 guest