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.setMaterialParameter("albedo_color", color, 0);
OMD.setProperty("surface_base", "*");
OMD.setWorldTransform(new dmat4(MathLib.translate(pos)));
OMD.setName(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.setWorldTransform(new dmat4(MathLib.translate(pos)));
dummy.setName(name);
//assigning a dummy body to the dummy object and adding a box shape to it
new BodyDummy(dummy.getObject());
dummy.getBody().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.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.setMaterialParameter("albedo_color", color, 0);
OMD.setProperty("surface_base", "*");
OMD.setName(name);
return OMD;
}
/* .. */
public override int init()
{
player = new PlayerSpectator();
player.release();
player.setPosition(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.get().setPlayer(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.getPosition(), new vec3(1.0f));
new JointParticles(dummy2.getBody(), cloth.getBody(), dummy2.getPosition(), new vec3(1.0f));
return 1;
}
/* .. */
}
}
Last update: 2018-08-10
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)