xmax constraint -- imperfect but good enough

This commit is contained in:
Kistaro Windrider 2024-07-27 18:49:48 -07:00
parent 00678f97fd
commit d33d7ad6d1
Signed by: kistaro
SSH Key Fingerprint: SHA256:TBE2ynfmJqsAf0CP6gsflA0q5X5wD5fVKWPsZ7eVUg8

View File

@ -81,9 +81,10 @@ function _draw()
print("dx: ".. the_ship.dx, 20, 86, 7) print("dx: ".. the_ship.dx, 20, 86, 7)
meter(80, 86, 128, 90, the_ship.dx/the_ship.maxspd/2) meter(80, 86, 128, 90, the_ship.dx/the_ship.maxspd/2)
print("x: "..the_ship.x, 24, 92, 7) print("x: "..the_ship.x, 24, 92, 7)
print("bx: "..gbx, 20, 98, 7)
print("xmin:"..tostr(constraints.xmin), 12, 102, 7) print("xmin:"..tostr(constraints.xmin), 12, 108, 7)
print("xmax:"..tostr(constraints.xmax), 12, 108, 7) print("xmax:"..tostr(constraints.xmax), 12, 114, 7)
end end
function meter(x0, y0, x1, y1, frac) function meter(x0, y0, x1, y1, frac)
@ -127,11 +128,11 @@ function ship:draw()
spr(1,self.x,self.y) spr(1,self.x,self.y)
end end
if (self.dx == 0) return --if (self.dx == 0) return
local bd, f = brake_dist(self.dx, self.thrust + self.drag) local bd, f = brake_dist(self.dx, self.thrust + self.drag)
local bdx = self.x+bd-2 gbx = self.x+bd
spr(3, bdx,self.y-2) spr(3, gbx-2,self.y-2)
print(tostr(f), bdx, self.y - 8, 14) print(tostr(f), gbx-2, self.y - 8, 14)
end end
function calc_velocity(v0, t, vmax, drag) function calc_velocity(v0, t, vmax, drag)
@ -147,6 +148,8 @@ function ship:update()
local t = btn(0) and -1 or btn(1) and 1 or 0 local t = btn(0) and -1 or btn(1) and 1 or 0
t *= self.thrust t *= self.thrust
t = constraints:constrain(self, t) t = constraints:constrain(self, t)
-- t = constraints:constrain(self, t)
-- t = constraints:constrain(self, t)
local s = calc_velocity(self.dx, t, self.maxspd, self.drag) local s = calc_velocity(self.dx, t, self.maxspd, self.drag)
self.x += s self.x += s
@ -172,6 +175,12 @@ function constraints:constrain(s, want)
local bd, bf = brake_dist(v1, bmx) local bd, bf = brake_dist(v1, bmx)
local bx, txm = s.x + bd + v1, self.xmax local bx, txm = s.x + bd + v1, self.xmax
if bx < self.xmin then if bx < self.xmin then
-- predicted brake point left
-- of xmin; apply max reverse
-- thrust, treat xmin as our
-- max target, and handle
-- overbraking by coalescing
-- with past +xmax case
self.color = 9 self.color = 9
want = s.thrust want = s.thrust
txm = self.xmin txm = self.xmin
@ -179,9 +188,11 @@ function constraints:constrain(s, want)
bd, bf = brake_dist(v1, bmx) bd, bf = brake_dist(v1, bmx)
bx = bd + s.x + v1 bx = bd + s.x + v1
end end
if (bx < txm) return want if (bx <= txm) return want
self.color = 8 self.color = 8
-- TODO: implement overshot constraint local overage = bx - txm
want -= overage/(bf+1)
if (want < -s.thrust) want = -s.thrust
return want return want
end end