Unigine.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.
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).
using Unigine;
namespace UnigineApp
{
class AppWorldLogic : WorldLogic
{
public override int init()
{
// create a mesh
Mesh mesh = new Mesh();
mesh.addBoxSurface("box_0", new vec3(1.0f));
// create a node (e.g. an instance of the ObjectMeshStatic class)
ObjectMeshStatic obj = new ObjectMeshStatic(mesh);
obj.release();
// assign a material to the node
obj.setMaterial("mesh_base", "*");
obj.setMaterialParameter("albedo_color", new vec4(1.0f, 0.0f, 0.0f, 1.0f), 0);
// create a pivot node
NodePivot pivot = new NodePivot();
pivot.release();
// add the static mesh to the pivot node as a child
pivot.addWorldChild(obj.getNode());
// add nodes to UnigineEditor
Editor.get().addNode(pivot.getNode());
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.
#if UNIGINE_DOUBLE
using Vec3 = Unigine.dvec3;
using Vec4 = Unigine.dvec4;
using Mat4 = Unigine.dmat4;
#else
using Vec3 = Unigine.vec3;
using Vec4 = Unigine.vec4;
using Mat4 = Unigine.mat4;
#endif
using Unigine;
namespace UnigineApp
{
class AppWorldLogic : WorldLogic
{
NodePivot pivot_0;
NodePivot pivot_1;
public override int init()
{
// create a mesh
Mesh mesh = new Mesh();
mesh.addBoxSurface("box_0", new vec3(0.7f));
// create a 1st node (e.g. an instance of the ObjectMeshStatic class)
ObjectMeshStatic object_0 = new ObjectMeshStatic(mesh);
// release script ownership
object_0.release();
// assign a material to the node
object_0.setMaterial("mesh_base", "*");
object_0.setMaterialParameter("albedo_color", new vec4(1.0f, 0.0f, 0.0f, 1.0f), 0);
// set node transformation
object_0.setWorldPosition(object_0.getWorldPosition() + new Vec3(2.0));
// create a 1st pivot node
pivot_0 = new NodePivot();
// release script ownership
pivot_0.release();
// set pivot node transformation
pivot_0.setWorldTransform(object_0.getWorldTransform() * MathLib.translate(new Vec3(-1.0,0.0,0.0)));
// add the 1st created node as a child to the pivot node
pivot_0.addWorldChild(object_0.getNode());
// create a 2nd node
ObjectMeshStatic object_1 = new ObjectMeshStatic(mesh);
// release script ownership
object_1.release();
// assign a material to the node
object_1.setMaterial("mesh_base", "*");
object_1.setMaterialParameter("albedo_color", new vec4(0.0f, 1.0f, 0.0f, 1.0f), 0);
// set node transformation
object_1.setWorldTransform(object_0.getWorldTransform() * MathLib.translate(new Vec3(1.0,0.0,0.0)));
// create a 2nd pivot node
pivot_1 = new NodePivot();
// release script ownership
pivot_1.release();
// set pivot node transformation
pivot_1.setWorldPosition(pivot_0.getWorldPosition() + new Vec3(1.0));
// add the 2nd created node as a child to the pivot node
pivot_1.addWorldChild(object_1.getNode());
// pass node ownership to UnigineEditor
Editor.get().addNode(pivot_0.getNode());
Editor.get().addNode(pivot_1.getNode());
// change the pivot point transformation of the 2nd pivot node
mat4 transform = pivot_1.getPivotTransform();
transform.setRotateX(45.0f);
pivot_1.setPivotTransform(pivot_1.getPivotTransform() * transform);
return 1;
}
// start of the main loop
public override int update()
{
float ifps = Game.get().getIFps();
float angle = ifps * 45.0f;
// set local transformation for the child nodes of the 1st pivot node
mat4 transform_0 = pivot_0.getLocalTransform();
transform_0.setRotateZ(angle);
pivot_0.setLocalTransform(pivot_0.getLocalTransform() * transform_0);
// set local transformation for the child nodes of the 2nd pivot node
mat4 transform_1 = pivot_1.getLocalTransform();
transform_1.setRotateZ(angle);
pivot_1.setLocalTransform(pivot_1.getLocalTransform() * transform_1);
return 1;
}
public override int shutdown()
{
// clear pointers
pivot_0.clearPtr();
pivot_1.clearPtr();
return 1;
}
}
}
To get position, rotation and scale components of transformation, use the decomposeTransform() method:
vec3 translate,scale;
quat rotate;
Unigine.Math.decomposeTransform(pivot.getPivotTransform(),translate,rotate,scale);
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).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.