-
.
-
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
-
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;
-
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!
-
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).
@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
-
@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.
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, 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
-
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