Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / 2D platformer collision

Author
Message
xxelixx
15
Years of Service
User Offline
Joined: 6th Dec 2008
Location: CodeLand
Posted: 2nd Oct 2011 22:24
I am having a little trouble with my collision. My player disappears when you walk on the ground. Any help is appreciated.

29 games
19
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 3rd Oct 2011 00:41
Hi xxelixx, I think I've solved your immediate question.

When I ran your code the following happened:

Player box would fall onto the green platform. When I moved left or right, the player box would go so far and then position itself and the far left of the screen and slowly move upward until it dispeared off the top of the screen.

I'm guessing this is what you mean by "My player disappears when you walk on the ground".

Ok then, for the player box to be on the far left of the screen the boxes x coordinate must have been set to zero for some reason.

Looking at the function "scanX", if a horizontal collision has occured then you reposition the player sprite at its old position. In this case "newX = sprx".

However, newX can only equal sprx if the conditions of the if statements are met. If the conditions of the if statment are not met then newX = 0.

For some reason, the conditions of the if statements were not being met so newX was being set to zero. This sets the player's x position to zero, which just so happens to put the player box in the left most had wall of green sprites.

When you deal with falling, if the player box hits the ground you then position the player box to the old y coordinate plus 1 pixel.

When the player box has been put into the left hand wall, there is constant collision and the box moves up and eventually off the screen.

The way I got around this was to modify the scanX function by setting newX to the player's current x coordinate before the if statements.




I have to admit, I don't know what:

If sprite x(spr) <= sprite x(tile) - 6

and

If playerwidth >= tilewidth + 6

actually do. I think this may be where the real problem lies as when I took them out, this achieved the same effect as putting in the line "newX = sprite x(spr)". You might want to have a look at this.

The other issue with the collision code is that when the player is touching the side of a green tile, the player box starts to move upward. Moving sideways into a green tile is being treated as if the player has fallen on it, i.e. move the player up 1 pixel. I also noticed that when this happens the player can not move horizontally so they are completely stuck. This means that the player can not fall through any of the gaps in the platform.

I think what you need to do is to first check whether the collision is horizontal or vetical (i.e. has the player moved sideways into a green tile or have they fallen on top of it) and then work out what they new coordinates are based on how the player has collided with the map.

One other thing, you seem to be mixing up floats and integer which, while it doesn't seem to be affecting the running of you, is something you may want to sort out.

Hope this helps and let us know if you sort out the collision or not.

Login to post a reply

Server time is: 2024-11-24 01:34:14
Your offset time is: 2024-11-24 01:34:14