Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411279 Posts in 69323 Topics- by 58380 Members - Latest Member: bob1029

March 28, 2024, 04:41:25 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Platformer Pixel Based Collisions
Pages: [1]
Print
Author Topic: Platformer Pixel Based Collisions  (Read 2979 times)
dustin
Level 6
*


View Profile
« on: March 26, 2008, 10:46:18 PM »

OK guys so I've made a platform engine before but the problem is that was for the DS so I had to be very very cautious of how much computational power I was using.  For that reason the collisions were all tile based.

Now I'm making a computer platformer and this seemed like the easiest/best way to go now that I have computing power.  It was super easy to code up so I already have it coded up and I think it looks pretty nice but I would like to know if there are any problems I'll run into further down the road if I use this model and if so what model should I switch to?

Right now it looks like this...

I have a sprite with 4 points on it one on the top,left,right, and bottom (this part is the main part I'm not sure about should I be checking just these 4 points or the whole line of the collision box?)

Each of these 4 points gets checked against my collision map.  If one of the 4 pixels on my sprite lines up with a solid pixel on the collision map the sprite gets pushed in the opposite direction of the collision and I run through collision checking again.

Does that sound right?

I just want to find out if this well mess me up somewhere down the line before I get that far in code Smiley
Logged
Zaphos
Guest
« Reply #1 on: March 27, 2008, 02:01:26 AM »

Whether you need to check more than those four points depends on how large the character is compared to the collision map's tiles.  If the collision box is at all larger than the tile width or height, you can move in to a tile like this:


You don't need to check the full line, but to avoid this you do need to check at intervals no greater than the tile size.

If you are colliding with non-square things inside tiles (eg slopes), then it's probably easiest to just check the full line.

Another issue to watch out for is how fast you can move the character in one 'step' between collision checks -- for example, if you move the character far enough that he passes through a tile between collision checks, your system isn't going to handle that.  So instead of moving the full distance at a time, you want do a collision check for at least every character_size+tile_size pixels of movement.
Logged
deadeye
First Manbaby Home
Level 10
*



View Profile
« Reply #2 on: March 27, 2008, 04:05:35 AM »

Right now it looks like this...

I have a sprite with 4 points on it one on the top,left,right, and bottom (this part is the main part I'm not sure about should I be checking just these 4 points or the whole line of the collision box?)

Top, bottom, left, and right?  Do you mean at the four corners or do you mean like the first figure in this?



Because that would cause major problems.

If you're not worried about computing power, go with the line check.  It's just as easy to check a line between two points as it is to check for multiple points on a line.

Personally, I've always just used a collision mask for my sprites, like in the second figure, and checked for overlap that way.  It takes up even more processing, but it's easier to check for small things like bullets.  It all depends on what you need your engine to do I guess.
Logged

tweet tweet @j_younger
Zaphos
Guest
« Reply #3 on: March 27, 2008, 11:41:17 AM »

Ooops, not sure why I assumed you meant the four corners  Embarrassed
deadeye's right -- testing only the midpoints is problematic.

For moving objects like bullets, usually they can have a rectangular collision box and your sprite has a rectangular collision box, so you can do pure rectangle-rectangle collision and it's quite cheap.  (eg, see badsector's post over here http://forums.tigsource.com/index.php?topic=1347.msg30644#msg30644)  I would only suggest testing individual points for collision against a more static collision map.

Or, if you may have unusual shapes you want to collide, or just want a very general solution, doing pixel perfect collision with collision masks makes good sense.  Note that you usually do want collision masks that are simple (not necessarily conforming to the exact character's shape) and static (they do not typically animate with the character).
Logged
dustin
Level 6
*


View Profile
« Reply #4 on: March 28, 2008, 10:20:32 AM »

yep I was talking about midpoints.  I know for sprite on sprite collision I'll just use rectangle-rectangle collision as it's so easy.  For the backgrounds though I just wanted to be able to draw something and have my sprite run around on it, not spend time figuring out what the rects are so pixel was the only way I could go for that...

I can see why the midpoints would cause some overlap but they never seem that big a deal really when the screen is so small Smiley .  I changed my code to make it check the whole line now so ya.

I've alredy got the tweening figured out so that's no problem.

I've never used collision masks before maybe I'll give that a try just to see how it looks at this early stage...
Logged
dustin
Level 6
*


View Profile
« Reply #5 on: March 28, 2008, 10:38:54 AM »

ooo I just added in collision masks and I'm liking it.  Super clean code and looks nice.  Thanks guys!
Logged
MekanikDestructiwKommando
Level 3
***


Let's ROCK!


View Profile
« Reply #6 on: April 10, 2008, 06:20:28 AM »

Collision masks is like.. only specific areas of your sprite gets checked for collision in a non-rectangular shape?
Somebody tell me how to do this please!
Logged

Quote
There have always been interactive experiences that go beyond entertainment.  For example, if mafia games are too fun for you, then you can always join the mafia.
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic