This page has been translated automatically.
Video Tutorials
Interface
Essentials
Advanced
How To
Rendering
Professional (SIM)
UnigineEditor
Interface Overview
Assets Workflow
Version Control
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
VR Development
Double Precision Coordinates
API
Animations-Related Classes
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
VR-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 in your IDE and call it ObjectGenerator. Copy the following code to the corresponding files and save the solution. This component generates the physical objects and places them in the world.

    ObjectGenerator.h (C++)
    #include <UnigineComponentSystem.h>
    #include <UnigineGame.h>
    
    #pragma once
    class ObjectGenerator : public Unigine::ComponentBase
    {
    
    public:
    	COMPONENT_DEFINE(ObjectGenerator, ComponentBase);
    	COMPONENT_INIT(init, 1); // 1st initialization order
    protected:
    	void init();
    };
    ObjectGenerator.cpp (C++)
    #include "ObjectGenerator.h"
    #include <UniginePrimitives.h>
    
    using namespace Unigine;
    
    REGISTER_COMPONENT(ObjectGenerator);
    
    void ObjectGenerator::init()
    {
    		// cube 
    		ObjectMeshDynamicPtr box = Primitives::createBox(Math::vec3(1.0f));
    		box->setTriggerInteractionEnabled(1);
    		box->setIntersection(1, 0);
    		box->setIntersectionMask(0x00000080, 0); // check the BulletIntersection bit
    		box->setWorldTransform(translate(Math::Vec3(0.5f, 7.5f, 1.0f)));
    		box->setMaterialPath("materials/mesh_base_0.mat", "*");
    		box->setName("box");
    		Unigine::BodyRigidPtr bodyBox = BodyRigid::create(box);
    		ShapeBoxPtr shapeBox = ShapeBox::create(bodyBox, Math::vec3(1.0f));
    		ShapeSphere::create(bodyBox, 0.5f);
    		bodyBox->setShapeBased(0);
    		bodyBox->setMass(2.0f);
    
    		// sphere
    		ObjectMeshDynamicPtr sphere = Primitives::createSphere(0.5f, 9, 32);
    		sphere->setTriggerInteractionEnabled(1);
    		sphere->setIntersection(1, 0);
    		sphere->setIntersectionMask(0x00000080, 0); // check the BulletIntersection bit
    		sphere->setWorldTransform(translate(Math::Vec3(4.5f, 5.5f, 1.0f)));
    		sphere->setMaterialPath("materials/mesh_base_1.mat", "*");
    		sphere->setName("sphere");
    		BodyRigidPtr bodySphere = BodyRigid::create(sphere);
    		ShapeSphere::create(bodySphere, 0.5f);
    		bodySphere->setShapeBased(0);
    		bodySphere->setMass(2.0f);
    
    		// capsule
    		ObjectMeshDynamicPtr capsule = Primitives::createCapsule(0.5f, 1.0f, 9, 32);
    		capsule->setTriggerInteractionEnabled(1);
    		capsule->setIntersection(1, 0);
    		capsule->setIntersectionMask(0x00000080, 0); // check the BulletIntersection bit
    		capsule->setWorldTransform(translate(Math::Vec3(4.5f, 0.5f, 3.0f)));
    		capsule->setMaterialPath("materials/mesh_base_2.mat", "*");
    		capsule->setName("capsule");
    		BodyRigidPtr bodyCapsule = BodyRigid::create(capsule);
    		ShapeCapsule::create(bodyCapsule, 0.5f, 1.0f);
    		bodyCapsule->setShapeBased(0);
    		bodyCapsule->setMass(2.0f);
    }
    Notice
    Do not change the size of an object after the creation. The physics doesn't support scaling nodes with Body attached.
  2. Build and run the solution in your IDE to generate a property file for the component.
  3. Switch back to the UnigineEditor and create a new Dummy Node, rename it to "object_generator", and place it somewhere in the world.
  4. Attach the created ObjectGenerator component to the "object_generator" node to make it generate physical objects on the initialization.
  5. Create 3 new materials in the UnigineEditor: in the Materials window, right-click the mesh_base material and choose Create Child. By default, the created materials are named as specified in the previous code snippet (mesh_base_*). Move the materials to the data/materials folder of your project (create this folder if it doesn't exist).

  6. 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.

  7. Save changes to the world via Ctrl + S.
  8. Run the project in your IDE to see the spawned physical objects in the world.
Last update: 2023-12-19
Build: ()