新增cloth与ragdoll

This commit is contained in:
yhh
2021-07-04 23:53:38 +08:00
parent 77ad112f67
commit 416f243bda
6 changed files with 196 additions and 1 deletions

View File

@@ -0,0 +1,33 @@
module es {
export class Cloth extends Composite {
constructor(topLeftPosition: Vector2, width: number, height: number, segments: number = 20, stiffness: number = 0.25,
tearSensitivity: number = 5, connectHorizontalParticles: boolean = true) {
super();
const xStride = width / segments;
const yStride = height / segments;
for (let y = 0; y < segments; y++) {
for (let x = 0; x < segments; x++) {
const px = topLeftPosition.x + x * xStride;
const py = topLeftPosition.y + y + yStride;
const particle = this.addParticle(new Particle(new Vector2(px, py)));
if (connectHorizontalParticles && x > 0)
this.addConstraint(new DistanceConstraint(this.particles[y * segments + x],
this.particles[y * segments + x - 1], stiffness))
.setTearSensitivity(tearSensitivity)
.setCollidesWithColliders(false);
if (y > 0)
this.addConstraint(new DistanceConstraint(this.particles[y * segments + x],
this.particles[(y - 1) * segments + x], stiffness))
.setTearSensitivity(tearSensitivity)
.setCollidesWithColliders(false);
if (y == 0)
particle.pin();
}
}
}
}
}

View File

@@ -0,0 +1,55 @@
module es {
export class Ragdoll extends Composite {
constructor(x: number, y: number, bodyHeight: number) {
super();
const headLength = bodyHeight / 7.5;
const head = this.addParticle(new Particle({ x: x + RandomUtils.randint(-5, 5), y: y + RandomUtils.randint(-5, 5) }));
head.radius = headLength * 0.75;
head.mass = 4;
const shoulder = this.addParticle(new Particle({ x: x + RandomUtils.randint(-5, 5), y: y + RandomUtils.randint(-5, 5) }));
shoulder.mass = 26;
this.addConstraint(new DistanceConstraint(head, shoulder, 1, 5 / 4 * headLength));
const elbowLeft = this.addParticle(new Particle({ x: x + RandomUtils.randint(-5, 5), y: y + RandomUtils.randint(-5, 5) }));
const elbowRight = this.addParticle(new Particle({ x: x + RandomUtils.randint(-5, 5), y: y + RandomUtils.randint(-5, 5) }));
elbowLeft.mass = 2;
elbowRight.mass = 2;
this.addConstraint(new DistanceConstraint(elbowLeft, shoulder, 1, headLength * 3 / 2));
this.addConstraint(new DistanceConstraint(elbowRight, shoulder, 1, headLength * 3 / 2));
const handLeft = this.addParticle(new Particle({ x: x + RandomUtils.randint(-5, 5), y: y + RandomUtils.randint(-5, 5) }));
const handRight = this.addParticle(new Particle({ x: x + RandomUtils.randint(-5, 5), y: y + RandomUtils.randint(-5, 5) }));
handLeft.mass = 2;
handRight.mass = 2;
this.addConstraint(new DistanceConstraint(handLeft, elbowLeft, 1, headLength * 2));
this.addConstraint(new DistanceConstraint(handRight, elbowRight, 1, headLength * 2));
const pelvis = this.addParticle(new Particle({ x: x + RandomUtils.randint(-5, 5), y: y + RandomUtils.randint(-5, 5) }));
pelvis.mass = 15;
this.addConstraint(new DistanceConstraint(pelvis, shoulder, 0.8, headLength * 3.5));
this.addConstraint(new DistanceConstraint(pelvis, head, 0.02, bodyHeight * 2))
.setCollidesWithColliders(false);
const kneeLeft = this.addParticle(new Particle({ x: x + RandomUtils.randint(-5, 5), y: y + RandomUtils.randint(-5, 5) }));
const kneeRight = this.addParticle(new Particle({ x: x + RandomUtils.randint(-5, 5), y: y + RandomUtils.randint(-5, 5) }));
kneeLeft.mass = 10;
kneeRight.mass = 10;
this.addConstraint(new DistanceConstraint(kneeLeft, pelvis, 1, headLength * 2));
this.addConstraint(new DistanceConstraint(kneeRight, pelvis, 1, headLength * 2));
const footLeft = this.addParticle(new Particle({ x: x + RandomUtils.randint(-5, 5), y: y + RandomUtils.randint(-5, 5) }));
const footRight = this.addParticle(new Particle({ x: x + RandomUtils.randint(-5, 5), y: y + RandomUtils.randint(-5, 5) }));
footLeft.mass = 5;
footRight.mass = 5;
this.addConstraint(new DistanceConstraint(footLeft, kneeLeft, 1, headLength * 2));
this.addConstraint(new DistanceConstraint(footRight, kneeRight, 1, headLength * 2));
this.addConstraint(new DistanceConstraint(footLeft, shoulder, 0.001, bodyHeight * 2))
.setCollidesWithColliders(false);
this.addConstraint(new DistanceConstraint(footLeft, shoulder, 0.001, bodyHeight * 2))
.setCollidesWithColliders(false);
}
}
}