Gravity in 2D, involving Sprites
Hi, I''m working on a 2D Tile Engine for a game. I have most of it besides the physics working correctly.
I have a CSprite class that contains position, acceleration and velocity for my sprites. I have errors in the jumping of my Sprites.
If anyone has a link to a helpful place or some pseudo code for jumping like physics I would appreciate it.
PS I have already read some tutorials on this site and others.
"It's easy to make things hard, but it's hard to make things easy."
To do realistic 2D gravity is quite simple actually.
You define a base jumping speed (the power the legs have to jump) and a gravity acceleration.
And for every frame, simple do something like
You define a base jumping speed (the power the legs have to jump) and a gravity acceleration.
#define LEGPOWER 50#define GRAVITY -2
And for every frame, simple do something like
if (player_starts_to_jump) speed = LEGPOWER; player_jumping = true;if (player_jumping) speed -= GRAVITY if (player_hits_platform) player_jumping = false; speed = 0;
Divide,
what about if u were to jump down a couple of tiles? you wouldn''t fall down, would you? not good.
and in your method there wouldn''t be any bouncing back up. i''m not saying that is a good thing in all games, but it''s better to let him decide, than to cut him off completely.
there should be a constant (let''s call it BOUNCINESS) indicating how "bouncy" that sprite is, with a value between 0 where the player wouldn''t bounce back up at all, to 1, where he would never stop bouncing. i suggest to set it to 0.3 at first, and experiment in order to get the best results. and another constant (let''s call it BOUNCINESS_TRESHOLD) is used for stopping the player if his bounce up would be too low by now, and should be in all cases negative. i''ll explain these better below.
a better way to do 2d physics with tiles might be something like this:
* each physics update, add a constant gravity (which should be negative) to your sprite''s vel_y.
* then move the sprite as usual, by adding vel_x to pos_x and vel_y to pos_x.
* next, check if the new position is inside a tile.
do the following if you want the player bounce back up slightly.
* if it is, then move it up by the amount that it is "inside" that tile times two (for example, a tile has dimensions {0, 0, 10, 10}. your player is at {5, 15} before the gravity was applied, and at {5, 8} after it was. therefore it''s inside that tile by 2 units (10 - 8). so we move it up by double that amount so now the player would be at {5, 12}.). then, if vel_y is less than BOUNCINESS_TRESHOLD, set it to 0. otherwise multiply that sprite''s vel_y by BOUNCINESS_TRESHOLD (it is negative so that the sprite will bounce in the opposite direction).
and if you don''t want him to bounce (note that you can do the same effecy by using the above described method and setting BOUNCINESS to 0) just do this:
* if it is, then move it up by the amount that it is "inside" (for example, if we have the same tile as above, and the player is in the same positions, then his final position should be {5, 10}).
that should give you some ideas on how to implement a good 2d physics model. i''ve only described the vertical (on the y axis) movement, collision detection (i haven''t described how do it it, but what it needs to do) and collision responce (the process of doing whatever you need to do when a collision has occured - in our case moving the player up and inverting his vertical velocity to make him bounce up), but if you''re smart enough should be able to implement all the above said things and figure out the rest (collision responce on the x plane, which is very similar). if not, you have to read more stuff about this.
on a totally separate topic, if you don''t understand what i said above because of the way i said it (i know i''m not good at explaining things...), please tell me what you don''t understand and i''ll try to explain it better.
one more thing, i referenced to sprites and players as the same thing. so don''t get confuzed!
please tell me if i''ve helped.
---
shurcool
wwdev
what about if u were to jump down a couple of tiles? you wouldn''t fall down, would you? not good.
and in your method there wouldn''t be any bouncing back up. i''m not saying that is a good thing in all games, but it''s better to let him decide, than to cut him off completely.
there should be a constant (let''s call it BOUNCINESS) indicating how "bouncy" that sprite is, with a value between 0 where the player wouldn''t bounce back up at all, to 1, where he would never stop bouncing. i suggest to set it to 0.3 at first, and experiment in order to get the best results. and another constant (let''s call it BOUNCINESS_TRESHOLD) is used for stopping the player if his bounce up would be too low by now, and should be in all cases negative. i''ll explain these better below.
a better way to do 2d physics with tiles might be something like this:
* each physics update, add a constant gravity (which should be negative) to your sprite''s vel_y.
* then move the sprite as usual, by adding vel_x to pos_x and vel_y to pos_x.
* next, check if the new position is inside a tile.
do the following if you want the player bounce back up slightly.
* if it is, then move it up by the amount that it is "inside" that tile times two (for example, a tile has dimensions {0, 0, 10, 10}. your player is at {5, 15} before the gravity was applied, and at {5, 8} after it was. therefore it''s inside that tile by 2 units (10 - 8). so we move it up by double that amount so now the player would be at {5, 12}.). then, if vel_y is less than BOUNCINESS_TRESHOLD, set it to 0. otherwise multiply that sprite''s vel_y by BOUNCINESS_TRESHOLD (it is negative so that the sprite will bounce in the opposite direction).
and if you don''t want him to bounce (note that you can do the same effecy by using the above described method and setting BOUNCINESS to 0) just do this:
* if it is, then move it up by the amount that it is "inside" (for example, if we have the same tile as above, and the player is in the same positions, then his final position should be {5, 10}).
that should give you some ideas on how to implement a good 2d physics model. i''ve only described the vertical (on the y axis) movement, collision detection (i haven''t described how do it it, but what it needs to do) and collision responce (the process of doing whatever you need to do when a collision has occured - in our case moving the player up and inverting his vertical velocity to make him bounce up), but if you''re smart enough should be able to implement all the above said things and figure out the rest (collision responce on the x plane, which is very similar). if not, you have to read more stuff about this.
on a totally separate topic, if you don''t understand what i said above because of the way i said it (i know i''m not good at explaining things...), please tell me what you don''t understand and i''ll try to explain it better.
one more thing, i referenced to sprites and players as the same thing. so don''t get confuzed!

