# Rotate a Vector3

I have a project where I’m trying to connect to different components of a structure (space station).

Each component has an array of connectors with a position and rotation that should be applied to the connected component.

See the repro.

I think we need to know more precisely what you want to achieve?

Are you able to setup a simpler repro that would demonstrate what you want to do but not succeed?

https://playground.babylonjs.com/#N4WY2S#3 Demonstrates correct behavior with the I connecter attaching to the core, though this does not account for rotating the connecter points.

https://playground.babylonjs.com/#N4WY2S#4 Has a second I connecter attached to the first, though rather than the “pole” coming off of the core doubling in size (which is expected), the “pole” does not change in size.

This gets more confusing since connectors can have any rotation.

Essentially, what I want when the core has connectors 0 and 1 attached with I connectors:

For this, I need to offset the I connector position by the I connecter’s connecter position rotated by the core’s connecter rotation (not to mention rotating the I connecter by both rotations).

Hence, I need to rotate a given position vector (P) by a rotation vector (R). I’m looking to do something like `P.rotate(R)`.

Edit: This Stack Overflow post is very helpful.

I like using a matrix method sometimes too:

``````const direction = new Vector3(0, 0, 1);
const rotation = Quaternion.FromEulerAngles(0, Math.PI * 0.5, 0);
const rotMat = Matrix.Zero();
rotation.toRotationMatrix(rotMat);
const newDirection = Vector3.TransformCoordinates(direction, rotMat);
``````

If you are going to be doing a lot of rotations you dont have to make a new matrix Instance each time, you can cache it on your own script or use one of BJS temporary ones that they have already cached for you.

``````rotation.toRotationMatrix(BABYLON.TmpVectors.Matrix);
const newDirection = Vector3.TransformCoordinates(direction, BABYLON.TmpVectors.Matrix);
``````
2 Likes

Interesting…

https://playground.babylonjs.com/#N4WY2S#5 Seems to have the positions somewhat correct, though the rotations are incorrect

https://playground.babylonjs.com/#N4WY2S#6 Almost seems like it is correct though the position is still not rotated (they are all centered at -0.5 z)

https://playground.babylonjs.com/#N4WY2S#7 Swapping stuff did not fix it
https://playground.babylonjs.com/#N4WY2S#6 More swapping still did not fix it

Sounds like you are not getting the correct rotation vector then. You need to basically construct a lookAt rotation vector from the nodes origin to the connectors origin and use that as the rotation value.

4 Likes