Help with gravity and jumping.

I've recently created a program and I'd like to be able to allow my spirte to jump. I'm able to move it left and right sucessfully but I just can't get the jumping to work. I'll post my code below and if anyone could help it would be greatly appricated (I've only started coding 2 months ago so sorry if its a stupid question ). Lines of interes possibly: 84 = Gravity and introduction of sy. 112 = Controls. 197 = Colisions.

//import ddf.minim.analysis.*;
//import ddf.minim.*;
//Minim minim;
//AudioPlayer Tune;


PImage  sprite1;//, sprite2;

int jcount = 0;  //Jump Count
int shotl = 0;
int shotr = 0;

float x=0.0; // X Position
float y=1.0; // Initial position
float sy=0; //Mapped y

float frame=0; //Frame Count
int direction=0; //Direction Facing
int fchange=0; //Frame Change

float g=-9.81; // Gravity
float k=0.25; // Spring constant
float m=1; // Mass kg
float dt=0.01; // Time step 50ms
float vy=0;// Initial velocity
float t=0; // Initial time

int shoot=0;
int travell=0;
int travelr=0;

void setup()
{
  size(1280,720, P2D);

  //Music Loading
  /*minim = new Minim(this);
  Tune = minim.loadFile("Tune.wav");
  Tune.loop();
  */


  //Image Loading
  sprite1 = loadImage("Sprite1.jpg");
  //sprite2 = loadImage("Sprite2.jpg");

  textureMode(NORMAL);
  blendMode(BLEND);
  noStroke();
}

void draw()
{
  background(0,200,255);

  //Sun
  fill(255,255,0);
  ellipse(100,100,100,100);

  //Top Platform
  fill(255,0,0);
  rect(490,310,300,30);

  //Middle Platform Left
  fill(255,0,0);
  rect(0,500,300,30);

  //Middle Platform Middle
  fill(255,0,0);
  rect(590, 500, 100, 30);

  //Middle Platform Right
  fill(255,0,0);
  rect(980,500,300,30);

  //Middle Colum
  fill(255,0,0);
  rect(625,340,30,160);

  //Bottom Platform
  fill(255,0,0);
  rect(0,690,1280,30);

  //Gravity
  vy=vy+(g-((k/m)*vy))*dt;
  y=y+(vy*dt);
  t=t+dt;
  sy=map(y,0,1,height-1,0);

  //Player
  float left = frame/2;
  float right = (frame+1)/2;

  if(direction==1)
  {
    float temp = left;
    left = right;
    right = temp;
  }

  //Draw Player
  pushMatrix();
  translate(x,sy);
  beginShape();
  texture(sprite1);
  vertex(0,0,right, 0);
  vertex(51,0,left,0);
  vertex(51,75,left,1);
  vertex(0,75,right,1);
  endShape(CLOSE);
  popMatrix();

  if(keyPressed)
    {
    if(keyCode==UP)
    {
      while(jcount<=10)
      {
        sy=sy-20;
        jcount++;
      }
    }

    if((keyCode==RIGHT)&(x!=width-50))
    {
      direction=1;
      x+=10;
      if(fchange == 10)
      {
        frame++;
        if(frame==2) frame=0;
        fchange=fchange-10;
      }
      fchange++;
    }

    if((keyCode==LEFT)&(x!=0))
    {
      direction = 0;
      x-=10;
      if(fchange == 10)
      {
        frame++;
        if(frame==2) frame=0;
        fchange=fchange-10;
      }
      fchange++;
    }

    if(keyCode==DOWN)
    {
      g=g-1;
    }

    if(keyCode==SHIFT)
    {
      shoot=1;
    }
  }

  //Bullet
  float bx = x;
  if((shoot==1)&(direction==1))
  {
    shotr=1;
    shoot=0;
  }
  if((shoot==1)&(direction==0))
  {
    shotl=1;
    shoot=0;
  }  

  fill(87);
  if(shotr==1)
  {
    rect(bx+50,sy+30, 20, 10);
    while(travelr<=80)
    {
      bx=bx+20;
      travelr++;
    }
  shotr=0;
  travelr=0;
  }
  if(shotl==1)
  {
    rect(bx-20, sy+30, 20, 10);
    while(travell<=80)
    {
      bx=bx-20;;
      travell++;
    }
  shotl=0;
  travell=0;
  }

  //Collisions
  if(sy+75>=690)
  {
    dt=0;
  }
}

Answers

  • Answer ✓

    Jumping thing example:

    float px = 200;
    float py = 300;
    float vx = 0;
    float vy = 0;
    float ax = 0;
    float ay = 0;
    
    boolean[] keys = { false, false };
    
    void setup() {
      size(400, 400);
    }
    
    void draw() {
      simulate();
      render();
    }
    
    void simulate() {
      ax = 0;
      ax += keys[0]?-.1:0;
      ax += keys[1]?.1:0;
      ay = .32;
      vx+=ax;
      vy+=ay;
      px+=vx;
      py+=vy;
      if( px<10){
        vx = 0;
        ax = 0;
        px = 10;
      }
      if( px>390){
        vx = 0;
        ax = 0;
        px = 390;
      }
    
      if (py>300 && px>100 && px<300) { 
        py=300; 
        vy=0; 
        ay=0;
      }
      if( py>420 ){
        px = 200;
        py = -100;
        ax = 0;
        vx = 0;
        vy = 10;
      }
    }
    
    void render() {
      background(64);
      strokeWeight(3);
      stroke(0);
      line(100, 300, 300, 300);
      noStroke();
      fill(0, 255, 0);
      rect(px-10, py-20, 20, 20);
    }
    
    void keyPressed(){
      if( keyCode == LEFT ){
        keys[0] = true;
      }
      if( keyCode == RIGHT ){
        keys[1] = true;
      }
      if( keyCode == UP ){
        vy = -10;
      }
    }
    
    void keyReleased(){
      if( keyCode == LEFT ){
        keys[0] = false;
      }
      if( keyCode == RIGHT ){
        keys[1] = false;
      }
    }
    

    There are 6 variables that control the movement of the thing. (px,py) is the position. (vx,vy) is the thing's current velocity. (ax,ay) is the thing's acceleration. Notice that there is a constant acceleration in the y direction (gravity). notice that a jump simply causes the y velocity to suddenly be set to a negative value (you're moving up immediately). Try jumping in the air! Whoops, did you fall off the line? It's okay.

Sign In or Register to comment.