The Rotate Vector patch can be used to find the forward vector of a 3D object in world space. This means you can find the direction an object is facing at a particular point in time and move the object forward in that direction.
This is very useful for animating objects commonly used in games, such as vehicles and characters.
Name | Description |
---|---|
Vector | The vector3 value that you want to apply the rotation to. |
Rotation | The degree you want to rotate the vector along the Z, Y and X axes. |
Name | Description |
---|---|
Rotated vector | The vector3 value with the rotation applied. |
In the example below, a 3D cone object is positioned at a slight angle and moves, over time, in the direction it's pointing (upwards).
We achieved this with the graph below:
Applying rotation
Just as a character in a game may be rotated slightly away from a 0,0,0 position before it moves forward, in our Patch Graph, we used the Loop Animation and bottom Transition patch to rotate the cone along its own z axis, by 30 degrees.
Returning the forward vector
We used the Rotate Vector patch in combination with the Local Transform patch, (which outputs the cone’s current rotation), to return the vector that points in the same direction as the cone.
Moving the object in the position it’s pointing towards
The remaining patches in the graph apply forward movement along the cone’s Y axis, positioning the cone over time in the direction it’s pointing.
The top Transition patch simply moves the cone from one point to another. The two Multiply patches calculate the correct speed and displacement for the object’s movement, based on it’s forward vector and progress over time.
The patch group below moves an object along its forward vector in a way that ensures it progresses at a consistent speed, unrelated to FPS, using the Delta Time and Rotate Vector patches.
The 3D object can be seen moving across the scene:
Equivalent functionality can be achieved in a script with the rotate()
method exposed by VectorSignal
objects.
The Rotate Vector Patch from the main Patch Graph shown above can be replaced with a rotated vector sent from a script:
// Load in the required modules const Scene = require('Scene'); const Reactive = require('Reactive'); const Patches = require('Patches'); (async function () { // Enables async/await in JS [part 1] // Locate the ConeObj object in the Scene const cone = await Scene.root.findFirst('ConeObj'); // Get the object's rotation const rotation = cone.transform.rotation; // Create a new vector with its forward direction along the y axis const vector = Reactive.vector(0, 1, 0); // Rotate the vector by the rotation quaternion const rotatedVector = vector.rotate(rotation); // Send the rotatedVector value to the Patch Editor Patches.inputs.setVector('rotatedVector', rotatedVector); })(); // Enables async/await in JS [part 2]
With the example script above, the patch graph would look like this: