Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help making a shape jump  (Read 1528 times)

0 Members and 1 Guest are viewing this topic.

expedo

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Help making a shape jump
« on: June 06, 2019, 11:53:06 pm »
Im trying to make a RectangleShape to jump and I tried looking at other posts but when I try it out, it either just doesn't do anything or it does something that its not suppose to do. Can anyone give me a simple explanation on how to do so? Heres my current code:
(click to show/hide)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help making a shape jump
« Reply #1 on: June 07, 2019, 10:51:29 pm »
Assuming you are already applying gravity and taking care of collisions with things like floors, to make something jump you can simply give it a y velocity that is in the opposite direction of gravity; it'll be negative (-1, for example) if your gravity is affecting y velocity in the positive direction.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Kanoha

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: Help making a shape jump
« Reply #2 on: June 17, 2019, 08:45:11 am »
So, basically, you have a boolean value: isInAir.

You're gonna want to set isInAir to true when you press space. Each frame, check wether your boolean is set to true. If that's the case, update the position of the rectangle according to the velocity and gravity:

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
    isInAir = true;
}

if(isInAir)
{
    velocity += gravity * deltaTime;
    rect.move(velocity * deltaTime);
}

(The delta time is the time that the last frame took to execute).

Finally, set your isInAir value to false when a collision with the ground is detected.

if(collisionDetected(rect, ground)) // Your collision detection algorithm
{
     isInAir = false;
}

Keep in mind that this may not be 100% accurate (it lacks things like acceleration and such) but that's the basics anyway.

Also, you may wanna fix your timestep (gafferongames.com/game-physics/fix-your-timestep/%22). It makes the whole physics handling a lot easier. With this technique, no need for any deltaTime values anymore.