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.
// 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.SetMaterialParameterFloat4("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);
dummy.Body.AddShape(new ShapeBox(size), 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);
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.SetMaterialParameterFloat4("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));
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 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.Body, cloth.Body, dummy1.Position, new vec3(1.0f));
new JointParticles(dummy2.Body, cloth.Body, dummy2.Position, new vec3(1.0f));
return true;
}
/* .. */
}
}
Last update:
2020-06-01
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)