Public Group

# "Moving" a sprite to negative y-axis using SFML

This topic is 2605 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello. My jumping mechanism is almost complete. At least I know my sprite can "jump".

Here's the situation. I have a sprite that is supposed to move up using velocity, acceleration, and gravity variables that help it reset its position. Which means I use SetPosition(). My velocity, acceleration, and gravity variables have positive value, which means, that if I make my character "jump", it "jumps" down and then back up. So I tried "fixing" my code like this

 float x = 400.0; float y = 300.0; float gravity = 0.5; float velocity = 3.0; float acceleration = 2.0; if(jumping == true) { y = y + velocity; Sprite.SetPosition(x, -y); acceleration = acceleration - gravity; velocity = velocity + acceleration; } if(Sprite.GetPosition().y <= 301) { velocity = 10.0; acceleration = 10.0; y = 300; Sprite.SetPosition(x, y); jumping = false; } 

So i tried fixing it by resetting the position of the sprite to the negative value of "y". When I press the "jump" button, the sprite won't jump.

How do I fix this? Cheers

##### Share on other sites
A couple of things that might help clarify your problem for you.

Position = velocity * time (units in meters)
velocity = acceleration * time (units in meters / second)
acceleration (units in meters / second^2)

That being said gravity in real life is 9.81 m/s^2. Adjust it to what every looks good in your game. But this is a constant acceleration in the y-axis. Assuming 0,0 coordinates is the upper left corner then the value of gravity should be positive. Otherwise if 0,0 is lower left the value should be negative.

Your player's acceleration in the y-axis should always be reset to the value of gravity for each frame.

In order to jump you would then need to apply an impulse that will overcome gravity. I.e. add an acceleration in the opposite direction that is larger than gravity for the frame that the jump button is pressed. The amount of acceleration will determine the height of the jump.

So your position calculations should look something like:

acceleration = gravity + (any inputs like jumping acceleration)
velocity = velocity of last frame + (acceleration * time passed since last frame)
position = last position + (velocity * time passed since last frame)

If your character is jumping in the wrong directin then change the sign of your gravity and your jumping impulse. I hope that helps a little.

##### Share on other sites

A couple of things that might help clarify your problem for you.

Position = velocity * time (units in meters)
velocity = acceleration * time (units in meters / second)
acceleration (units in meters / second^2)

That being said gravity in real life is 9.81 m/s^2. Adjust it to what every looks good in your game. But this is a constant acceleration in the y-axis. Assuming 0,0 coordinates is the upper left corner then the value of gravity should be positive. Otherwise if 0,0 is lower left the value should be negative.

Your player's acceleration in the y-axis should always be reset to the value of gravity for each frame.

In order to jump you would then need to apply an impulse that will overcome gravity. I.e. add an acceleration in the opposite direction that is larger than gravity for the frame that the jump button is pressed. The amount of acceleration will determine the height of the jump.

So your position calculations should look something like:

acceleration = gravity + (any inputs like jumping acceleration)
velocity = velocity of last frame + (acceleration * time passed since last frame)
position = last position + (velocity * time passed since last frame)

If your character is jumping in the wrong directin then change the sign of your gravity and your jumping impulse. I hope that helps a little.

How were you able to determine which corner position(0,0) would be?

##### Share on other sites
Hi,

like I pointed out in the previous thread on the subject : use 2-dimensional values for position, velocity, acceleration.

[source]
struct Vector2
{
float x,y;
};

Vector2 Acceleration;
Vector2 Position;
Vector2 Velocity;

[/source]

Otherwise, it doesn't really matter where 0,0 is. You can decide everything by yourself. It is a question of deciding how do you interpret your position data etc.

By all means, you may think yourself that 0,0 is the lower left of the screen. You just need to remember that when you draw your sprites. It means practically that when drawing the sprite you mirror the coordinates [color="#1C2837"][font="CourierNew, monospace"][color="#660066"]Sprite[color="#666600"].[color="#660066"]SetPosition[color="#666600"]([color="#000000"]x[color="#666600"],[color="#000000"] ScreenHeight [color="#666600"]- [color="#000000"]y[color="#666600"]); Assuming that originally the 0,0 is the top left of the screen.[/font]

It is good practice not the mix your objects positions in the "world space" and where you actually draw them on screen ie. "screen space".

Cheers!

##### Share on other sites
Since I have used SFML before, the top left is 0,0. Just to help out. That is designated by SFML's code, and probably due to the fact that it is OpenGL code. If you want to test it, do a simple SetPosition of some sprite to 0,0 and see where the sprite is placed. You can alter how you handle your coordinates by making a function that flips the y value. You know your screen height from creating your window, so use that value:

Screen Height - SFML Y Position = newY

This puts 0,0 at the bottom left like the first quadrant in a coordinate plane.

EDIT: Misread kauna's post. Essentially the same thing as what I said. My equation gives a converted Y, while his converts back to SFML's Y. Extra advice: creating a window with a width and a height sets the width and height of the inside of the window, not including title bar and the like. So you can use the full width and height from creation.

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633654
• Total Posts
3013168
×