In a speed based system where player.x and player.y +=1 when a button is pressed, a 3pt quadratic can be calculated via
function quad(p0,p1,p2,t) --t from 0->1;
x=p0.x*(1-(t))^2+2*(t)*p1.x*(1-(t))+p2.x*(t)^2
y=p0.y*(1-(t))^2+2*(t)*p1.y*(1-(t))+p2.y*(t)^2
return x,y end
p0: the player's initial position
p2: the player's intended end position
p1: the point between that guides the curve up/down
t: the timer+=.?; @0 its at p0, @1 its at p2
If applying this to a player, the directions of the quadratic can be manually written out based on last or current buttons pressed (like ex below). If I'm doing 8 way movement that's 8 cases to writeout...N..NE..E..SE..S.
My question is how can I apply this to a velocity based system. The code below outlines a situations where the player p can move around, and when button 4 is pressed the player jumps in the static 'to the right' manner via the quad() function. This could be repeated for the remaining cases as outlined above, but if I'm using velocity, I have dx/dy change in speed that give me direction vectors. So shouldn't I be able to use those 2 direction vector values instead of the 8 buttons to determine direction and the speed of movement (assuming I want dynamic values of t and not static like above)? I would think so, but then the question is how do I use velocity with quad() or what do I need to do to modify quad() to work with velocity. The intent being while moving: if a player pressed btnp(4) the quadratic would activate and do the 'hop' according to the direction vectors of dx/dy. Magnitude of dx/dy would also potentially play a role in how fast t moves from 0 to 1 (aka the speed of the jump).
p={jump=false,t=0,x=30,y=60,dx=0,dy=0,r=4,spd=.5,
update=function(self) --runs @30fps in the gameloop
self.dx*=.7; self.dy*=.7
if self.jump==false then
if btn(0) then self.dx-=self.spd end
if btn(1) then self.dx+=self.spd end
if btn(2) then self.dy-=self.spd end
if btn(3) then self.dy+=self.spd end
if btnp(4) then self.jump=true end
else
if not init then
--static jump right from current position, endup 40 units to the right.
x0=self.x;y0=self.y --position started at
x1=self.x+20;y1=self.y-20 --curve x 20 right, y 20 up
x2=self.x+40;y2=self.y --end position
init=true
end
self.t+=1/32
if self.t<=1 then
self.x,self.y=quad({x=x0,y=y0},{x=x1,y=y1},{x=x2,y=y2},self.t<1 and self.t or 1)
else
self.jump=false self.t=0 init=nil
end
end
if abs(self.dx)<.01 then self.dx=0 end; if abs(self.dy)<.01 then self.dy=0 end
self.x+=self.dx; self.y+=self.dy
end
}
Should I be taking the Euclidian of the dx/dy and using that as a multiplier for t? If the game used a mouse the p2 could always be the mouse position, but that's not the case in this example. I want a method for just the 5 keyboard buttons.