Starfighter acceleration concept - nsiomos/3dCorridor GitHub Wiki

Logic

Control

  • Acc/Deceleration is started by holding pressed the accelerate key.
  • Similarly, deceleration is started by holding pressed the decelerate key.
  • Accelerating and decelerating at the same time is not possible.
  • Releasing the keys stops acc/deceleration and starts reversion back to initial state.
  • Pressing opposite direction key (i.e. decelerate key when accelerating) stops acc/deceleration and starts reversion back to initial state.
  • Accelerate and decelerate keys are computed as axes, they softly increase intensity while being pressed.
  • The axis value increase is considered quick.

Accelerometer

  • An accelerometer automatically fills over time up to 100% while not acc/decelerating. At game start it is full.
  • The accelerometer decreases while acc/decelerating down to 0%.
  • Acc/Deceleration can only be started when the accelerometer is at max (100%).
  • Acc/Deceleration is stopped when the accelerometer reaches min (0%).

Applying acceleration motion

  • As with motion and rotation, starfighter publishes changes to its position as events, and the active objects interested in these changes (for acceleration, it is the level mover) will subscribe.
  • The position changed event will be enriched by acceleration related information, so subscribers can carry on their task in a straight-forward manner.

During acc/deceleration

  • In standard forward motion, level mover applies the forward translation and starfighter applies the sideward translation. In acceleration, the accelerate vector, which is part of the forward motion, is being applied at the beginning to a configurable extent by both. Only the non-acceleration part of the forward motion is being applied by the level mover as usual.
  • The division of accelerate portion applying (i.e. how much of the acceleration vector is applied by the starfighter and how much by the level mover) gradually shifts:
    • At acc/deceleration start (accelerometer is at max) it is determined by initial starfighter to level mover ratio of acceleration value. Should be around 0.5f or higher for both directions, accelerating and decelerating, to deliver a kick-start feeling to the player.
    • At the end (accelerometer is at min) all acceleration motion is being applied by the level mover and none by the starfighter.

During acceleration reset

  • The accelerate vector is gradually being diminished down, till at the end (starfighter reaches initial position) it is near zero.
    • NOTE: Emphasis on [...] "near" zero, because we need to ensure some motion for starfighter to fall back to its initial position. Thus, acceleration vector should not drop below 0.1f of its max value.
  • The accelerate vector diminishing speed and, with it, the speed at which starfighter will be back to its initial position, is determined by the accelerate reset factor, which is relative to the accelerate factor.
  • The accelerate portion division is gradually being rewinded from being fully applied by the level mover at accelerometer min up to being applied completely by the starfighter.
  • Since the starfighter actually needs to revert the motion done during acc/deceleration, it applies the starfighter portion of the acceleration in backwards direction and the level mover compensates for that by applying starfighter portion of the acceleration twice in forward direction.

Definitions

  • Acceleration state: Current acceleration state of the starfighter. One of none, accelerating, accelerate reset.
  • Accelerate factor: Scale of base speed (throttle) when acceleration is at max.
  • Accelerate reset factor: Scale of the accelerate factor representing how much faster the reset to base position happens compared to the acceleration.
  • Initial starfighter to level mover ratio of acceleration: What portion of the acceleration component of the forward translation is being done by the starfighter at the beginning of the acc/deceleration. (the "rest" is being applied by the level mover).

Applying acc/deceleration to the translation vector

Acc/Deceleration computation

  • Acc/Deceleration computation happens the translate vector computation phase of the starfighter motion logic.
  • Acceleration direction is determined by sign of the accelerate key axis:
    • accelerate key axis >= 0: acceleration direction is accelerating.
    • accelerate key axis < 0: acceleration direction is decelerating.
  • Differing computation depending on acceleration direction:
    • For acceleration the forward translation scale factor is (at max) accelerate factor.
    • For deceleration the forward translation scale factor is (at max) inverted, 1 / accelerate factor.
  • The accelerate key axis is interpreted as 0 being no and 1 being max acceleration. Forward translation scale factor is Lerp-ed by accelerate key axis:
    • Absolute accelerate key axis = 0: forward translation scale factor = 1.
    • Absolute accelerate key axis = 1: forward translation scale factor = accelerate factor.
  • The forward direction of the translation vector (which is by default scaled by throttle) is scaled by current forward translation scale factor. I.e. on a current forward translation scale factor of 2, the forward direction of the translation vector will be for acceleration: 2 * throttle, for deceleration 1/2 * throttle.

