Getting the position and direction of a rigidbody


I’m new to nphysics so apologies if this is a silly question. I have a large height map which I’m rendering with opengl (in 3d) and so far have managed to get a ball rolling around the terrain. I’m able to get the position of the body with .position.translation.vector and set the position of my ‘camera’ so that I can see the ball is moving correctly, although it looks like it’s sliding around because the camera always faces the same way.

The next thing I’m going to do is make a simple vehicle and will need to get a matrix from the body of the vehicle to use as my view matrix in opengl, so the view is from the ‘front’ of the vehicle.

How do I get either the matrix OR a direction vector (I already have the position)? I hope that made sense!



If you want the position+orientaton of the vehicle as a 4x4 matrix, then you can do body.position.to_homogeneous() (the 4x4 matrix representation of an isometry are called its homogeneous coordinates).

Hi, and thanks for your reply. That’s exactly what I needed.

But now I’ve found that the cgmath::matrix4 I’m using for my projection matrix can’t be multiplied by the to_homogeneous() matrix. I’m fairly new to Rust so my silly workaround was to copy each value from the isometry matrix to the cgmath matrix4, like this:

matrix_2[0][0] = matrix_1.m11;

…and so on. It ‘kind of’ worked in that the rotation part is working great, but the position of the camera is now going high above and far below the terrain in a huge circle as the body rotates, as if it’s doing the rotation before the translation.

I’ll see if I can work out what I’m doing wrong. Is there a way to get the rotation as a vector if that makes sense? I know how to get the position.translation.vector. If I can get a ‘front’ vector I can use that in my camera struct along with the translation.

Thanks again for your help, sebcrozet. I’m using game dev to learn Rust and loving it. I’ll switch to Rapier eventually but just trying to get a simple vehicle with suspension working in Nphysics for now.