How to make some sort of gravity system

edited December 2016 in Questions about Code

Hello Processing community, I'm relatively new to coding and I'm working on a little project for fun, It's a dodge game where u should jump and dodge things that come in your way, but I have a little problem with the jump mechanics and I was wondering if someone could clear it up a little for me. What I've done right now is this

        float dodgerPosX, dodgerPosY;
        float levelarenaPosX, levelarenaPosY;
        float speed;
        float gravity;

        void setup() {
        size(640, 360);
        frameRate(200);
         dodgerPosX = 300;
         dodgerPosY = 10;
         levelarenaPosX = 200;
         levelarenaPosY = 300;
         speed = 0;
         gravity = 0.001;
        }
        void draw()
        {
        background(51);
        text("V 1.0 (ALPHA)", 550, 350);
        fill(255);
        rect(dodgerPosX, dodgerPosY, 10, 10);
        fill(92);
        rect(levelarenaPosX, levelarenaPosY, 200, 100);
          dodgerPosY = dodgerPosY + speed;
          speed = speed + gravity;
            if ( speed < 0.65 && dodgerPosY > height-99.5) {
            speed = 0;
            gravity = 0;
          }
          else if (dodgerPosY > height-70.5) {
            println(speed);
            speed = speed * -0.65;
          }
        }
        void keyPressed()
          {
              if (key == 'd')
              {
                dodgerPosX = dodgerPosX + 25;
              }
              if (key == 'D')
              {
                dodgerPosX = dodgerPosX + 25;
              }
              if  (key == 'a')
              {
                dodgerPosX = dodgerPosX - 25;
              }
              if  (key == 'A')
              {
                dodgerPosX = dodgerPosX - 25;
              }
              if (key == 'w')
              {
                 dodgerPosY = dodgerPosY - 35;
              }
              if (key == 'W')
              {
                dodgerPosY = dodgerPosY - 35;
              }
              if (key == 's')
              {
                dodgerPosY = dodgerPosY + 25;
              }
              if (key == 'S')
              {
                dodgerPosY = dodgerPosY + 25;
              }
            }`

If you run this you'll see that my figure falls down and lands on the platform, however as i start moving it stops the gravity scripts and it just floats in the air. I have tried a lot of different things but can't seem to work, I'm not expecting a full code, just something that can point me in the right direction :D Thanks alot BromanV

Answers

  • Answer ✓

    Dont reset gravity at line 28, because that means speed will stay at zero all the time, and therefore just hang in the air. When you do that, it will see it will stop at 100 over the bottom and not 70, as i think you intended. It also seems to me that you want it to bounce, it will do that if you put -70 instead of -99.5 at line 26,

    But this is great work! you should look up a switch statement and use that instead of all those if statements in. You'll find it in the reference on processing.org or in the help menu in the IDE

  • edited December 2016 Answer ✓

    If you see this video from Daniel Shiffman, you will learn a lot of useful skills - Debugging Video

  • Thank you guys, Will be sure to check the video out aswell.

Sign In or Register to comment.