This page has been translated automatically.
Video Tutorials
Interface
Essentials
Advanced
How To
Professional (SIM)
UnigineEditor
Interface Overview
Assets Workflow
Settings and Preferences
Working With Projects
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Lighting
Sandworm
Using Editor Tools for Specific Tasks
Extending Editor Functionality
Built-in Node Types
Nodes
Objects
Effects
Decals
Light Sources
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
Programming
Fundamentals
Setting Up Development Environment
Usage Examples
C++
C#
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
Materials and Shaders
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
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
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.

Generating Physical Objects

We need some geometric primitives that the player can move around the Play Area and shoot at. Let's generate them via code.

A primitive should have a body and a shape to collide with the Player Character and be affected by gravity. We have implemented interaction with bullets via Intersections at the previous step, so we should set the BulletIntersection bit for the object's Intersection mask.

When the robot moves fast and the application framerate is low, the character will be teleported each frame and may go through physical objects due to discrete collision detection. To avoid this we are going to use continuous collision detection.

Notice
Continuous collision detection is available for sphere and capsule shapes only.
  1. Create a new C# component and call it ObjectGenerator. Copy the following code and save it in your IDE to ensure it's automatic compilation on switching back to UnigineEditor. This component generates the physical objects and places them in the world.

    ObjectGenerator.cs (C#)
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Unigine;
    
    [Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
    public class ObjectGenerator : Component
    {
    
    	private void Init()
    	{
    
    			// cube 
    			Mesh meshBox = new Mesh();
    			meshBox.AddBoxSurface("box_surface", new vec3(1.0f));
    			ObjectMeshStatic box = new ObjectMeshStatic(meshBox);
    
    			box.TriggerInteractionEnabled = true;
    			box.SetIntersectionMask(0x00000080, meshBox.FindSurface("box_surface")); // check the BulletIntersection bit
    			box.WorldTransform = MathLib.Translate(new vec3(0.5f, 7.5f, 1.0f));
    			box.SetMaterialPath("materials/mesh_base_0.mat", "*");
    			box.Name = "box";
    			BodyRigid bodyBox = new BodyRigid(box);
    			ShapeBox shapeBox = new ShapeBox(bodyBox, new vec3(1.0f));
    			new ShapeSphere(bodyBox, 0.5f);
    			bodyBox.ShapeBased = false;
    			bodyBox.Mass = 2.0f;
    
    			// sphere
    			Mesh meshSphere = new Mesh();
    			meshSphere.AddSphereSurface("sphere_surface", 0.5f, 9, 32);
    			ObjectMeshStatic sphere = new ObjectMeshStatic(meshSphere);
    
    			sphere.TriggerInteractionEnabled = true;
    			sphere.SetIntersectionMask(0x00000080, meshSphere.FindSurface("sphere_surface")); // check the BulletIntersection bit
    			sphere.WorldTransform = MathLib.Translate(new vec3(4.5f, 5.5f, 1.0f));
    			sphere.SetMaterialPath("materials/mesh_base_1.mat", "*");
    			sphere.Name = "sphere";
    			BodyRigid bodySphere = new BodyRigid(sphere);
    			new ShapeSphere(bodySphere, 0.5f);
    			bodySphere.ShapeBased = false;
    			bodySphere.Mass = 2.0f;
    
    			// capsule
    			Mesh meshCapsule = new Mesh();
    			meshCapsule.AddCapsuleSurface("capsule_surface", 0.5f, 1.0f, 9, 32);
    			ObjectMeshStatic capsule = new ObjectMeshStatic(meshCapsule);
    
    			capsule.TriggerInteractionEnabled = true;
    			capsule.SetIntersectionMask(0x00000080, meshCapsule.FindSurface("capsule_surface")); // check the BulletIntersection bit
    			capsule.WorldTransform = MathLib.Translate(new vec3(4.5f, 0.5f, 3.0f));
    			capsule.SetMaterialPath("materials/mesh_base_2.mat", "*");
    			capsule.Name = "capsule";
    			BodyRigid bodyCapsule = new BodyRigid(capsule);
    			new ShapeCapsule(bodyCapsule, 0.5f, 1.0f);
    			bodyCapsule.ShapeBased = false;
    			bodyCapsule.Mass = 2.0f;
    
    	}
    }
  2. Switch back to the UnigineEditor and create a new Dummy Node, rename it to "object_generator", and place it somewhere in the world.
  3. Attach the created ObjectGenerator component to the "object_generator" node to make it generate physical objects on the initialization.
  4. Create 3 new materials in the UnigineEditor by choosing Create->Create Material in the Asset Browser. Make them children of the mesh_base material, give them names as specified in the previous code snippet (mesh_base_*), and move them to the data/materials folder of your project (create this folder if it doesn't exist).

  5. Select materials one by one in the Materials window and in the Parameters window switch to the Parameters tab and click on the color field near Albedo to choose different colors for the objects via the color picker.

  6. Save changes to the world via Ctrl + S.
  7. Run the project via the UnigineEditor to see the spawned physical objects in the world.
Last update: 2022-12-14
Build: ()