aboutsummaryrefslogtreecommitdiff
path: root/src/shared/player_movement.moon
diff options
context:
space:
mode:
authorAlex Pickering <alex@cogarr.net>2026-02-01 13:14:32 -0600
committerAlexander M Pickering <alex@cogarr.net>2026-02-01 13:14:32 -0600
commit3a975db66a3711f34e8b64bb27a8eaca79fdeca9 (patch)
treefcc12f8f9d638ff575c1963796de76b7628854b4 /src/shared/player_movement.moon
downloadggj26-3a975db66a3711f34e8b64bb27a8eaca79fdeca9.tar.gz
ggj26-3a975db66a3711f34e8b64bb27a8eaca79fdeca9.tar.bz2
ggj26-3a975db66a3711f34e8b64bb27a8eaca79fdeca9.zip
Initial commitHEADmaster
Diffstat (limited to 'src/shared/player_movement.moon')
-rw-r--r--src/shared/player_movement.moon54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/shared/player_movement.moon b/src/shared/player_movement.moon
new file mode 100644
index 0000000..31aada4
--- /dev/null
+++ b/src/shared/player_movement.moon
@@ -0,0 +1,54 @@
+-- we want to find the location based on inital velocity and position, constant acceleration, and delta time
+-- Each function should be called with a `PredictedComponent` as `self`.
+
+-- In a normal simulation, velocity adds
+-- acceleration * delta time
+-- every tick, minus some friction:
+-- coefficient * current velocity
+-- i.e. velocity = (acceleration * delta) - (friction * velocity)
+-- every tick
+-- velocity[tick] = (acceleration * delta[tick]) - (friction * velocity[tick - 1])
+-- velocity[4] = (acceleration * delta[4]) - (friction * velocity[3])
+-- = (acceleration * delta[4]) - (friction * ((acceleration * delta[3]) - (friction * velocity[2])))
+-- = (acceleration * delta[4]) - (friction * ((acceleration * delta[3]) - (friction * ((acceleration * delta[2]) - (friction * velocity[inital])))))
+-- = (acceleration * delta[4]) - (friction * ((acceleration * delta[3]) - ((friction * acceleration * delta[2]) - (friction * friction * velocity[inital]))))
+-- = (acceleration * delta[4]) - (friciton * ((acceleration * delta[3]) - (friction * acceleration * delta[2]) + (friction^2 * velocity[inital])))
+-- = (acceleration * delta[4]) - ((friction * acceleration * delta[3]) - (friction * friction * acceleration * delta[2]) + (friction^3 * velocity[inital]))
+-- = (acceleration * delta[4]) - (friction * acceleration * delta[3]) + (friction^2 * acceleration * delta[2]) - (friction^3 * velocity[inital])
+-- as delta approaches 0 (high fidelity simulation), the middle components become e^(-friction * delta), and acceleration needs to be divided by friction
+-- Position is a second layer on top
+-- position[tick] = position[tick-1] + velocity[tick]
+-- position[2] = position[inital] + velocity[2]
+-- = position[inital] + (acceleration * delta[2]) - (friction * velocity[inital])
+-- position[delta] = (delta * (acceleration / friction) ) - ((1 / friction) * (velocity[inital] - (acceleratin / friction)) * e^(-friction * delta) + position[inital]
+
+friction = 0.3
+{
+ acc:() =>
+ acc = vec3(unpack(@net.properties.acc))
+ movement_speed = 1/1000 -- @net.properties.move_speed?
+ freeze = 1 -- @net.properties.frozen?
+ newacc = acc * (movement_speed) * (freeze)
+ {newacc.x, newacc.y, newacc.z}
+ vel: () =>
+ acc = vec3(unpack(@properties.acc))
+ vel = vec3(unpack(@net.properties.vel))
+ now = am.eval_js("Date.now();")
+ --print("Net is ", @net.properties)
+ delta = (now - @net.properties.last_update) / 1000
+ newvel = (acc / friction) + ((vel - (acc / friction)) * math.exp(-friction * delta))
+ {newvel.x, newvel.y, newvel.z}
+ pos: () =>
+ now = am.eval_js("Date.now();")
+ delta = (now - @net.properties.last_update) / 1000
+ vel = vec3(unpack(@properties.vel))
+ pos = vec3(unpack(@properties.pos))
+ acc = vec3(unpack(@properties.acc))
+ friction_loss = acc / friction
+ -- when delta = 0 (up to date)
+ -- pos = (1/friction) * (velocity - friction_loss) * 1 + position
+ -- = 2 * (2 - 2) * 1 + position
+ -- = position
+ newpos = (friction_loss * delta) - ((1/friction) * (vel - friction_loss) * (math.exp(-friction * delta))) + pos
+ {newpos.x, newpos.y, newpos.z}
+}