Player movement, jumping and collision

Started by
14 comments, last by L. Spiro 15 years, 2 months ago
Ok, i have implemented my basic collision detection system that i posted about here and it works ok. But i discovered that there is one annoying problem. When the player hits a wall they stop (duh!) which means setting their x velocity is set to 0, even if the left or right key is still held down. So if the wall is small enough to jump over and the player jumps then they won't continue to move forward over the wall. I'm not sure at this point whether that behaviour is what i want. It is somewhat realistic because it will mean that the player has to take a run-up to do the jump, but on the other hand it will be really annoying because you won't be able to run fast accross a level/map jumping over things and stuff. You will have to stop at every little bump and make sure you get the jump right. This will really destroy the fast paced feel that i want my game to have. So what are your thoughts on this and does anyone have a solution? I was thinking i could just keep another variable for x & y velocity which is what gets set when the left/right key is pressed. But that would be tightly coupled to the player so it's not the best O-O solution.
[Window Detective] - Windows UI spy utility for programmers
Advertisement
what does your collision code look like? Can you post a little about your movement too. In theory if you have Collision is true then velocity = 0; once you jump high enough you should no longer have collision and be able to press the right key to make the x axis increase.
I agree with wicked. If the feet of the guy are above the wall then collision should no longer be true and you should be able to move him forward. It sounds like there is a flaw in your collision detection. You also don't need to set velocity to 0 on a collision. If the sprite overlaps with a wall just bump him backwards (make sure you do that before you draw). You can make velocity go down the longer the player is colliding with the wall, but if he jumps immediately after colliding velocity could be the same or slightly less than it was.
Quote:Original post by wicked357
In theory if you have Collision is true then velocity = 0; once you jump high enough you should no longer have collision and be able to press the right key to make the x axis increase.

Yes, that's true. But because i only set velocity on key down then the player would have to press the left/right key again once they clear the wall. Do you understand what i mean? I have the key repeat rate set to 0 (i'm using SDL) so a key down event is only generated the first time the key is pressed down. That's the best way to do it isn't it.

Quote:Original post by gapern
You also don't need to set velocity to 0 on a collision. If the sprite overlaps with a wall just bump him backwards

Well that's what i thought too but think about what will happen with gravity. If velocity is not set to 0 then it will keep getting acceleration added to it so then if the player walks off a ledge they will fall with the current velocity.

You can check out my other post (in the link) to see some of my code but i will upload the most recent collision code to my website here.
[Window Detective] - Windows UI spy utility for programmers
Quote:
Well that's what i thought too but think about what will happen with gravity. If velocity is not set to 0 then it will keep getting acceleration added to it so then if the player walks off a ledge they will fall with the current velocity.


I'm not sure what you are trying to point out here. Shouldn't gravity only apply to up/down movement? Why would fall speed be affected by forward velocity? I'm not sure if I'm completely missing your point or if you are confused about something.
So are you saying that i should only keep the x velocity the same on a collision and set the y velocity to 0? I would prefer not to do such a "hack" because i don't want to code for such specialised cases. I am planning on having wind that will affect the trajectory of missiles. And i will implement it simply as a small x component of gravity. Well what if i want the wind really strong, strong enough to push the players horizontally. Now that will be the same case as what i described but for x velocity.
[Window Detective] - Windows UI spy utility for programmers
I'm sorry but you've completely lost me now. You were talking about running into a wall and wanting the character to be able to go forward when they jump high enough and now you're talking about gravity. My idea had nothing to do with gravity/the y axis at all.
Zeroing out the velocity when hitting the wall is fine, but since the key is still being held down, they should get their velocity back in the next frame. Just adding velocity once when the key is pressed isn't enough if you're going to clear it later. You need to apply acceleration or set the velocity as long as the key is down.
Quote:Original post by Vorpy
Zeroing out the velocity when hitting the wall is fine, but since the key is still being held down, they should get their velocity back in the next frame.

Yeah, but how exactly would i go about doing that? I am doing physics and collision detection in my PhysicsObject class which i want completely separate from my Player class. The Player just inherits from PhysicsObject in order to have physical behaviour (i'm not sure if it's the best design but i like it). So i want a solution that's better than just storing some variable in PhysicsObject which says "has the player started walking left/right?" because not all PhysicsObjects are players. I want the solution to work for all game objects. Of course, if there is no other solution then i guess i will have to go for a hack but there must be a better way.

@gapern: Sorry if i didn't explained things better but you have to understand that i am trying not to think of the problem in general (i.e. for both x and y axis) even though i have used a specific case (moving horizontally and jumping). So you see, i don't want to code for just this case of jumping and moving forward because the physics should apply to all cases on both axis.
[Window Detective] - Windows UI spy utility for programmers
Input, your player, collision, and physics are all entirely separate.
There is no reason why constantly adding a forward impulse to your player while the key is down would cause these things to merge.

If these things are working together (while staying separate) then it means you already have something at a higher level binding them together. Normally it is your state class.

Your game class is responsible for updating the time and input once per frame.
A state class running under the game class is responsible for distributing that input in a state-specific way. Your state class already knows what input, players, collisions, and physics are since it is binding them together.


Every frame, if the key is still down, you apply a forward impulse to the player from the state class. The input class does not know it is being applied to a player; it does not know what a player is.
The physics system does not know how the impulse was applied; it does not know what input is nor what a player is.

So there is no reason for your concerns regarding getting messy dependences and letting objects know about each other who shouldn’t; that will not happen.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This topic is closed to new replies.

Advertisement