the-world-engine
Version:
three.js based, unity like game engine for browser
178 lines (172 loc) • 5.78 kB
JavaScript
import { b2Vec2, b2Transform } from "../common/b2_math.js";
import { b2Color } from "../common/b2_draw.js";
export var b2ParticleGroupFlag;
(function(t) {
t[t["b2_solidParticleGroup"] = 1] = "b2_solidParticleGroup";
t[t["b2_rigidParticleGroup"] = 2] = "b2_rigidParticleGroup";
t[t["b2_particleGroupCanBeEmpty"] = 4] = "b2_particleGroupCanBeEmpty";
t[t["b2_particleGroupWillBeDestroyed"] = 8] = "b2_particleGroupWillBeDestroyed";
t[t["b2_particleGroupNeedsUpdateDepth"] = 16] = "b2_particleGroupNeedsUpdateDepth";
t[t["b2_particleGroupInternalMask"] = 24] = "b2_particleGroupInternalMask";
})(b2ParticleGroupFlag || (b2ParticleGroupFlag = {}));
export class b2ParticleGroupDef {
constructor() {
this.flags = 0;
this.groupFlags = 0;
this.position = new b2Vec2;
this.angle = 0;
this.linearVelocity = new b2Vec2;
this.angularVelocity = 0;
this.color = new b2Color;
this.strength = 1;
this.shapeCount = 0;
this.stride = 0;
this.particleCount = 0;
this.lifetime = 0;
this.userData = null;
this.group = null;
}
}
export class b2ParticleGroup {
constructor(t) {
this.m_firstIndex = 0;
this.m_lastIndex = 0;
this.m_groupFlags = 0;
this.m_strength = 1;
this.m_prev = null;
this.m_next = null;
this.m_timestamp = -1;
this.m_mass = 0;
this.m_inertia = 0;
this.m_center = new b2Vec2;
this.m_linearVelocity = new b2Vec2;
this.m_angularVelocity = 0;
this.m_transform = new b2Transform;
this.m_userData = null;
this.m_system = t;
}
GetNext() {
return this.m_next;
}
GetParticleSystem() {
return this.m_system;
}
GetParticleCount() {
return this.m_lastIndex - this.m_firstIndex;
}
GetBufferIndex() {
return this.m_firstIndex;
}
ContainsParticle(t) {
return this.m_firstIndex <= t && t < this.m_lastIndex;
}
GetAllParticleFlags() {
if (!this.m_system.m_flagsBuffer.data) {
throw new Error;
}
let t = 0;
for (let i = this.m_firstIndex; i < this.m_lastIndex; i++) {
t |= this.m_system.m_flagsBuffer.data[i];
}
return t;
}
GetGroupFlags() {
return this.m_groupFlags;
}
SetGroupFlags(t) {
t |= this.m_groupFlags & b2ParticleGroupFlag.b2_particleGroupInternalMask;
this.m_system.SetGroupFlags(this, t);
}
GetMass() {
this.UpdateStatistics();
return this.m_mass;
}
GetInertia() {
this.UpdateStatistics();
return this.m_inertia;
}
GetCenter() {
this.UpdateStatistics();
return this.m_center;
}
GetLinearVelocity() {
this.UpdateStatistics();
return this.m_linearVelocity;
}
GetAngularVelocity() {
this.UpdateStatistics();
return this.m_angularVelocity;
}
GetTransform() {
return this.m_transform;
}
GetPosition() {
return this.m_transform.p;
}
GetAngle() {
return this.m_transform.q.GetAngle();
}
GetLinearVelocityFromWorldPoint(t, i) {
const s = b2ParticleGroup.GetLinearVelocityFromWorldPoint_s_t0;
this.UpdateStatistics();
return b2Vec2.AddVCrossSV(this.m_linearVelocity, this.m_angularVelocity, b2Vec2.SubVV(t, this.m_center, s), i);
}
GetUserData() {
return this.m_userData;
}
SetUserData(t) {
this.m_userData = t;
}
ApplyForce(t) {
this.m_system.ApplyForce(this.m_firstIndex, this.m_lastIndex, t);
}
ApplyLinearImpulse(t) {
this.m_system.ApplyLinearImpulse(this.m_firstIndex, this.m_lastIndex, t);
}
DestroyParticles(t) {
if (this.m_system.m_world.IsLocked()) {
throw new Error;
}
for (let i = this.m_firstIndex; i < this.m_lastIndex; i++) {
this.m_system.DestroyParticle(i, t);
}
}
UpdateStatistics() {
if (!this.m_system.m_positionBuffer.data) {
throw new Error;
}
if (!this.m_system.m_velocityBuffer.data) {
throw new Error;
}
const t = new b2Vec2;
const i = new b2Vec2;
if (this.m_timestamp !== this.m_system.m_timestamp) {
const s = this.m_system.GetParticleMass();
this.m_mass = s * (this.m_lastIndex - this.m_firstIndex);
this.m_center.SetZero();
this.m_linearVelocity.SetZero();
for (let t = this.m_firstIndex; t < this.m_lastIndex; t++) {
this.m_center.SelfMulAdd(s, this.m_system.m_positionBuffer.data[t]);
this.m_linearVelocity.SelfMulAdd(s, this.m_system.m_velocityBuffer.data[t]);
}
if (this.m_mass > 0) {
const t = 1 / this.m_mass;
this.m_center.SelfMul(t);
this.m_linearVelocity.SelfMul(t);
}
this.m_inertia = 0;
this.m_angularVelocity = 0;
for (let r = this.m_firstIndex; r < this.m_lastIndex; r++) {
b2Vec2.SubVV(this.m_system.m_positionBuffer.data[r], this.m_center, t);
b2Vec2.SubVV(this.m_system.m_velocityBuffer.data[r], this.m_linearVelocity, i);
this.m_inertia += s * b2Vec2.DotVV(t, t);
this.m_angularVelocity += s * b2Vec2.CrossVV(t, i);
}
if (this.m_inertia > 0) {
this.m_angularVelocity *= 1 / this.m_inertia;
}
this.m_timestamp = this.m_system.m_timestamp;
}
}
}
b2ParticleGroup.GetLinearVelocityFromWorldPoint_s_t0 = new b2Vec2;