0

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.

kite
  • 207
  • 1
  • 9
  • Can you describe your desired gameplay outcome? Set the scene for us a bit — what's the perspective of this game? Is it 2D or 3D? What should the player see happen when they press their buttons in various combinations? Once we thoroughly understand the gameplay you want to create, we can help you create it, even if the solution might have nothing to do with the formulas you've shared. – DMGregory Jan 28 '22 at 10:09
  • @DMGregory I added 2d tag for further clarity. I feel I am clearly describing a 2 axis situation and have thoroughly explained all your other concerns. Redefinition of the problem can always be requested: I want a quadratic that moves and operates according to the 2 velocity values of the player (dx/dy). The method should be as versatile as the function for speed values in the OP. Where I can define 3 pts of movement. The requested general look is that of a bunny hop in the direction of the velocity values, in a quadrant where x+ increases to the right; y+ increases downwards as per the op ex. – kite Jan 28 '22 at 22:23
  • You can easily just do movement multipliers on your velocity on button press, but making movement multipliers that follow parabola following general predefined/envisioned shapes I am confused on how to create. You move in a direction and buildup velocity....faster...faster. U press a button and ur movement should change from a straightline to a curve for xtime. The setup for this is already in the code. The math for doing it is not. – kite Jan 28 '22 at 22:29
  • 1
    The way I'd approach this is, whenever you want to start a hop, imagine for a moment that your character is a projectile. Then you can use existing answers for how to plan a projectile arc through a specific point or with a specific gravity that hits a target point (your launch position + velocity * hop duration). Then you can project positions from that 3D ballistic arc down to a 2D rendered output for your top-down perspective. – DMGregory Jan 28 '22 at 22:37

0 Answers0