Acceleration reset computation

  • Acceleration reset computation happens the translate vector computation phase of the starfighter motion logic.
  • Acceleration reset direction is determined by sign of starfighter's local position z value:
    • local position z value >= 0: acceleration reset direction is resetting acceleration.
    • local position z value < 0: acceleration reset direction is resetting deceleration.
  • Differing computation depending on acceleration reset direction:
    • For resetting acceleration the forward translation scale factor is (at max) accelerate factor.
    • For resetting deceleration the forward translation scale factor is (at max) inverted, 1 / accelerate factor.
  • Forward translation scale factor is gradually being Lerp-ed by accelerometer value down to 0.1f in speed that is accelerate reset factor faster that acc/deceleration:
    • Accelerometer = 0: forward translation scale factor = accelerate factor.
    • Accelerometer = max / accelerate reset factor: forward translation scale factor = 0.1f.
  • As with acc/deceleration, the forward direction of the translation vector (which is by default scaled by throttle) is scaled by current forward translation scale factor. I.e. on a current forward translation scale factor of 2, the forward direction of the translation vector will be for acceleration: 2 * throttle, for deceleration 1/2 * throttle.

Applying acceleration motion by starfighter and levelmover

For Acc/deceleration

Starfighter

  • Acc/deceleration motion by starfighter is being applied in starfighter's motion phase if acceleration state is accelerating.
  • Acc/deceleration motion is applied to local forward translation only.
  • Getting the acceleration component of the forward translation vector by excluding standard forward motion from it: forward translation vector - throttle.
  • Thus: differing sign depending on acceleration direction:
    • For acceleration the acceleration component is positive.
    • For deceleration the acceleration component is negative.
  • Getting the portion of the acceleration component that is to be applied by starfighter. It is gradually being Lerp-ed by accelerometer value from an initial value to being fully applied by level mover:
    • Accelerometer = max: Starfighter part of acceleration component = acceleration component * initial starfighter to level mover ratio of acceleration.
    • Accelerometer = 0: Starfighter part of acceleration component = 0.
  • Applying starfighter's portion of the acceleration component by adding it to its local forward translation.
  • Firing position changed event and populating it with
    • Previous position (even if not changed).
    • Previous local position (even if not changed).
    • Acceleration state.
    • Acceleration component of forward translate vector.
    • Starfighter part of acceleration component.

Level mover

  • Acceleration motion by level mover is being applied in level mover's handling of starfighter position changed event.
  • Checking if starfighter's acceleration state is accelerating.
  • Getting the part of the acceleration component that is to be applied by level mover. It is the "rest" of the acceleration component not applied by starfighter, i.e. from the values published by starfighter's position changed event: level mover part of acceleration component = acceleration component of forward translate vector - starfighter part of acceleration component.
  • Applying level mover part of acceleration by adding it to level mover's standard forward translation (throttle).

For acceceleration reset

Starfighter

  • Acc/deceleration reset motion by starfighter is being applied in starfighter's motion phase if acceleration state is acceleration reset.
  • Acceleration reset motion is applied to local forward translation only.
  • Getting the acceleration component of the forward translation vector by excluding standard forward motion from it: forward translation vector - throttle.
  • Thus: differing sign depending on acceleration direction:
    • For acceleration the acceleration component is positive.
    • For deceleration the acceleration component is negative.
  • Getting the portion of the acceleration component that is to be applied by starfighter. It is gradually being Lerp-ed by accelerometer value from being fully applied by level mover to being fully applied by starfighter in speed that is accelerate reset factor faster that acc/deceleration:
    • Accelerometer = 0: Starfighter part of acceleration component = 0.
    • Accelerometer = max / accelerate reset factor: Starfighter part of acceleration component = acceleration component.
  • Applying starfighter's portion of the acceleration component "backwards" by subtracting it from its local forward translation. Thus gradually resetting starfighter's done during acc/deceleration in local space.
  • Firing position changed event and populating it with the same way as for acc/deceleration motion.

Level mover

  • Acceleration reset motion by level mover is being applied in level mover's handling of starfighter position changed event.
  • Checking if starfighter's acceleration state is accelerate reset.
  • Getting the part of the acceleration component that is to be applied by level mover. It is the "rest" of the acceleration component not applied by starfighter, i.e. from the values published by starfighter's position changed event: level mover part of acceleration component = acceleration component of forward translate vector - starfighter part of acceleration component.
  • Applying level mover part of acceleration reset by adding twice its amount to level mover's standard forward translation (throttle).