From 37db329b08f021498d1cfdb28efe3c0e828e2adf Mon Sep 17 00:00:00 2001 From: Nyeogmi Date: Sun, 18 Dec 2022 17:20:59 -0800 Subject: [PATCH] Fix rope bug in pull code --- chameleonic.p8 | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/chameleonic.p8 b/chameleonic.p8 index f61c39b..7442438 100644 --- a/chameleonic.p8 +++ b/chameleonic.p8 @@ -1199,8 +1199,11 @@ end function _vec_ang_diff(x0,y0,x1,y1) local ang0=atan2(x0,y0) local ang1=atan2(x1,y1) + return _ang_diff(ang0,ang1) +end - return (ang1-ang0+0.5)%1-0.5 +function _ang_diff(ang0,ang1) + return abs((ang1-ang0+0.5)%1-0.5) end function sum_distance(x,y,z) @@ -1387,8 +1390,8 @@ function rope:_tug(hypothetically) local touched={} for i=#ancs-1,2,-1 do - local ops_before_trash=self:_calc_push(ancs[i+1],ancs[i],ancs[i-1]) - local ops_after_trash=self:_calc_push(ancs[i-2],ancs[i-1],ancs[i]) + local ops_before_trash=self:_calc_push(ancs[i+1],ancs[i],ancs[i-1],ancs[i-2]) + local ops_after_trash=self:_calc_push(ancs[i-2],ancs[i-1],ancs[i],ancs[i+1]) local ops_to_do={} if #ops_before_trash>0 then ops_to_do=ops_before_trash @@ -1474,7 +1477,7 @@ function rope:_tug(hypothetically) (dmx!=0 and sgn0(pull_dx)!=dmx) or (dmy!=0 and sgn0(pull_dy)!=dmy) or - abs(_vec_ang_diff(pull_anc.x\8-mx0,pull_anc.y\8-my0,dmx,dmy)) > 0.125 or + _vec_ang_diff(pull_anc.x\8-mx0,pull_anc.y\8-my0,dmx,dmy) > 0.125 or sgn0(mx0-mxa)!= sgn0(mx0+dmx-mxa) or @@ -1503,12 +1506,17 @@ function rope:_tug(hypothetically) end function rope:_calc_push( - an,a0,a1 + an,a0,a1,af ) local ops={} if (an==nil) return ops - + local pull_ang=atan2( + a0.x\8 - an.x\8, + a0.y\8-an.y\8 + ) + if a0.x==a1.x then + -- no far side applying pressure? local y0,y1=_mnmx(a0.y,a1.y) local my0,my1,smy=(y0+1)\8,(y1-1)\8,1 if a0.y>a1.y then @@ -1518,10 +1526,16 @@ function rope:_calc_push( local mx,dmx if a0.x%8==0 and a0.x>an.x+7 then + local needs_good_pull=af==nil or af.x>=a1.x + if (needs_good_pull and _ang_diff(pull_ang,0.5)>=0.125) return {} + -- push left mx=(a0.x-1)\8 dmx=-1 elseif a0.x%8==7 and a0.x=0.125) return {} + -- push right mx=(a0.x+1)\8 dmx=1 @@ -1544,11 +1558,17 @@ function rope:_calc_push( local my,dmy if a0.y%8==0 and a0.y>an.y+6 then + local needs_good_pull=af==nil or af.x>=a1.x + if (needs_good_pull and _ang_diff(pull_ang,0.75)>=0.125) return {} + -- push up my=(a0.y-1)\8 dmy=-1 elseif a0.y%8==7 and a0.y=a1.x + if (needs_good_pull and _ang_diff(pull_ang,0.25)>=0.125) return {} + -- push down my=(a0.y+1)\8 dmy=1