Skip to main content
gamedev.net
🔒 Locked

2d Platform Collision

Started by Xero-X2 4 replies 500+ views
Original Post
Xero-X2
Xero-X2
Ive been working on a small Platform game and have basic stuff done, but my collision detection has some flaws in it. Heres hows its done: I compare the bounding Points to the collision polygons of the world. after finding which points are colliding with a given polygon, Upper two are colliding and not lower two = Move down (out of ceilng) Left two are colliding = Move right (out of wall) Right two are colliding = Move left (out of wall) Lower two are colliding and not upper two = Move up (out of floor) Over all it worked good, that was before I impliemented jumping Heres are the problems: 1.When falling down aganst a wall, lower point and upper point collide with diffrent objects so it dosn't register properly 2.When jumping up to a ledge if you fall short you can "Climb" up the rest of the way because the upper point no longer collides, this means it treats it as a floor, forcing you onto the ledge. Further Discription: The world consits of Objects placed at any point, each Object has a collision "polygon" the walls are vertical, and the top & bottom are any given line having any sloap, vertical lines are not allowed, all of these are ensured conditions. I already understand my methods short comings and how it is working. But I can't seem to find a way to do it better then this. Any help would be appreciated. "I seek knowledge and to help those who also seek it" Edit: Claraified some sentences [edited by - Xero-X2 on April 17, 2004 6:55:01 PM]
"I seek knowledge and to help those who also seek it"
BrentBarrett
BrentBarrett
I came across this same problem when I was programming collision for my side-scroller/platformer. I had all the same conditions you do (no vertical lines, otherwise any slope is fine). For this example solution I'm assuming the use of a simple square platform with a bounding box or four seperate lines of collision that encompass its dimensions. If you've already got a formula to figure out which side of the square your character is hitting, then this shouldn't be too difficult to figure out. The reason your guy is going up is because he's colliding with both the side of the square as well as the the top of the square, like this:

________
| man |
___________|___ |
| |__|___|
| platform |
|_____________|


In your collision loop, you're probably checking for collision with the top of the square before the bottom or sides. Because of that, your character will be pushed up rather than to the side when colliding with both. To solve this, check for which side of the square you're colliding with the most. Lets look at my example again, this time with the overlapping area "shaded" in:

________
| man |
___________|___ |
| |XX|___|
| platform |
|_____________|

Notice that the "man" is two "X"s in, but only one "X" down on the platform. This means he's colliding more with the top than he is the side, and that's how it'd appear to the player as well, so go ahead and push him up. By doing a few simple checks between X and Y positions you can find out which side he is colliding with more. For the above example you could've found this out by using the following formula:

(platform.Xposition + platform.Width) - man.Xposition;
(platform.Yposition + platform.Height) - (man.Yposition + man.Height);

If you compared the two different results of those equations, you'd know which side was colliding more. Now, those formulas only work for the example above, you'd have to do more to account for other instances.

[edited by - BrentBarrett on April 17, 2004 10:54:34 PM]

[edited by - BrentBarrett on April 17, 2004 10:56:03 PM]
leiavoia
leiavoia
as a side note, don''t check the vertices, check for the whole edge. otherwise, besides being slower, this kind of collision won''t work:


**********
* *
****************
* * * *
****************
* *
**********

BornToCode
BornToCode
Yo, I had the Same problem. So Instead what i did is that i placed Values inside the Tiles that i want to be able to collide with the i check those values for Collisions for example
supposed you have this:

MAPARRAY[Y][X] Right, Now Let''s say that you have an Tile at Location x=100,y=200, and you stored an one into it. Now all you have to do to check for Collision is take you Playerx and Playery Position and Divide it by the size of each Tiles for Example, if our tiles are 16*16 then simply do
MAPARRAY[PLAYERY/16][PALYERX/16],It will return the Value that is the inside the Array where the Player is Standing. So to check for a tile that is in Front of you All you have to do is
MAPARRAY[PLAYERY/16][(PLAYERX/16)+1] and it will return the Tile that is in front of you. Then you can check the value where you ar standing, and if it is an value that is collision you know what to do. The same method can be apply to you jumping and falling routine. That is the method i used when i recoded ninja gaiden.If it''s not Too Clear the Email me. Peace
Xero-X2
Xero-X2
BornToCode: my world is object based. not tile based.

Im working with it, buy im not making much progress yet.

"I seek knowledge and to help those who also seek it"
"I seek knowledge and to help those who also seek it"
Xero-X2
Xero-X2
I have fixed it, thanks for trying to help, In the end I figured that if any points were to pass thought the walls of any objects then it would properly collide with the wall. I belive adding future more precise detection to prevent leiavoia''s Seneario will have to be made, but for now it works. Thanks again for helping.

"I seek knowledge and to help those who also seek it"
"I seek knowledge and to help those who also seek it"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.