Unigine::NodePivot Class
Header: | #include <UnigineNodes.h> |
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 "AppWorldLogic.h"
#include <UnigineNodes.h>
#include <UnigineEditor.h>
#include <UnigineMesh.h>
#include <UnigineObjects.h>
using namespace Unigine;
using namespace Math;
int AppWorldLogic::init() {
// create a mesh
MeshPtr mesh = Mesh::create();
mesh->addBoxSurface("box_0", vec3(1.0f));
// create a node (e.g. an instance of the ObjectMeshStatic class)
ObjectMeshStaticPtr object = ObjectMeshStatic::create(mesh);
object->release();
// 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
NodePivotPtr pivot = NodePivot::create();
pivot->release();
// add the static mesh to the pivot node as a child
pivot->addWorldChild(object->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.
#include "AppWorldLogic.h"
#include <UnigineNodes.h>
#include <UnigineEditor.h>
#include <UnigineMesh.h>
#include <UnigineObjects.h>
#include <UnigineGame.h>
#include <UnigineLog.h>
using namespace Unigine;
using namespace Math;
NodePivotPtr pivot_0;
NodePivotPtr pivot_1;
int AppWorldLogic::init() {
// create a mesh
MeshPtr mesh = Mesh::create();
mesh->addBoxSurface("box_0", vec3(0.7f));
// create a 1st node (e.g. an instance of the ObjectMeshStatic class)
ObjectMeshStaticPtr object_0 = ObjectMeshStatic::create(mesh);
// release script ownership
object_0->release();
// 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
pivot_0 = NodePivot::create();
// release script ownership
pivot_0->release();
// 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->getNode());
// create a 2nd node
ObjectMeshStaticPtr object_1 = ObjectMeshStatic::create(mesh);
// release script ownership
object_1->release();
// 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_1->getWorldTransform() * translate(Vec3(1.0, 0.0, 0.0)));
// create a 2nd pivot node
pivot_1 = NodePivot::create();
// release script ownership
pivot_1->release();
// 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->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;
}
int AppWorldLogic::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;
}
int AppWorldLogic::shutdown() {
// clear pointers
pivot_0.clear();
pivot_1.clear();
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 NodePivotPtr create ( ) #
Creates a pivot node (with the pivot and local transformation matrices equal to mat4_identity).static Ptr<NodePivot> cast ( const Ptr<Node> & node ) #
Casts a NodePivot out of the Node instance.Arguments
- const Ptr<Node> & node - Pointer to Node.
Return value
Pointer to NodePivot.void setLocalTransform ( const Math::mat4 & transform ) #
Sets a matrix that controls local transformations of child nodes.Arguments
- const Math::mat4 & transform - Local transformation matrix.
Math::mat4 getLocalTransform ( ) #
Returns the current matrix used to control local transformations of the child nodes.Return value
Local transformation matrix.void setPivotTransform ( const Math::mat4 & transform ) #
Sets a matrix that controls the basis of rotation of the child nodes (the pivot point transformation matrix).Arguments
- const Math::mat4 & transform - Pivot transformation matrix.
Math::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
Local transformation 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)