NodePivot Class
Inherits: | Node |
This class adds a pivot node that can serve as a pivot point for its children and allows for easy control over their transformation. It has two matrices: one sets the basis of rotation (the pivot point transformation matrix), and another one describes local transformation for child nodes relative to the pivot point.
It has virtually zero influence on performance and shares all common options that other nodes have.
Creating a Pivot Node#
To create a pivot node, perform as follows:
- Create an instance of the NodePivot class.
- Add child nodes to the pivot node.
- Release script ownership so that the node can be added to UnigineEditor.
- Add the node with its children to UnigineEditor (wherein, node ownership will be passed to the editor automatically).
#include <core/unigine.h>
int init() {
// create a mesh
Mesh mesh = new Mesh();
mesh.addBoxSurface("box_0", vec3(1.0f));
// create a node (e.g. an instance of the ObjectMeshStatic class)
ObjectMeshStatic object = class_remove(new ObjectMeshStatic(mesh));
// assign a material to the node
object.setMaterial("mesh_base", "*");
object.setMaterialParameter("albedo_color", vec4(1.0f, 0.0f, 0.0f, 1.0f), 0);
// create a pivot node
NodePivot pivot = class_remove(new NodePivot());
// add the static mesh to the pivot node as a child
pivot.addWorldChild(object);
// add nodes to UnigineEditor
engine.editor.addNode(pivot);
return 1;
}
Editing a Pivot Node#
Editing the pivot node includes changing its pivot and local transformation matrices. The example below performs as follows:
- Create a first node and set its transformation.
- Create a first pivot node and add the previously created node to it as a child.
- Perform the same for the second pivot node and its child node.
- Change pivot point transformation of the second pivot node.
- In the main loop, on the engine update, change local transformations of the child nodes.
In the result, child nodes will have different trajectories of movement, as the pivot nodes have different pivot point transformations.
#include <core/unigine.h>
NodePivot pivot_0;
NodePivot pivot_1;
int init() {
// create a mesh
Mesh mesh = new Mesh();
mesh.addBoxSurface("box_0", vec3(0.7f));
// create a 1st node (e.g. an instance of the ObjectMeshStatic class) and release script ownership
ObjectMeshStatic object_0 = class_remove(new ObjectMeshStatic(mesh));
// assign a material to the node
object_0.setMaterial("mesh_base", "*");
object_0.setMaterialParameter("albedo_color", vec4(1.0f, 0.0f, 0.0f, 1.0f), 0);
// set node transformation
object_0.setWorldPosition(object_0.getWorldPosition() + Vec3(2.0));
// create a 1st pivot node and release script ownership
pivot_0 = class_remove(new NodePivot());
// set pivot node transformation
pivot_0.setWorldTransform(object_0.getWorldTransform() * translate(Vec3(-1.0,0.0,0.0)));
// add the 1st created node as a child to the pivot node
pivot_0.addWorldChild(object_0);
// create a 2nd node and release script ownership
ObjectMeshStatic object_1 = class_remove(new ObjectMeshStatic(mesh));
// assign a material to the node
object_1.setMaterial("mesh_base", "*");
object_1.setMaterialParameter("albedo_color", vec4(0.0f, 1.0f, 0.0f, 1.0f), 0);
// set node transformation
object_1.setWorldTransform(object_0.getWorldTransform() * translate(Vec3(1.0,0.0,0.0)));
// create a 2nd pivot node and release script ownership
pivot_1 = class_remove(new NodePivot());
// set pivot node transformation
pivot_1.setWorldPosition(pivot_0.getWorldPosition() + Vec3(1.0));
// add the 2nd created node as a child to the pivot node
pivot_1.addWorldChild(object_1);
// pass node ownership to UnigineEditor
engine.editor.addNode(pivot_0);
engine.editor.addNode(pivot_1);
// change the pivot point transformation of the 2nd pivot node
pivot_1.setPivotTransform(pivot_1.getPivotTransform() * rotateX(45.0f));
return 1;
}
int update() {
float ifps = engine.game.getIFps();
float angle = ifps * 45.0f;
// set local transformation for the child nodes of the 1st pivot node
pivot_0.setLocalTransform(pivot_0.getLocalTransform() * rotateZ(angle));
// set local transformation for the child nodes of the 2nd pivot node
pivot_1.setLocalTransform(pivot_1.getLocalTransform() * rotateZ(angle));
return 1;
}
See Also#
- Article on the Pivot node
NodePivot Class
Members
static NodePivot ( ) #
Creates a pivot node (with the pivot and local transformation matrices equal to mat4_identity).static NodePivot cast ( Node node ) #
Casts a NodePivot out of the Node instance.Arguments
- Node node - Node instance.
Return value
NodePivot instance.void setLocalTransform ( mat4 transform ) #
Sets a matrix that controls local transformations of child nodes.Arguments
- mat4 transform - Local transformation matrix.
mat4 getLocalTransform ( ) #
Returns the current matrix used to control local transformations of the child nodes.Return value
Local transformation matrix.void setPivotTransform ( mat4 transform ) #
Sets a matrix that controls the basis of rotation of the child nodes (the pivot point transformation matrix).Arguments
- mat4 transform - Pivot point matrix.
mat4 getPivotTransform ( ) #
Returns the current transformation matrix used to control the basis of rotation of the child nodes (the pivot point transformation matrix).Return value
Pivot point matrix.static int type ( ) #
Returns the type of the node.Return value
NodePivot type identifier.Last update:
2019-08-16
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)