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. A tutorial teaching how to reproduce the same scene in UnigineEditor can be found here.
#include <core/scripts/primitives.h>
ObjectMeshDynamic MyCloth;
ObjectMeshDynamic MySphere;
ObjectDummy dummy1;
ObjectDummy dummy2;
/// function, creating a named sphere with a specified radius and color at pos
Object createSphere(string name, float radius, Vec4 color, Vec3 pos)
{
// creating a dynamic mesh object with a sphere surface and passing it to Editor
ObjectMeshDynamic sphere = Unigine::createSphere(radius);
engine.editor.addNode(node_remove(sphere));
// setting object's parameters and transformation
sphere.setWorldTransform(translate(pos));
sphere.setMaterial("mesh_base", "*");
sphere.setMaterialParameter("albedo_color", color, 0);
sphere.setProperty("surface_base", "*");
sphere.setName(name);
return sphere;
}
/// function, creating a named cloth body with specified parameters
Object 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, Vec3 pos)
{
// creating a dynamic mesh object with a sphere surface and passing it to Editor
ObjectMeshDynamic cloth = Unigine::createPlane(width,height,step);
engine.editor.addNode(node_remove(cloth));
//assigning a cloth body to the dynamic mesh object and setting cloth parameters
BodyCloth body = class_remove(new BodyCloth(cloth));
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
cloth.setMaterial("mesh_base", "*");
cloth.setMaterialParameter("albedo_color", color, 0);
cloth.setProperty("surface_base", "*");
cloth.setWorldTransform(pos);
cloth.setName(name);
return cloth;
}
/// function, creating a named dummy body of a specified size at pos
Object createBodyDummy(string name, Vec3 size, Vec3 pos)
{
// creating a dummy object and passing it to Editor
ObjectDummy dummy = new ObjectDummy();
engine.editor.addNode(node_remove(dummy));
// setting object's parameters
dummy.setWorldTransform(translate(pos));
dummy.setName(name);
//assigning a dummy body to the dummy object and adding a box shape to it
BodyDummy body = class_remove(new BodyDummy(dummy));
ShapeBox shape = class_remove(new ShapeBox(size));
body.addShape(shape, translate(0.0f, 0.0f, 0.0f));
return dummy;
}
int init() {
// setting up player
Player player = new PlayerSpectator();
player.setPosition(Vec3(30.0f,0.0f, 30.5f));
player.setDirection(Vec3(-1.0f, 0.0f, -0.4f));
engine.game.setPlayer(player);
// creating a cloth
MyCloth = createBodyCloth("MyCloth", 20.0f, 20.0f, 1.0f, 10.0f, 0.05f, 0.05f, 0.05f, 0.2f, 0.05f, 8, Vec4(1.0f, 0.1f, 0.1f, 1.0f), translate(Vec3(0.0f, 0.0f, 25.0f)));
// creating a sphere
MySphere = 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
class_remove(new JointParticles(dummy1.getBody(),MyCloth.getBody(),dummy1.getPosition(),Vec3(1.0f)));
class_remove(new JointParticles(dummy2.getBody(),MyCloth.getBody(),dummy2.getPosition(),Vec3(1.0f)));
return 1;
}
Last update: 03.07.2017
Помогите сделать статью лучше
Была ли эта статья полезной?
(или выберите слово/фразу и нажмите Ctrl+Enter