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.

Creating and Attaching a Cloth

This example shows how to create a cloth pinned to 2 dummy bodies resting on a sphere using a particles joint.

In the AppWorldLogic.h file, define smart pointers for the objects of our scene.

Source code (C++)
// AppWorldLogic.h

#include <UnigineObjects.h>

/* .. */

class AppWorldLogic : public Unigine::WorldLogic {
	
public:
	/* .. */

private:
	Unigine::ObjectMeshDynamicPtr cloth;
	Unigine::ObjectMeshDynamicPtr sphere;
	Unigine::ObjectDummyPtr dummy1;
	Unigine::ObjectDummyPtr dummy2;
	Unigine::PlayerSpectatorPtr player;
};

Insert the following code into the AppWorldLogic.cpp file.

Notice
Unchanged methods of the AppWorldLogic class are not listed here, so leave them as they are.
Source code (C++)
// AppWorldLogic.cpp
/* .. */

#include "UnigineGame.h"
#include "UniginePrimitives.h"
#include "UnigineObjects.h"

using namespace Unigine;
using namespace Math;

/// function, creating a named sphere with a specified radius and color at pos	
ObjectMeshDynamicPtr createSphere(char *name, float radius, const vec4& color, const vec3& pos)
{
	// creating a sphere dynamic mesh
	ObjectMeshDynamicPtr OMD = Primitives::createSphere(radius);

	// setting parameters

	OMD->setMaterial("mesh_base", "*");
	OMD->setMaterialParameterFloat4("albedo_color", color, 0);
	OMD->setWorldTransform(Mat4(translate(pos)));
	OMD->setName(name);

	return OMD;
}

/// function, creating a named dummy body of a specified size at pos
ObjectDummyPtr createBodyDummy(char *name, const vec3& size, const vec3& pos)
{
	// creating a dummy object
	ObjectDummyPtr dummy = ObjectDummy::create();

	// setting parameters
	dummy->setWorldTransform(Mat4(translate(pos)));
	dummy->setName(name);

	//assigning a dummy body to the dummy object and adding a box shape	to it
	BodyDummy::create(dummy);
	dummy->getBody()->addShape(ShapeBox::create(size), translate(0.0f, 0.0f, 0.0f));

	return dummy;
}

/// function, creating a named cloth with specified parameters 
ObjectMeshDynamicPtr createBodyCloth(char *name, float width, float height, float step, float mass, float friction, float restitution, float rigidity, float lrestitution, float arestitution, int num_iterations, const vec4& color, const Mat4& tm)
{
	// creating a dynamic mesh object with a plane surface
	ObjectMeshDynamicPtr OMD = Primitives::createPlane(width, height, step);

	//assigning a cloth body to the dynamic mesh object and setting rope parameters
	BodyClothPtr body = BodyCloth::create(OMD);

	body->setMass(mass);
	body->setFriction(friction);
	body->setRestitution(restitution);
	body->setLinearRestitution(lrestitution);
	body->setAngularRestitution(arestitution);
	body->setRigidity(rigidity);
	body->setNumIterations(num_iterations);

	// setting object's parameters and transformation
	OMD->setWorldTransform(tm);
	OMD->setMaterial("mesh_base", "*");
	OMD->setMaterialParameterFloat4("albedo_color", color, 0);
	OMD->setName(name);

	return OMD;
}
			
/* .. */
	
int AppWorldLogic::init() {
		
	player = PlayerSpectator::create();
	
	player->setPosition(Vec3(30.0f, 0.0f, 30.5f));
	player->setDirection(vec3(-1.0f, 0.0f, -0.4f), vec3(0.0f, 0.0f, -1.0f));
	Game::setPlayer(player);

	cloth = createBodyCloth("MyCloth", 20.0f, 20.0f, 1.0f, 10.0f, 0.05f, 0.05f, 0.05f, 0.2f, 0.05f, 8, vec4(0.3f, 0.3f, 1.0f, 1.0f), Mat4(translate(vec3(0.0f, 0.0f, 25.0f))));

	// creating a sphere
	sphere = createSphere("MySphere", 3.0f, vec4(1.0f, 0.1f, 0.1f, 1.0f), vec3(-1.0f, 0.0f, 16.0f));

	// creating 2 dummy bodies to attach the cloth to
	dummy1 = createBodyDummy("fixpoint1", vec3(1.0f, 1.0f, 1.0f), vec3(-10.0f, -10.0f, 25.0f));
	dummy2 = createBodyDummy("fixpoint2", vec3(1.0f, 1.0f, 1.0f), vec3(-10.0f, 10.0f, 25.0f));

	// creating 2 particles joints to attach the cloth to dummy bodies
	JointParticles::create(dummy1->getBody(), cloth->getBody(), dummy1->getPosition(), vec3(1.0f));
	JointParticles::create(dummy2->getBody(), cloth->getBody(), dummy2->getPosition(), vec3(1.0f));

	return 1;
}

/* .. */
Last update: 2021-04-09
Build: ()