MCPcopy
hub / github.com/KilledByAPixel/LittleJS / update

Method update

src/engineParticles.js:380–461  ·  view source on GitHub ↗

Update the particle

()

Source from the content-addressed store, hash-verified

378
379 /** Update the particle */
380 update()
381 {
382 // emitter properties
383 const emitter = this.emitter;
384 const damping = emitter.damping;
385 const angleDamping = emitter.angleDamping;
386 const restitution = emitter.restitution;
387 const friction = emitter.friction;
388 const gravityScale = emitter.gravityScale;
389 const collideTiles = emitter.collideTiles;
390 const collideCallback = emitter.particleCollideCallback;
391
392 // destroy particle when its time runs out
393 if (this.lifeTime > 0 && time - this.spawnTime > this.lifeTime)
394 {
395 this.destroy();
396 return;
397 }
398
399 // apply physics
400 const oldPos = this.pos.copy();
401 this.velocity.x *= damping;
402 this.velocity.y *= damping;
403 this.pos.x += this.velocity.x += gravity.x * gravityScale;
404 this.pos.y += this.velocity.y += gravity.y * gravityScale;
405 this.angle += this.angleVelocity *= angleDamping;
406
407 // don't do collision if solver disabled
408 if (!enablePhysicsSolver || !collideTiles) return;
409
410 // apply max circular speed to prevent going through collision
411 const length2 = this.velocity.lengthSquared();
412 if (length2 > objectMaxSpeed*objectMaxSpeed)
413 {
414 const s = objectMaxSpeed / length2**.5;
415 this.velocity.x *= s;
416 this.velocity.y *= s;
417 }
418
419 // check collision against tiles
420 this.groundObject = undefined;
421 const testCollision = collideCallback ? (pos)=>
422 {
423 const data = tileCollisionGetData(pos);
424 return data && collideCallback(this, data, pos);
425 } : (pos)=> tileCollisionGetData(pos) > 0;
426
427 if (testCollision(this.pos))
428 {
429 // if already was stuck in collision, don't do anything
430 const hitLayer = tileCollisionTest(this.pos);
431 if (!testCollision(oldPos))
432 {
433 // testCollision already invoked collideCallback with the
434 // correct (this, data, pos) args; no need to re-check here.
435 // test which side we bounced off (or both if a corner)
436 const isBlockedX = testCollision(vec2(this.pos.x, oldPos.y));
437 const isBlockedY = testCollision(vec2(oldPos.x, this.pos.y));

Callers 4

engineUpdateFunction · 0.45
updateChildObjectFunction · 0.45
engineObjectsUpdateFunction · 0.45
updateMethod · 0.45

Calls 7

destroyMethod · 0.95
tileCollisionGetDataFunction · 0.85
tileCollisionTestFunction · 0.85
vec2Function · 0.85
lengthSquaredMethod · 0.80
debugRectFunction · 0.70
copyMethod · 0.45

Tested by

no test coverage detected