Impulse

A short push, such as a knockback, that an entity ticks once per update. step(dt) returns how far to move this frame, so the entity can add it to its position or route it through its own collision (e.g. BGE.SolidWorld.moveAndSlide()).

Two shapes: start() eases out (the velocity decays each step until it falls below a stop speed), and startTimed() moves at a constant velocity for a set time.

Properties

  • velocity (dynamic) — Current velocity, in units per second (or per step unit; see step()).

Example

CODE
' On a hit: 600 units/s away from the attacker, easing out
m.knockback.start(BGE.Math.VectorOps.scale(away, 600), 10)
' In onUpdate(dt):
push = m.knockback.step(dt)
m.position.x += push.x
m.position.z += push.z

Constructor

new Impulse(): Impulse

Instance Methods

start( velocity: BGE.Math.Vector, decayRate: float, stopSpeed?: float, ): void

Starts (or replaces) an easing push. Each step keeps 1 - decayRate * dt of the velocity, and stops once the speed falls below stopSpeed.

Parameters

  • velocity (BGE.Math.Vector) — initial velocity
  • decayRate (float) — fraction of the velocity lost per second; 10 stops in well under a second
  • stopSpeed (float, optional, default: "1.0") — speed below which the push ends

Returns

  • void

startTimed(displacement: BGE.Math.Vector, seconds: float): void

Starts (or replaces) a push that covers displacement at a constant velocity over seconds. The last step moves a whole dt, like a CountdownTimer-gated move.

Parameters

  • displacement (BGE.Math.Vector) — total distance to move
  • seconds (float)

Returns

  • void

stop(): void

Ends the push now.

Returns

  • void

isActive(): boolean

Whether the push is still running. A timed push counts as running for its whole time, even with a zero displacement.

Returns

  • boolean

step(dt: float): BGE.Math.Vector

Advances the push and returns the displacement for this step (zero once it's over). dt is normally seconds; a fixed-tick game can pass 1 per tick and give velocities and decayRate per tick instead.

Parameters

  • dt (float)

Returns

  • BGE.Math.Vector