vehicle.rotation setter/getter

Recommended Posts

Hello all, I've spent some time working with the rotation property of the vehicle object (server side of course) and I've worked out why things are "broken" with it... The setter requires x, y, z angles relative to the vehicle where the x is roll, y is pitch and z is heading. The getter returns the x, y, z angles relative to the world. That is, if the vehicle is facing south (z = 180), the setter and getter values are equal, but at any other heading, the getter object cannot be used as the setter. To solve this, I came up with the following function - I thought it might be helpful to others...

```const relativeRotation = (rotation) => {
const {x, y, z} = rotation;
const rx = (360-z) / 180 * Math.PI;
const ry = z / 180 * Math.PI;
return {
x: x * -Math.cos(rx) + y * Math.sin(rx),
y: y * -Math.cos(ry) + x * Math.sin(ry),
z
};
};```

Basic usage...

```
// get the rotation of the vehicle relative to the vehicle.
let {x, y, z} = relativeRotation(vehicle.rotation);

x += 45; // adjust the pitch of the vehicle by 45 degrees

// update the vehicle rotation.
vehicle.rotation = new mp.Vector3(x, y, z);```

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.