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

Creating and Attaching a Cloth

This example shows how to create a cloth pinned to two dummy bodies using the particles joints. The cloth falls down and rests on a sphere, which illustrates the cloth ability to change its form.

Falling cloth

To implement this, we need to perform the following:

  • Create a cloth and set its parameters.
  • Create two Dummy Objects that can hold the cloth. They also should have a body so that we can connect a cloth to them using joints.
  • Create a sphere that is not a physical body (so that it would not drop and roll away), but is a collider, so our cloth will not fall through.

We are going to create functions for each participant of the example and then use these functions in init() to create the required objects.

Creating a Cloth#

We create a plane that will represent a cloth — the Dynamic Mesh should be used to allow the cloth change at run time. Here we use the Primitives class to create a plane. You can also try to use your mesh instead, just make sure it complies with the triangulation requirements.

Then we assign the Cloth body to the created Dynamic Mesh and specify the required Cloth body parameters (mass, friction, restitution, etc) and the object parameters (transformation, color, name).

The whole function is as follows:

Source code (C#)
ObjectMeshDynamic createBodyCloth(String name, float width, float height, float step, float mass, float friction, float restitution, float rigidity, float lrestitution, float arestitution, int num_iterations, vec4 color, Mat4 tm)
{
	// creating a dynamic mesh object with a plane surface
	ObjectMeshDynamic OMD = Primitives.CreatePlane(width, height, step);

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

	body.Mass = mass;
	body.Friction = friction;
	body.Restitution = restitution;
	body.LinearRestitution = lrestitution;
	body.AngularRestitution = arestitution;
	body.Rigidity = rigidity;
	body.NumIterations = num_iterations;

	// setting object's parameters and transformation
	OMD.WorldTransform = tm;
	OMD.SetMaterialParameterFloat4("albedo_color", color, 0);
	OMD.Name = name;

	return OMD;
}

Using this function we create a cloth in the init() method.

Hanging the Cloth in the Air#

We need to fix one edge of the cloth in the air, otherwise it will unpredictably drop down, and there might be no contact with the sphere at all. This task is easily solved by using Dummy Object. We also assign the Dummy body to it, as joints may connect bodies only.

Source code (C#)
ObjectDummy createBodyDummy(String name, Vec3 size, Vec3 pos)
{
	// creating a dummy object
	ObjectDummy dummy = new ObjectDummy();

	// setting parameters
	dummy.WorldTransform = new Mat4(MathLib.Translate(pos));
	dummy.Name = name;

	//assigning a dummy body to the dummy object
	new BodyDummy(dummy);

	return dummy;
}

Using this function we create two points in the init() method and pin the cloth to them using the Particles joint:

Source code (C#)
// creating 2 dummy bodies to which the cloth will be attached
dummy1 = createBodyDummy("fixpoint1", new Vec3(1.0f, 1.0f, 1.0f), new Vec3(-10.0f, -10.0f, 25.0f));
dummy2 = createBodyDummy("fixpoint2", new Vec3(1.0f, 1.0f, 1.0f), new Vec3(-10.0f, 10.0f, 25.0f));

// creating 2 particles joints to attach the cloth to dummy bodies
new JointParticles(dummy1.Body, cloth.Body, dummy1.Position, new vec3(1.0f));
new JointParticles(dummy2.Body, cloth.Body, dummy2.Position, new vec3(1.0f));

Creating a Sphere#

A sphere is used as a prop to demonstrate how the cloth would cover it, thus there's no need to make it physical. We only need to enable collision for its surface, otherwise the cloth would pass through the sphere without any interaction.

Source code (C#)
ObjectMeshDynamic createSphere(String name, float radius, vec4 color, Vec3 pos)
{
	// creating a sphere dynamic mesh
	ObjectMeshDynamic OMD = Primitives.CreateSphere(radius);

	// setting parameters
	OMD.SetMaterialParameterFloat4("albedo_color", color, 0);
	OMD.WorldTransform = new Mat4(MathLib.Translate(pos));
	OMD.Name = name;
	OMD.SetCollision(true,0);

	return OMD;
}

Using this function we create a sphere in the init() method.

Code Sample#

The complete code sample is provided below.

Source code (C#)
using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;

#region Math Variables
#if UNIGINE_DOUBLE
using Scalar = System.Double;
using Vec3 = Unigine.dvec3;
using Mat4 = Unigine.dmat4;
#else
using Scalar = System.Single;
using Vec3 = Unigine.vec3;
using Mat4 = Unigine.mat4;
#endif
#endregion

[Component(PropertyGuid = "AUTOGENERATED_GUID")] // <-- this line is generated automatically for a new component
public class ClothParticlesJoint : Component
{
	ObjectMeshDynamic cloth;
	ObjectMeshDynamic sphere;
	ObjectDummy dummy1;
	ObjectDummy dummy2;
	PlayerSpectator player;
	
	// method, creating a named sphere with a specified radius and color a specified position	

	ObjectMeshDynamic createSphere(String name, float radius, vec4 color, Vec3 pos)
	{
		// creating a sphere dynamic mesh
		ObjectMeshDynamic OMD = Primitives.CreateSphere(radius);

		// setting parameters
		OMD.SetMaterialParameterFloat4("albedo_color", color, 0);
		OMD.WorldTransform = new Mat4(MathLib.Translate(pos));
		OMD.Name = name;
		OMD.SetCollision(true,0);

		return OMD;
	}

	// method, creating a named dummy body of a specified size a specified position

	ObjectDummy createBodyDummy(String name, Vec3 size, Vec3 pos)
	{
		// creating a dummy object
		ObjectDummy dummy = new ObjectDummy();

		// setting parameters
		dummy.WorldTransform = new Mat4(MathLib.Translate(pos));
		dummy.Name = name;

		//assigning a dummy body to the dummy object
		new BodyDummy(dummy);

		return dummy;
	}

	// method, creating a named cloth with specified parameters 

	ObjectMeshDynamic createBodyCloth(String name, float width, float height, float step, float mass, float friction, float restitution, float rigidity, float lrestitution, float arestitution, int num_iterations, vec4 color, Mat4 tm)
	{
		// creating a dynamic mesh object with a plane surface
		ObjectMeshDynamic OMD = Primitives.CreatePlane(width, height, step);

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

		body.Mass = mass;
		body.Friction = friction;
		body.Restitution = restitution;
		body.LinearRestitution = lrestitution;
		body.AngularRestitution = arestitution;
		body.Rigidity = rigidity;
		body.NumIterations = num_iterations;

		// setting object's parameters and transformation
		OMD.WorldTransform = tm;
		OMD.SetMaterialParameterFloat4("albedo_color", color, 0);
		OMD.Name = name;

		return OMD;
	}

	private void Init()
	{
		player = new PlayerSpectator();

		player.Position = new Vec3(30.0f, 0.0f, 30.5f);
		player.SetDirection(new vec3(-1.0f, 0.0f, -0.4f), new vec3(0.0f, 0.0f, -1.0f));
		Game.Player = player;

		cloth = createBodyCloth("MyCloth", 20.0f, 20.0f, 1.0f, 10.0f, 0.05f, 0.05f, 0.05f, 0.2f, 0.05f, 8, new vec4(0.3f, 0.3f, 1.0f, 1.0f), new Mat4(MathLib.Translate(new Vec3(0.0f, 0.0f, 25.0f))));

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

		// creating 2 dummy bodies to which the cloth will be attached
		dummy1 = createBodyDummy("fixpoint1", new Vec3(1.0f, 1.0f, 1.0f), new Vec3(-10.0f, -10.0f, 25.0f));
		dummy2 = createBodyDummy("fixpoint2", new Vec3(1.0f, 1.0f, 1.0f), new Vec3(-10.0f, 10.0f, 25.0f));

		// creating 2 particles joints to attach the cloth to dummy bodies
		new JointParticles(dummy1.Body, cloth.Body, dummy1.Position, new vec3(1.0f));
		new JointParticles(dummy2.Body, cloth.Body, dummy2.Position, new vec3(1.0f));
	}

}
Last update: 2022-12-14
Build: ()