Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Need help with my jump physics code

Need help with my jump physics code

Scheduled Pinned Locked Moved C / C++ / MFC
game-devjsonhelptutorialquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Seeker2002
    wrote on last edited by
    #1

    I need some help with the jump physics example I found on the web. The following code makes my character go up and down, but I have to press and hold down the arrow-up key on my keyboard. I want my character to complete a full jump cycle without having to hold the arrow-up key down. I want to be able to press down on the key once and release to do a complete jump. Do I need to somehow include a do while loop? :confused Thanks! :)

    bool jumping;
    const float step = 0.05f; // how much the player will move per frame when jumping
    const float limit = 1.0f; // the maximum height the player can reach while jumping

    if (GetAsyncKeyState(VK_UP)== TRUE) // jump key
    {

    static float jump = 0.0f;
    static float strength = limit;

    if(jumping){

    jump += step;

    if(jump <= limit){

    strength -= step;

    player.y += (step * strength);
    }
    else{
    strength += step;

    // detect if moving the player down would put him below the ground
    // 0.0f is assumed to be the ground here

    if(player.y - (step * strength) <= 0.0f)){
    player.y = 0.0f;

    jumping = false; // the player has landed
    jump = 0.0f;
    strength = limit; // reset the strength
    }

    else{
    player.y -= (step * strength);
    }
    }
    }
    // rest of rendering...
    }

    C CPalliniC 2 Replies Last reply
    0
    • S Seeker2002

      I need some help with the jump physics example I found on the web. The following code makes my character go up and down, but I have to press and hold down the arrow-up key on my keyboard. I want my character to complete a full jump cycle without having to hold the arrow-up key down. I want to be able to press down on the key once and release to do a complete jump. Do I need to somehow include a do while loop? :confused Thanks! :)

      bool jumping;
      const float step = 0.05f; // how much the player will move per frame when jumping
      const float limit = 1.0f; // the maximum height the player can reach while jumping

      if (GetAsyncKeyState(VK_UP)== TRUE) // jump key
      {

      static float jump = 0.0f;
      static float strength = limit;

      if(jumping){

      jump += step;

      if(jump <= limit){

      strength -= step;

      player.y += (step * strength);
      }
      else{
      strength += step;

      // detect if moving the player down would put him below the ground
      // 0.0f is assumed to be the ground here

      if(player.y - (step * strength) <= 0.0f)){
      player.y = 0.0f;

      jumping = false; // the player has landed
      jump = 0.0f;
      strength = limit; // reset the strength
      }

      else{
      player.y -= (step * strength);
      }
      }
      }
      // rest of rendering...
      }

      C Offline
      C Offline
      Chuck OToole
      wrote on last edited by
      #2

      OK, this code is posted "out of context" (it simply starts with the "if" statement, not how you got there). A reasonable assumption given your problem statement is that this code is run as part of an "OnChar()" or other event *caused* by your holding a key down. If that's the case, and you want to convert this to code that runs in a loop, you need to address two things. 1) what event starts the "jumping"? 2) what event stops the "jumping"? Clearly, right now event 1) is you holding down a key, event 2) is when you let it go. Letting the key go is *not* an event, it's the absence of a key_down event. You'll need to figure out the "stop" event first.

      S 1 Reply Last reply
      0
      • C Chuck OToole

        OK, this code is posted "out of context" (it simply starts with the "if" statement, not how you got there). A reasonable assumption given your problem statement is that this code is run as part of an "OnChar()" or other event *caused* by your holding a key down. If that's the case, and you want to convert this to code that runs in a loop, you need to address two things. 1) what event starts the "jumping"? 2) what event stops the "jumping"? Clearly, right now event 1) is you holding down a key, event 2) is when you let it go. Letting the key go is *not* an event, it's the absence of a key_down event. You'll need to figure out the "stop" event first.

        S Offline
        S Offline
        Seeker2002
        wrote on last edited by
        #3

        Hi Chuck O'Toole, thanks for the quick reply. These are global variables:

        bool jumping;
        const float step = 0.05f; // how much the player will move per frame when jumping
        const float limit = 1.0f; // the maximum height the player can reach while jumping

        And the rest of the code below is contained inside of my game rendering function. The jump code works when I press the arrow-key, it just does't work correctly.

        BOOL StartGame ()
        {
        if (GetAsyncKeyState(VK_UP)== TRUE) // jump key
        {

        static float jump = 0.0f;
        static float strength = limit;

        if(jumping){

        jump += step;

        if(jump <= limit){

        strength -= step;

        player.y += (step * strength);
        }
        else{
        strength += step;

        // detect if moving the player down would put him below the ground
        // 0.0f is assumed to be the ground here

        if(player.y - (step * strength) <= 0.0f)){
        player.y = 0.0f;

        jumping = false; // the player has landed
        jump = 0.0f;
        strength = limit; // reset the strength
        }

        else{
        player.y -= (step * strength);
        }
        }
        }
        // rest of rendering...
        return TRUE;
        }

        1 Reply Last reply
        0
        • S Seeker2002

          I need some help with the jump physics example I found on the web. The following code makes my character go up and down, but I have to press and hold down the arrow-up key on my keyboard. I want my character to complete a full jump cycle without having to hold the arrow-up key down. I want to be able to press down on the key once and release to do a complete jump. Do I need to somehow include a do while loop? :confused Thanks! :)

          bool jumping;
          const float step = 0.05f; // how much the player will move per frame when jumping
          const float limit = 1.0f; // the maximum height the player can reach while jumping

          if (GetAsyncKeyState(VK_UP)== TRUE) // jump key
          {

          static float jump = 0.0f;
          static float strength = limit;

          if(jumping){

          jump += step;

          if(jump <= limit){

          strength -= step;

          player.y += (step * strength);
          }
          else{
          strength += step;

          // detect if moving the player down would put him below the ground
          // 0.0f is assumed to be the ground here

          if(player.y - (step * strength) <= 0.0f)){
          player.y = 0.0f;

          jumping = false; // the player has landed
          jump = 0.0f;
          strength = limit; // reset the strength
          }

          else{
          player.y -= (step * strength);
          }
          }
          }
          // rest of rendering...
          }

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          If I got you: use a tristate variable instead of a boolean, say jump_state.

          enum JumpState
          {
          JS_IDLE,
          JS_START,
          JS_JUMPING
          };

          //..

          jump_state = JS_IDLE; // initialization

          // ...

          // main loop
          if (GetAsyncKeyState(VK_UP) == TRUE)
          if (jump_state == JS_IDLE)
          jump_state = JS_START;
          else
          if (jump_state == JS_START)
          jump_state == JS_JUMPING;
          //...

          if ( jump_state == JUMPING)
          {
          // here do a step of jumping movement
          if ( landed )
          jump_state == JS_IDLE;
          }

          Veni, vidi, vici.

          In testa che avete, signor di Ceprano?

          C S 2 Replies Last reply
          0
          • CPalliniC CPallini

            If I got you: use a tristate variable instead of a boolean, say jump_state.

            enum JumpState
            {
            JS_IDLE,
            JS_START,
            JS_JUMPING
            };

            //..

            jump_state = JS_IDLE; // initialization

            // ...

            // main loop
            if (GetAsyncKeyState(VK_UP) == TRUE)
            if (jump_state == JS_IDLE)
            jump_state = JS_START;
            else
            if (jump_state == JS_START)
            jump_state == JS_JUMPING;
            //...

            if ( jump_state == JUMPING)
            {
            // here do a step of jumping movement
            if ( landed )
            jump_state == JS_IDLE;
            }

            Veni, vidi, vici.

            C Offline
            C Offline
            Chuck OToole
            wrote on last edited by
            #5

            Nice idea

            CPalliniC 1 Reply Last reply
            0
            • C Chuck OToole

              Nice idea

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              Thank you.

              Veni, vidi, vici.

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • CPalliniC CPallini

                If I got you: use a tristate variable instead of a boolean, say jump_state.

                enum JumpState
                {
                JS_IDLE,
                JS_START,
                JS_JUMPING
                };

                //..

                jump_state = JS_IDLE; // initialization

                // ...

                // main loop
                if (GetAsyncKeyState(VK_UP) == TRUE)
                if (jump_state == JS_IDLE)
                jump_state = JS_START;
                else
                if (jump_state == JS_START)
                jump_state == JS_JUMPING;
                //...

                if ( jump_state == JUMPING)
                {
                // here do a step of jumping movement
                if ( landed )
                jump_state == JS_IDLE;
                }

                Veni, vidi, vici.

                S Offline
                S Offline
                Seeker2002
                wrote on last edited by
                #7

                Hey guys, thanks again for y'alls help. I got it working now. You guys are great! :)

                CPalliniC 1 Reply Last reply
                0
                • S Seeker2002

                  Hey guys, thanks again for y'alls help. I got it working now. You guys are great! :)

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #8

                  You are welcome.

                  Veni, vidi, vici.

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups