Help with physics

if (keyCode=='W') {
    p.setY(p.getY()-10);
  }
  if (keyCode=='S') {
    p.setY(p.getY()+10);
  }

so, how can i create physics with this? i want to make a jump animation...

Tagged:

Answers

  • Answer ✓

    Here's a better example for you:

    float py = 100, vy = -2, ay = 0.032;
    
    void setup(){
      size(200,200);
      rectMode(CENTER);
      fill(0,64,0);
      stroke(0,255,0);
    }
    
    void draw(){
      background(0);
      vy+=ay;
      py+=vy;
      if(py>width-11){
        py = width-11;
        vy = 0;
      }
      rect(100,py,20,20);
    }
    
    void keyPressed(){
      if(keyCode == ' '){
        vy -= 1;
      }
    }
    
  • thanks tfguy

  • @tfguy44 how do you use that with this code?

    Runner run;
    public class Runner{
     int x,y;
     public Runner(int xpos,int ypos){
       x=xpos;
       y=ypos;
     }
     void display(){
       fill(0);
       rect(150,y,50,50);
     }
    }
    
    void setup(){
      size(800,800);
      run=new Runner(40,height-50);
    }
    void draw(){
      run.display();
    }
    
  • You have a Runner object, run. Objects of type Runner have two variables that they use - x and y. These are that Runner object's position.

    When a Runner object is created, it is told where its initial position is.

    You may want to add additional variables to your Runner class. In addition to a position, you may want to give each runner a velocity and an acceleration (in both the x and y directions, probably!). In my sample code, I had three variables - py (y position), vy (y velocity) and ay (y acceleration).

    Each frame, the y velocity changes by the y acceleration, and the y position changes by the y velocity. That is what vy+=ay; py+=vy; does.

    Let's walk through adding these things to your Runner...

  • Here we have added the new variables. Notice that I have also renamed some of your variables, and that the runner is now being drawn at it's py AND px position (not the constant x position of 150 like you had before). Also notice that I have switched over to using floats for the variables, because they might not be whole numbers (integers).

    Runner run;
    class Runner{
     float px,py;
     float vx,vy;
     float ax,ay;
     public Runner(float px_initial, float py_initial){
       px=px_initial;
       py=py_initial;
       vx=0;
       vy=0;
       ax=0;
       ay=0;
     }
     void display(){
       fill(0,196,0);
       rect(px,py,50,50);
     }
    }
    
    void setup(){
      size(800,800);
      run=new Runner(40,height-50);
    }
    void draw(){
      background(0);
      run.display();
    }
    

    I've swapped the colors of things about too. Notice that, other than the color changes, everything seems the same. This is because I am not using the new variables yet.

  • Runner run;
    public class Runner {
      float px, py;
      float vx, vy;
      float ax, ay;
      public Runner(float px_initial, float py_initial) {
        px = px_initial;
        py = py_initial;
        vx = 0;
        vy = 0;
        ax = 0;
        ay = 0;
      }
      void draw() {
        simulate();
        render();
      }
      void simulate() {
        vx+=ax;
        vy+=ay;
        px+=vx;
        py+=vy;
      }
      void render() {
        fill(0, 196, 0);
        rect(px, py, 50, 50);
      }
    }
    
    void setup() {
      size(800, 800);
      run = new Runner(40, height-50);
    }
    void draw() {
      background(0);
      run.draw();
    }
    

    I decided that your Runner's display() function should really be draw(). And that draw() should do two things - first, it should simulate() the movement of the runner. Then it should render() the runner.

    Notice also the new logic in simulate(). The velocities are updated based on the accelerations, and the positions are updated by the velocities. Yet the runner still does not move! Can you see why?

    ...

    That's right. The velocities and accelerations are all 0!

  • Runner run;
    public class Runner {
      float px, py;
      float vx, vy;
      float ax, ay;
      public Runner(float px_initial, float py_initial) {
        px = px_initial;
        py = py_initial;
        vx = 2;
        vy = -5;
        ax = 0;
        ay = 0.032;
      }
      void draw() {
        simulate();
        render();
      }
      void simulate() {
        vx+=ax;
        vy+=ay;
        px+=vx;
        py+=vy;
      }
      void render() {
        fill(0, 196, 0);
        rect(px, py, 50, 50);
      }
    }
    
    void setup() {
      size(800, 800);
      run = new Runner(40, height-50);
    }
    void draw() {
      background(0);
      run.draw();
    }
    

    Now the runner appears to jump! Why? I gave it some meaningful values for velocity and acceleration. It moves a constant 2 in the x direction every frame, and also starts with an upward movement of -10 in the y-direction. This movement is not constant, however, as there is a small amount of acceleration downwards in the y direction.

  • Now all you need to do is add some boundary conditions (to stop it from accelerating off the bottom of the screen, for example (as my first example did)), and re-add the key pressed function that will check which key was pressed and change the Runner's values accordingly (which your first post of code did). Attempt this yourself and post your attempt before asking for more help.

  • @tfguy44 one more question, can you help me with creating a delay

    Runner run;
    float py = 100, vy = 0, ay = 0.032;
    
    public class Runner {
      public Runner(float ypos) {
        py=ypos;
      }
      void display() {
        fill(0);
        vy+=ay;
        py+=vy;
        if (py>width-50) {
          py = width-50;
          vy = 0;
        }
        rect(150, py, 50, 50);
      }
    }
    void keyPressed() {
      if (keyCode == ' ') {
        vy -= 3;
      }
    }
    
    void setup() {
      size(600, 600);
      run=new Runner(height-50);
    }
    void draw() {
      background(255-50);
      run.display();
    }
    
  • The code you have just posted shows us that you are trying to use classes without understanding them. This is the wrong approach.

    To see what you are doing wrong, create two Runners, and have each start at a different y position.

  • Save your Runner variables inside your Runner class, not globally. Then, if you create two Runners, each will have its own variables that it updates.

  • @jeremydouglass I only need one runner, I am trying to make an infinite runner game

  • @tfguy44 fixed it :D I just worked through it with logic

    Runner run;
    
    public class Runner {
      float py = 100, vy = 0, ay = 0.032;
      public Runner(float ypos) {
        py=ypos;
      }
      void display() {
        fill(0);
        vy+=ay;
        py+=vy;
        if (py>width-50) {
          py = width-50;
          vy = 0;
        }
        rect(150, py, 50, 50);
      }
    }
    void keyPressed() {
      if (keyCode == ' ') {
        if (run.py==height-50)
          run.vy -= 3;
      }
    }
    
    void setup() {
      size(600, 600);
      run=new Runner(height-50);
    }
    void draw() {
      background(255-50);
      run.display();
    }
    
  • I only need one runner

    @lolnyancats -- that is true.

    I was simply trying to explain why @TfGuy44 was saying you were using classes wrong. Looks like you figured it all out.

Sign In or Register to comment.