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

Author Topic: .  (Read 4055 times)

0 Members and 1 Guest are viewing this topic.

Helios101

  • Newbie
  • *
  • Posts: 15
    • View Profile
.
« on: January 21, 2014, 09:22:31 am »
.
« Last Edit: May 03, 2018, 05:27:28 am by Helios101 »

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Simulating Jumping
« Reply #1 on: January 21, 2014, 10:29:52 am »
Sorry that I only post some code, but I wrote an example a couple of days ago for a friend  :)

Here:

#include <SFML/Graphics.hpp>

int main(int argc, char **argv)
{
        sf::RenderWindow window(sf::VideoMode(400,400), "Jumping");
       
        sf::CircleShape player;
        player.setPosition( { 100.f, 360.f } );
        player.setRadius(20.f);
        sf::Vector2f playerVelocity;
        bool isJumping = false;
        float gravity = 125.f;
       
        sf::Clock frameTimer;
        while(window.isOpen())
        {
                sf::Time dt = frameTimer.restart();
               
                sf::Event event;
                while(window.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed)
                                window.close();
                }
                //Jumping only if the player isnt already jumping
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && !isJumping)
                {
                        playerVelocity.y = -125.f;
                        isJumping = true;
                }
                //Add gravity to velocity only if the player is in air
                if(isJumping)
                        playerVelocity.y += (gravity * dt.asSeconds());
                //Update player position
                player.move(playerVelocity * dt.asSeconds());
                //Simple collision with the ground
                if(player.getPosition().y > 360.f)
                {
                        player.setPosition( { player.getPosition().x, 360.f } );
                        isJumping = false;
                }
                window.clear();
                window.draw(player);
                window.display();
        }
       
        return 0;
}
 

Hope this code will help you


AlexAUT

Lee R

  • Jr. Member
  • **
  • Posts: 86
    • View Profile
Re: Simulating Jumping
« Reply #2 on: January 21, 2014, 10:40:24 am »
This:
Velocity = Velocity + Gravity * DeltaTime
Position = Position + Velocity * DeltaTime

Is not the same as this:
vel += vel + gravity * dt;
position += position + vel * dt;
 

You meant either:
vel = vel + gravity * dt;
position = position + vel * dt;
 

Or:
vel += gravity * dt;
position += vel * dt;
 

G.

  • Hero Member
  • *****
  • Posts: 1554
    • View Profile
Re: Simulating Jumping
« Reply #3 on: January 21, 2014, 07:32:05 pm »
Czar05, what do you mean "it didn't work"? We don't know what happens in your program so you have to describe it.

Lee R, I'm not sure I understand...
@Lee R: Ah! I thought your quote was from his code. And after reading your post 3 times I couldn't understand why you were reapeting the quote. :p Sorry!
« Last Edit: January 21, 2014, 09:48:53 pm by G. »

Lee R

  • Jr. Member
  • **
  • Posts: 86
    • View Profile
Re: Simulating Jumping
« Reply #4 on: January 21, 2014, 09:17:15 pm »
Lee R, I'm not sure I understand...

I'm not sure that you need to ;) He translated the Euler method into C++ incorrectly (and, looking at it again, I see he's only applying it when a certain key has been pressed, rather than every frame).

Quote
@Lee R: Ah! I thought your quote was from his code. And after reading your post 3 times I couldn't understand why you were reapeting the quote. :p Sorry!

No problem. I'm guessing that you weren't the only one :s
« Last Edit: January 21, 2014, 10:10:04 pm by Lee R »

Lee R

  • Jr. Member
  • **
  • Posts: 86
    • View Profile
Re: Simulating Jumping
« Reply #5 on: January 21, 2014, 10:01:10 pm »
@Lee R, when I wrote:
Velocity = Velocity + Gravity * DeltaTime
Position = Position + Velocity * DeltaTime

I meant to say I used Euler Integration. Sorry for the confusion. The code you highlighted was the code implementation.

Erm, yes, I am aware. I appear to be speaking a different language or something, so I'll bow out of this one.

Quote
When I said it didn't work I meant it didn't give the desired effect. The player would go up really high at a fast rate and disappear off the display.
That's because you were reapplying the movement from all previous frames to the current frame (i.e. position += position ...).

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Simulating Jumping
« Reply #6 on: January 21, 2014, 10:29:38 pm »
@AlexAUT, Your implementation works fine, but whats wrong with mine. When I run the program and press the jump button, the player goes up at a slow pace. Gravity doesn't have any effect. The player keeps going up at a slow then rapid pace. Its the same implementation as goes but I'm using gravity as a vector instead as a scalar value.

Nope it's not the same ;) . You are doing the real time inputs in you PollEvent-loop. Place it outside of the poll-event-loop and the example for jumping works find  :)

for you right/left movement you have to write

rect.move(-vel.x * dt.asSeconds(), 0.0);

instead of

rect.move(-vel.x, 0.0);


AlexAUT

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Simulating Jumping
« Reply #7 on: January 22, 2014, 12:18:54 am »
Was there a problem with the way I implemented the Delta Time variable (Any constructive criticism you might have):

No it's fine, but my version is shorter. The advantage of your approach is that you can always get with clock.getElapsedTime() the duration the application is running.

But:

        if(dt > 0.15f)
            dt = 0.15f;

What is this? Do you try to ensure that the delta time is always the same? If so, your application won't run frame-time independant, thus on a more powerful hardware the application will run faster! You shouldn't do this. Here is a good articel why and how you should consider the frametime into movements/physics http://gafferongames.com/game-physics/fix-your-timestep/


Thank you both for you time and patience.

You are welcome  :)



AlexAUT

 

anything