This page has been translated automatically.
UnigineEditor
Interface Overview
Assets Workflow
Settings and Preferences
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Landscape Tool
Using Editor Tools for Specific Tasks
FAQ
Programming
Fundamentals
Setting Up Development Environment
UnigineScript
C++
C#
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine and 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
CIGI Client Plugin
Rendering-Related Classes
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.

Source code (C#)
// AppWorldLogic.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Unigine;

namespace UnigineApp
{
	class AppWorldLogic : WorldLogic
	{
        ObjectMeshDynamic cloth;
        ObjectMeshDynamic sphere;
        ObjectDummy dummy1;
        ObjectDummy dummy2;
		PlayerSpectator player;
		
        /// method, creating a named sphere with a specified radius and color at pos	
        ObjectMeshDynamic createSphere(String name, float radius, vec4 color, vec3 pos)
        {
	        // creating a sphere dynamic mesh
	        ObjectMeshDynamic OMD = Primitives.CreateSphere(radius);

	        // setting parameters
            OMD.SetMaterial("mesh_base", "*");
	        OMD.SetMaterialParameter("albedo_color", color, 0);
	        OMD.WorldTransform = new dmat4(MathLib.Translate(pos));
	        OMD.Name = name;

        	return OMD;
        }

        /// method, creating a named dummy body of a specified size at pos
        ObjectDummy createBodyDummy(String name, vec3 size, vec3 pos)
        {
	        // creating a dummy object
	        ObjectDummy dummy = new ObjectDummy();

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

	        //assigning a dummy body to the dummy object and adding a box shape	to it
	        new BodyDummy(dummy.getObject());
	        dummy.Body.AddShape(new ShapeBox(size).GetShape(), MathLib.Translate(0.0f, 0.0f, 0.0f));

	        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, dmat4 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.GetObject());

	        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.SetMaterial("mesh_base", "*");
	        OMD.SetMaterialParameter("albedo_color", color, 0);
	        OMD.Name = name;

	        return OMD;
        }

		/* .. */

		public override bool Init()
		{
	
            player = new PlayerSpectator();

	        player.Position = new dvec3(30.0f, 0.0f, 30.5f);
	        player.SetDirection(new vec3(-1.0f, 0.0f, -0.4f), new vec3(0.0f, 0.0f, -1.0f));
	        Engine.game.Player = player.getPlayer();

	        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 dmat4(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 attach the cloth to
	        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.GetBody(), cloth.GetBody(), dummy1.Position, new vec3(1.0f));
	        new JointParticles(dummy2.GetBody(), cloth.GetBody(), dummy2.Position, new vec3(1.0f));

			return true;
		}

		/* .. */
		
	}
}
Last update: 2019-08-16
Build: ()