• Advertisement
Sign in to follow this  

2D platform player physics

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a 2D platform game which will use physics for all the objects, including the player. I've got the game going, physics implemented, but it doesn't work correct, I will try to explain... I'm using the standard way of including physics, which during the object's loop (for the player), it does this: 1. Forces = all forces acting upon the player (which for now will only be gravity): Forces.Y = -gravity 2. Acceleration = Forces / Player_Mass 3. Velocity += Acceleration * Time 4. Position += Velocity * Time First question is how do I correctly implement moving the player? For right now I am apply a horizontal force depending on the player's facing direction to the static Forces vector. Next question is falling collisions, the player falls and falls until the player hits a solid object (or land in this case), for now I set it so the player's position is automaticly set above the ground... but I notice doing console-based debugging that the player is unable to jump because the velocity keeps decreasing, and I tried to manually set the velocity vertical value to 0 on collision, but this breaks vertical velocity then, not allowing to jump unless I add special code to skip manually setting the vertical velocity. Example: if Object_Collision = true then player_feet_position.y = object_top if player_is_jumping = false then player_velocity.y = 0.0 end if end if Any help is apreciated, I've tried searching google and here and nothing comes up too exact, or may it be questions not related to what I need. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
1) Yes you can apply a force depending on the direction, you will need to simulate friction if you don't want your player to slide forever (and want a physically accurate game).


2) I don't understand what you mean by the velocity keeps decreasing -
Do you mean that even after he hits the floor his velocity keeps decreasing?
Perhaps you need to apply the reaction force (upward force) of the floor.



Share this post


Link to post
Share on other sites
For ground and air friction I'm using damping, ie:
for an example: velocity.x = (velocity.x * 0.95)

For a work around with the velocity keeps increasing, I added it so when the player hits the floor it sets the velocity to zero (0.0).

Share this post


Link to post
Share on other sites
but my pain problem I'm having now is that when the player hits objects (like the floor, the ceiling, walls) it doesn't interact correctly looks like the player is jerking back and forth.

Share this post


Link to post
Share on other sites
if anyone has tried making a 2d platformer game before, please contact me or post helpful information or links that I can't find off the bat. Anything would be appreciated.

Share this post


Link to post
Share on other sites
Quote:
Original post by digital_phantom
but my pain problem I'm having now is that when the player hits objects (like the floor, the ceiling, walls) it doesn't interact correctly looks like the player is jerking back and forth.


Ok, I've noticed this before on my 2D games when I forget to apply physics and forces BEFORE I move my character.

So for example, lets say you have a velocity.x = 5. So now, we apply our velocity to our position, so position.x = position.x + velocity.x, update sprite, and apply a negative velocity to move the player away from the object. Next update our position.x = position.x + velocity.x is going to be negative, therefore the player is pushed away from the object. BUT now we can again apply a velocity.x = 5 because we are no longer in the object, and repeat. Do you see how this can cause that weird jitter?

The best way to deal with this I've read about is using a world physics manager. What you can do then is take "potential movements" such as potential.x = position.x + velocity.x, then see if that potential.x will collide with an object. If so, either make the potential.x = potential.x +/- objectEdge.x, or say we have violated our potential so we cannot move any further, therefore potential.x = position.x.

I'm sure there are far superior ways of handling this through pure physics, but this is a quick and easy way of doing it for 2D games.

Share this post


Link to post
Share on other sites
Thanks Cthulhu32,

I think pre-calculating if the object will be collided in the next step will work in my case, since I'm already doing the rest.

Share this post


Link to post
Share on other sites
Is your background tile-based? If it is, and all of your tiles are of fixed size, you can calculate the correct tile position for your object and it will be pushed back (so to speak) to the correct location in tiles.

Share this post


Link to post
Share on other sites
Yes, the background is tile-based, the positions for that are correct.
But there will also be objects, which will act just like a player, meaning being anywhere.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement