From a1df463a16f4c37ecc8de5bf5dcd3114bfb1681f Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Sun, 14 Jan 2024 19:57:22 -0800 Subject: [PATCH] braking distance and visualization. bad calculation the visualization is really helpful for showing me that I fucked up my math --- autobrake_test.p8 | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/autobrake_test.p8 b/autobrake_test.p8 index bf88058..07a9102 100644 --- a/autobrake_test.p8 +++ b/autobrake_test.p8 @@ -126,6 +126,12 @@ function ship:draw() else spr(1,self.x,self.y) end + + if (self.dx == 0) return + local bd, f = brake_dist(self.dx, self.thrust + self.drag) + local bdx = self.x+bd-2 + spr(3, bdx,self.y-3) + print(tostr(f), bdx, self.y - 8, 14) end function calc_velocity(v0, t, vmax, drag) @@ -163,9 +169,21 @@ constraints = { function constraints:constrain(s, want) self.color=10 + + local v1 = calc_velocity(s.dx, want, s.maxspd, s.drag) + local bd, bf = brake_dist(v1, s.thrust + s.drag) + return want end +function brake_dist(v0, brake_max) + local tri_frames = abs(v0/brake_max)&-1 + local chunks = tri_frames * (tri_frames - 1) >> 1 + local chunk_zone = chunks * brake_max + local overage = v0 - chunk_zone + return chunk_zone + overage * (tri_frames + 1) * -sgn(v0), (overage > 0) and tri_frames + 1 or tri_frames +end + function constraints:cycle() if self.ctype=="bounds" then self.ctype="point" @@ -201,10 +219,10 @@ end -- todo: spark ring buffer __gfx__ -00000000800000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -00000000006666000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000008000000000080000a000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000006666000080000009090000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000067777600800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -00000000675555758008888000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -00000000675000750800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000675555758008888009090000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +000000006750007508000000a000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000067777500080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000005555000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000