This page has been translated automatically.
Video Tutorials
Interface
Essentials
Advanced
How To
UnigineEditor
Interface Overview
Assets Workflow
Settings and Preferences
Working With Projects
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Lighting
Landscape Tool
Sandworm
Using Editor Tools for Specific Tasks
Extending Editor Functionality
Built-in Node Types
Nodes
Objects
Effects
Decals
Light Sources
Geodetics
World Objects
Sound Objects
Pathfinding Objects
Players
Programming
Fundamentals
Setting Up Development Environment
UnigineScript
C++
C#
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine Tools
GUI
Double Precision Coordinates
API
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
Content Creation
Content Optimization
Materials
Art Samples
Tutorials
Warning! This version of documentation is OUTDATED, as it describes an older SDK version! Please switch to the documentation for the latest SDK version.
Warning! This version of documentation describes an old SDK version which is no longer supported! Please upgrade to the latest SDK version.

Using Manipulators to Transform Objects

After adding an object to the scene in Unigine Editor, you can control object transformations with control devices. But sometimes transformations are supposed to be made at application runtime. For example, when you create your own game with a world editor, where you can place objects in the world and move them around.

This usage example will teach you how to:

  1. Select an object on the screen with the mouse using Intersections.
  2. Use manipulator widgets to modify the object transform matrix.
  3. Switch between different manipulator widgets using the keyboard.

See Also#

Creating Manipulators to Control Object Transformations#


There are 3 types of manipulator widgets used for object transforming:


All these manipulators work the same way, each of them visually represents a part of the transformation matrix that you can change by dragging the control elements of the widget. Use the CHANGE callback of the widget to make the selected object follow manipulators transformations.

