Util steering
Steering functions.
Helps with physics interactions. Credit to ivan
Usage:
-- current linear velocity local cvx, cvy = body:GetLinearVelocity() -- target linear velocity required to counteract the effects of gravity and linear damping: local lvx, lvy = steering.compensateGravity(0, 0, gx, gy, interval) lvx, lvy = steering.compensateLinearDamping(lvx, lvy, damping, maxLV, interval) -- force required to reach the target linear velocity local fx, fy = steering.force(cvx, cvy, lvx, lvy, mass, interval) -- keep the body aloft using only forces body:ApplyForce(fx, fy)
Functions
| futurePosition (x, y, lvx, lvy, damping, gx, gy, dt) | Returns the future position assuming no external forces are acting on the body |
| futureAngle (a, av, damping, dt) | Returns the future angle assuming no external forces are acting on the body |
| compensateGravity (lvx, lvy, gx, gy, dt) | Compensates for the effects of gravity |
| compensateLinearDamping (lvx, lvy, damping, maxLV, dt) | Compensates for the effects of linear damping |
| compensateAngularDamping (av, damping, maxAV, dt) | Compensates for the effects of angular damping |
| force (ivx, ivy, fvx, fvy, mass, dt) | Returns the force required to reach a given linear velocity. |
| torque (iv, fv, mass, dt) | Returns the torque required to reach a given angular velocity. |
| accelerationTime (ivx, ivy, fvx, fvy, mass, force) | Returns the time required to accelerate to a given linear velocity. |
| angularAccelerationTime (iv, fv, mass, torque) | Returns the time required to accelerate to a given angular velocity. |
Functions
- futurePosition (x, y, lvx, lvy, damping, gx, gy, dt)
-
Returns the future position assuming no external forces are acting on the body
Parameters:
- x number x position at the beginning of the time step
- y number y position at the beginning of the time step
- lvx number linear velocity (x) at the beginning of the time step
- lvy number linear velocity (y) at the beginning of the time step
- damping number linear damping
- gx number gravity (x)
- gy number gravity (y)
- dt number time step
Returns:
- number x position at the end of the time step
- number y position at the end of the time step
- futureAngle (a, av, damping, dt)
-
Returns the future angle assuming no external forces are acting on the body
Parameters:
- a number angle at the beginning of the time step
- av number angular velocity at the beginning of the time step
- damping number linear damping
- dt number time step
Returns:
-
number
future angle at the end of the time step
- compensateGravity (lvx, lvy, gx, gy, dt)
-
Compensates for the effects of gravity
Parameters:
- lvx number desired linear velocity (x) at the end of the time step
- lvy number desired linear velocity (y) at the end of the time step
- gx number gravity (x)
- gy number gravity (y)
- dt number time step
Returns:
- number target linear velocity (x) at the beginning of the time step
- number target linear velocity (y) at the beginning of the time step
- compensateLinearDamping (lvx, lvy, damping, maxLV, dt)
-
Compensates for the effects of linear damping
Parameters:
- lvx number desired linear velocity (x) at the end of the time step
- lvy number desired linear velocity (y) at the end of the time step
- damping number linear damping
- maxLV number maximum linear velocity (length)
- dt number time step
Returns:
- number target linear velocity (x) at the beginning of the time step
- number target linear velocity (y) at the beginning of the time step
- compensateAngularDamping (av, damping, maxAV, dt)
-
Compensates for the effects of angular damping
Parameters:
- av number desired angular velocity at the end of the time step
- damping number linear damping
- maxAV number Maximum angular velocity
- dt number time step
Returns:
-
number
target angular velocity at the beginning of the time step
- force (ivx, ivy, fvx, fvy, mass, dt)
-
Returns the force required to reach a given linear velocity.
Reminder: force = ( change in velocity / time ) * mass
Parameters:
- ivx number linear velocity (x) at the beginning of the time step
- ivy number linear velocity (y) at the beginning of the time step
- fvx number desired linear velocity (x) at the end of the time step
- fvy number desired linear velocity (y) at the end of the time step
- mass number mass
- dt number time step
Returns:
-
number
force to apply
- torque (iv, fv, mass, dt)
-
Returns the torque required to reach a given angular velocity.
Reminder: inertia = mass * initial velocity,
torque = ( change in velocity / time ) * inertia
Parameters:
- iv number angular velocity at the beginning of the time step
- fv number angular velocity at the end of the time step
- mass number mass
- dt number time step
Returns:
-
number
torque to apply
- accelerationTime (ivx, ivy, fvx, fvy, mass, force)
-
Returns the time required to accelerate to a given linear velocity.
Reminder: time = change in velocity * mass / force
Parameters:
- ivx number linear velocity (x) at the beginning of the time step
- ivy number linear velocity (y) at the beginning of the time step
- fvx number desired linear velocity (x) at the end of the time step
- fvy number desired linear velocity (y) at the end of the time step
- mass number mass
- force number force being applied
Returns:
-
number
time
- angularAccelerationTime (iv, fv, mass, torque)
-
Returns the time required to accelerate to a given angular velocity.
Reminder: inertia = mass * initial velocity,
time = change in velocity * inertia / torque
Parameters:
- iv number angular velocity at the beginning of the time step
- fv number desired angular velocity at the end of the time step
- mass number mass
- torque number torque being applied
Returns:
-
number
time