Posted: 9th Mar 2004 9:09
I created this jump code for a 2d game I am working on. Let me know what you think!

+ Code Snippet
`                    ---Jump Code---
`Written in DarkBASIC Classic by Hyrum Richter
`This code uses the trig functions sine and cosine to create a curved jumping movement.
`I've tried this code on several different sized sprites and it worked flawlessly.
`If you don't understand how Sin and Cos work, it would be well to consult a math book, as these functions are
`very often used in game programming and allow you to make some really interesting effects.
`You are welcome to use this code in your projects and modify it as you need.
`I know there are probably better ways to do jumping, but this code works, and I'm glad to share it with other people.
`You can contact me at hyrichter@email.com if you have any questions about this code.
`DON'T POST THIS CODE AS YOUR OWN!  I went to a lot of work to build this, and would appreciate being credited if it's used in your game.
`p.s. I hope you understand my comments :)

set display mode 1024,768,16
sync on
sync rate 60

load image "mypic.bmp",1
sprite 1,100,100,1

`The Variable "Jump" is used to determine which way the player should jump.  If it's set to 0, no jumping takes place.
`Jump = 1, jump straight up.  Jump =2, jump left.  Jump = 3, jump right.
`JumpLife is only used for when the player is jumping straight up, as explained later, and jumping is used to determine whether a jump sequence
`is taking place.
jump = 0
JumpLife = 0
jumping = 0

do
   if leftkey()=1 and jumping = 0
      sprite 1,sprite x(1)-5,sprite y(1),1
      if jumping = 0
         jump=2
         `x=360...This is the starting point for our jump curve.  We will be going from 360 to 180
         x=360
      endif

   endif
   if leftkey()=0 and rightkey()=0 and downkey()=0 and upkey()=0 and jumping = 0 then jump =1

   if rightkey()=1 and jumping = 0
      sprite 1,sprite x(1)+5,sprite y(1),1
      jump=3
      `x=180...Same as above, except we will be going from left to right...
      x=180
   endif

   if downkey()=1 and jumping =0
      sprite 1,sprite x(1),sprite y(1)+5,1
   endif
   if upkey()=1 and jumping =0
      sprite 1,sprite x(1),sprite y(1)-5,1
   endif

   if spacekey()=1 and jumping=0
      jumping=1
      if jump = 2
      `We must have the original player position when the jump sequence was started to calculate the curve...
      `100 must be subtracted from PlayerPosX because that is what we are multiplying the cosine of our
      `angle by and we are going left.
         PlayerPosX = sprite x(1) -100
         playerposy = sprite y(1)
      endif
      if jump = 3
      `Same as above except we are going to the right, so we add 100 to playerposX
         playerposx = sprite x(1) +100
         playerposy=sprite y(1)
      endif
   endif

   if jumping=1 then gosub PlayerJump
   sync
loop

PlayerJump:
   select jump
   case 1
   `Jumping straight up.  While jumplife is less than 20, we will move the sprite up 5 pixels.
   `As soon as the jumplife variable gets above 20, we will move the sprite down.
   `When jumplife = 40, the jumpsequence is done, and we are back where we started.
   if jumplife <20
      sprite 1,sprite x(1),sprite y(1)-5,1
      inc jumplife
   endif
   if jumplife >=20
      sprite 1,sprite x(1),sprite y(1)+5,1
      inc jumplife
   endif
   if jumplife = 40
      jumplife = 0
      jumping = 0
   endif
   endcase

   case 2
   `Jumping left
   x=x-5
   `Here we get into the fun stuff.  Our jump curve will be a half-circle, or half-ellipse in this case.
   `Angx is used to determine how far on the x-axis the player will jump.  Angy is used to determine the height of the jump
   `By altering what you multiply the sine and cosine by, you can make the player jump higher, or farther.
   angx = cos(x)*100
   angy = sin(x)*70
   sprite 1,playerposx+angx,playerposY+angy,1
   if x=180 then jumping =0
   endcase

   case 3
   `Jumping right.  Notice that instead of x=x-5 we have x=x+5.  This is because we start at the left of the curve and go to the right
   x=x+5
   angx=cos(x)*100
   angy=sin(x)*70
   sprite 1,playerposx+angx,playerposy+angy,1
   if x=360 then jumping =0
   endcase

   endselect

return