Source code (C#)
public class Manipulator : Component
{
	// declare object and translator widget manipulator variables
    Unigine.Node obj;
	WidgetManipulatorTranslator ObjManTranslator;
	
    private void Init()
    {
        // write here code to be called on component initialization
		
		// create a manipulator class instance and add it to the UI
        gui = Gui.Get();
        ObjManTranslator = new WidgetManipulatorTranslator(gui);
        gui.AddChild(ObjManTranslator);
		
		// setting the ApplyTransform method as a callback on changing the widget state
		ObjManTranslator.AddCallback(Gui.CALLBACK_INDEX.CHANGED, ApplyTransform);
    }
	
	// a callback method used to transform an object
	private void ApplyTransform()
	{
		if(obj != null)
			obj.WorldTransform = ObjManTranslator.Transform;
	}
}

Selecting Objects Using Mouse Cursor#

To select an object under the mouse cursor we should cast a ray from the cursor location in the view direction of the camera using the World.GetIntersection() method, that returns an intersected object (if any).

Source code (C#)
private Unigine.Node GetNoteUnderCursor()
{
	WorldIntersection intersection = new WorldIntersection();
	ivec2 mouse = Input.MouseCoord;
	
	// find a point 100 units away in front of the camera
	vec3 p0 = Game.Player.WorldPosition;
	vec3 p1 = p0 + new vec3(Game.Player.GetDirectionFromScreen(mouse.x, mouse.y)) * 100;
	
	// cast a ray, that will detect an object within 100 units in front of the camera and return it
	return World.GetIntersection(p0, p1, 1, intersection);
}

Putting it All Together#

Now let's put it all together and add a keyboard handler to switch the current manipulator. For example, let's use Z,X,C keys to select Translator, Rotator, Scaler Manipulators accordingly. Selected widget should be displayed on the screen where the object is located (have the same transformation).

  1. Create a new C# component - Manipulator. And double-click it in the Asset Browser to edit code in your IDE.
  2. Copy the code below and paste it to your component.
  3. Assign the Manipulator to any enabled node in the world and click Play.
Source code (C#)
// Manipulator.cs
public class Manipulator : Component
{
    // WorldIntersection object to store the information about the intersection 
    WorldIntersection intersection = new WorldIntersection();
 
    // Unigine.Object object to gain access to a selected object's transform in Unigine 
    Unigine.Node obj;
 
    // create manipulators for each type of transform(Translating, Scaling, Rotating) 
    WidgetManipulator ObjManTranslator;
    WidgetManipulator ObjManScaler;
    WidgetManipulator ObjManRotator;
	
	// create widget manipulator used to store transformation matrix of an active widget
	WidgetManipulator CurrentObjectManipulator;
    Gui gui;
 
    [Method(Order=2)] private void Init()
    {
        // write here code to be called on component initialization 
        gui = Gui.Get();

		// create widget for each transformation type
        ObjManTranslator = new WidgetManipulatorTranslator(gui);
        ObjManScaler = new WidgetManipulatorScaler(gui);
        ObjManRotator = new WidgetManipulatorRotator(gui);
		
		// set projection for the widgets
        ObjManTranslator.Projection= Game.Player.Camera.Projection;
        ObjManScaler.Projection = Game.Player.Camera.Projection;
        ObjManRotator.Projection = Game.Player.Camera.Projection;

		// add widgets to UI
        gui.AddChild(ObjManTranslator);
        gui.AddChild(ObjManScaler);
        gui.AddChild(ObjManRotator);

        // add a callback for each widget   
        ObjManTranslator.AddCallback(Gui.CALLBACK_INDEX.CHANGED, ApplyTransform);
        ObjManRotator.AddCallback(Gui.CALLBACK_INDEX.CHANGED, ApplyTransform);
        ObjManScaler.AddCallback(Gui.CALLBACK_INDEX.CHANGED, ApplyTransform);


		// hide created widgets
        ObjManScaler.Hidden = true;
        ObjManRotator.Hidden = true;
        ObjManTranslator.Hidden = true;
    }
 
    private void Update()
    {
        // write here code to be called before updating each render frame
         if (!Game.Player)
            return;
	
		// set modelview for each widget manipulator every frame
        ObjManTranslator.Modelview = Game.Player.Camera.Modelview;
        ObjManScaler.Modelview = Game.Player.Camera.Modelview;
        ObjManRotator.Modelview = Game.Player.Camera.Modelview;

        // select an object with right-click
		if (Input.IsMouseButtonDown(Input.MOUSE_BUTTON.RIGHT))
        {
            obj = GetNoteUnderCursor();
        }

		// choose manipulator with Z,X,C keys
		if(obj)
		{
			if(Input.IsKeyDown(Input.KEY.Z))
				SwitchManipulator(ObjManTranslator);
			if(Input.IsKeyDown(Input.KEY.X))
				SwitchManipulator(ObjManRotator);
			if(Input.IsKeyDown(Input.KEY.C))
				SwitchManipulator(ObjManScaler);
            CurrentObjectManipulator.Transform = obj.WorldTransform;
		}
    }
 
    // transform an object
	private void ApplyTransform()
	{
		if(obj != null)
			obj.WorldTransform = CurrentObjectManipulator.Transform;
	}
 
    // find an object under the cursor
    private Unigine.Node GetNoteUnderCursor()
    {
        ivec2 mouse = Input.MouseCoord;

        // find a point 100 units away in front of the camera
        vec3 p0 = Game.Player.WorldPosition;
        vec3 p1 = p0 + new vec3(Game.Player.GetDirectionFromScreen(mouse.x, mouse.y)) * 100;
		
        // cast a ray, that will detect an object within 100 units in front of the camera
        return World.GetIntersection(p0, p1, 1, intersection);
    }

	// relocate chosen manipulator to the position of selected object and make it visible
	void SwitchManipulator(WidgetManipulator CurrentManipulator)
	{
		// relocate a widget and making it visible
		CurrentManipulator.Transform = obj.WorldTransform;
		CurrentManipulator.Hidden = false;
		
		// make other widgets hidden
		CurrentObjectManipulator = CurrentManipulator;
		if(ObjManTranslator != CurrentManipulator)
			ObjManTranslator.Hidden = true;
		if(ObjManRotator != CurrentManipulator)
			ObjManRotator.Hidden = true;
		if(ObjManScaler != CurrentManipulator)
			ObjManScaler.Hidden = true;
	}
}
Last update: 2021-04-29
Build: ()