please tell me if i''ve helped.
---
shurcool
wwdev
So is this game''s view top-down or cross-section style? I don''t get exactly what the problem is. Can you fill me in some more?
i assumed it was what you call "cross-section style", because it has gravity. if it were top-down view, where would the gravity come in?
---
shurcool
wwdev
---
shurcool
wwdev
Yes, it is side-viewed like Mario.
What I would like to implement is a constant gravity effect, so that I could fall off of tiles. Not only when I jump. Thanks for the good ideas guys, I''m still piecing it together in my mind.
More replies would be great, any idea helps |:^)
What I would like to implement is a constant gravity effect, so that I could fall off of tiles. Not only when I jump. Thanks for the good ideas guys, I''m still piecing it together in my mind.
More replies would be great, any idea helps |:^)
"It's easy to make things hard, but it's hard to make things easy."
umm, why would a player sprite bounce? most organism use their legs to absorb the shock during a landing thus dont bounce (in fact most organism are not stiff enough, nor keep enough energy on impact to bounce).
anywho, it is best NOT to add gravity to sprites that are on solid ground. the reasoning simply being that gravity is a force that causes accelertaion. while on the ground, the object exerts enough force to negate gravity (otherwise it would be a pancake). thus its safe to not deal with the force unless it would change things. when the player exerts force against gravity (ie jump), then you deal with adding the force to the players movement vector (really just increase/decrese dy). if the player moves off a tile and there is no solid tile beneath, then you begin to add gravity again.
if you constantly add the force of gravity you could cause some sluggishness and complications (like trying to deal with proper acceleration due to gravity) i wont supply code since its trivial to implement and you should know c/c++/java/basic/whatever yoru coding in to write the code. partial psudeo code was given, now apply the ideas and create a solution, thats all thats left.
anywho, it is best NOT to add gravity to sprites that are on solid ground. the reasoning simply being that gravity is a force that causes accelertaion. while on the ground, the object exerts enough force to negate gravity (otherwise it would be a pancake). thus its safe to not deal with the force unless it would change things. when the player exerts force against gravity (ie jump), then you deal with adding the force to the players movement vector (really just increase/decrese dy). if the player moves off a tile and there is no solid tile beneath, then you begin to add gravity again.
if you constantly add the force of gravity you could cause some sluggishness and complications (like trying to deal with proper acceleration due to gravity) i wont supply code since its trivial to implement and you should know c/c++/java/basic/whatever yoru coding in to write the code. partial psudeo code was given, now apply the ideas and create a solution, thats all thats left.
hey, i was just giving him ideas, nothing more. in my 2d game, worms can bounce. i don''t know what he''s doing, but it''s always nice to know "i can do that if i want to" rather than "i''m not doing that because i don''t want and i can''t either".
and sure, you can modify my method: don''t add gravity if you''re on ground. it would''ve got canceled anyway (if BOUNCINESS_TRESHOLD is high enough), so it doesn''t matter.
it seems as if Amythius doesn''t really understand my method, or does not have the possibility to accomplish it (because it needs a nice collision detection algorithm too), and seeks for an easier method (because if done properly, it''s fast, effective, realistic, and most importantly does all the things he wants it to do).
that disappoints me, but what can i do. :-\
---
shurcool
wwdev
and sure, you can modify my method: don''t add gravity if you''re on ground. it would''ve got canceled anyway (if BOUNCINESS_TRESHOLD is high enough), so it doesn''t matter.
it seems as if Amythius doesn''t really understand my method, or does not have the possibility to accomplish it (because it needs a nice collision detection algorithm too), and seeks for an easier method (because if done properly, it''s fast, effective, realistic, and most importantly does all the things he wants it to do).
that disappoints me, but what can i do. :-\
---
shurcool
wwdev
quote:
Original post by Amythius
What I would like to implement is a constant gravity effect, so that I could fall off of tiles. Not only when I jump. Thanks for the good ideas guys, I''m still piecing it together in my mind.
Maybe you could try something like this. It''s somewhat similiar to divide''s method, but it should also work so that you can fall off tiles. Each frame, do something like this:
if (jumpKeyIsPressed()) player.yspeed = LEGPOWER;...if (!playerIsOnPlatform()) player.yspeed -= GRAVITY;player.y += player.yspeed;if (playerHitsCeiling()){ player.y = ? // you''ll have to calculate this, // so that the player doesn''t go through // the ceiling player.yspeed = 0;}if (playerHitsFloor()){ player.y = ? // same thing as above, calculate this // to make sure the player doesn''t go // trough the floor player.yspeed = 0;}
Hi!
about "what about if u were to jump down a couple of tiles? you wouldn''t fall down, would you? not good."
Just take the code Divide posted and change it like this
if (player_starts_to_jump)
{
player_jumping = true; // Just so that the
// player can''t hold down
// the jump key
speed = LEGPOWER;
}
if (player_hits_platform)
{
player_jumping = false; // Now the player can press
// the jump key again
speed = 0;
}
else
speed -= GRAVITY;
about "what about if u were to jump down a couple of tiles? you wouldn''t fall down, would you? not good."
Just take the code Divide posted and change it like this
if (player_starts_to_jump)
{
player_jumping = true; // Just so that the
// player can''t hold down
// the jump key
speed = LEGPOWER;
}
if (player_hits_platform)
{
player_jumping = false; // Now the player can press
// the jump key again
speed = 0;
}
else
speed -= GRAVITY;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement