Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075922 Posts in 44152 Topics- by 36120 Members - Latest Member: Royalhandstudios

December 29, 2014, 03:45:40 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Collison perfection?
Pages: [1]
Print
Author Topic: Collison perfection?  (Read 865 times)
theman515
Level 0
**


The code... the code!!


View Profile WWW Email
« on: October 12, 2011, 07:55:47 PM »

ok so i'm writing a small side-scroller and i'm using the separating axis therom for AABB's here.



as you can see, my guy is being held up by his gun. is there anyway to fix this by code? without redrawing the character?
Logged
Prinsessa
Level 10
*****


Ava Skoog


View Profile WWW Email
« Reply #1 on: October 12, 2011, 08:08:09 PM »

If you're just using a collision box, it doesn't have to be as wide as the image. In either case, the collision mask doesn't have to be the same as the visible parts of the image.
Logged

dum mak gam
Vatnsmyrkr, deep sea exploration: devlog
avaskoog @ twitter
avaskoog.se

Triplefox
Level 9
****



View Profile WWW Email
« Reply #2 on: October 13, 2011, 12:31:03 AM »

You can also use multiple collision boxes if you want to get super-precise and have the behavior be different for different situations or parts of the body. (almost no platformers do this)
Logged

sorceress
Level 5
*****


Location: England


View Profile
« Reply #3 on: October 13, 2011, 02:17:49 AM »

Rather than use pairs of collision boxes, you could test each pixel in the sprite against the box of the terrain.

Or simplify: look along each row of the sprite. Test only those pixels where transparency begins/ends.

Or simplify again: create a list of such pixels.
Logged

I ain't pushing no moon buttons.
ThemsAllTook
Moderator
Level 10
******


Alex Diener


View Profile WWW
« Reply #4 on: October 13, 2011, 06:30:01 AM »

Rather than use pairs of collision boxes, you could test each pixel in the sprite against the box of the terrain.

Or simplify: look along each row of the sprite. Test only those pixels where transparency begins/ends.

Or simplify again: create a list of such pixels.

I've never implemented per-pixel collision detection, but I've always been curious how one would reconcile it with animation. If the character ends up balanced on his gun, and the next frame of animation moves the gun downward, you'd either be stuck penetrating the floor, or you'd have to push the character upward, or your collision pixel mask would have to not match what you're seeing on the screen. The first seems broken, the second seems messy, and the third seems no better than just using a bounding box or two. Maybe there's some better way to do it that I'm missing?
Logged
vinheim3
Level 4
****


vinheim3
View Profile Email
« Reply #5 on: October 13, 2011, 06:34:04 AM »

Or just have the same rectangular bounding box?
Logged
DeadPixel
Level 2
**



View Profile WWW
« Reply #6 on: October 13, 2011, 06:55:51 AM »

Approach it in two phases:  broad rectangle test, followed by pixel/pixel testing.  In your case I would probably make the bounding box ignore the gun portion, and instead just cover the actual character.  That means the gun will clip over tiles, but there isn't anything necessarily wrong with that.

Step 1:  Test your character bounding box vs your level bounding boxes.  If they intersect...

Step 2:  Determine the area of collision (the smaller rectangle created by the intersection of two other rectangles).

Step 3:  Iterate through this area and only check the pixels of each sprite that fall within this area.  If any of the pixels that overlap have an alpha value > 0, then there is a collision and you can break out and return true.  Otherwise, you're clear.

EDIT:  As a note, if you DO want your gun portion involved in collision detection, then you will likely need a seperate bounding box for it only, and likely with special rules (ie, the box will prohibit horizontal movement if the gun collides with terrain, but will allow vertical so that your dude doesn't cliffhang by the barrel of his shotty.)
Logged

sorceress
Level 5
*****


Location: England


View Profile
« Reply #7 on: October 13, 2011, 08:08:16 AM »

I've never implemented per-pixel collision detection, but I've always been curious how one would reconcile it with animation.

That's a good question.Smiley

If collision = death, then it's not an issue.

If collision = blocked movement, then you have to work out where the sprite will be moved to. You can either:

-- deny the movement. The sprite won't move in the direction it is being ordered to move.

-- deny movement in x and y independently. The sprite may be able to move in one direction, even if it can't move in both.

-- nudge the sprite back along it's movement vector until it no longer collides.

-- nudge the sprite along a fixed vector, eg, a platform may push up if a collision is detected.


Logged

I ain't pushing no moon buttons.
motorherp
Level 2
**



View Profile WWW Email
« Reply #8 on: October 14, 2011, 02:02:39 AM »

Use a smaller collision AABB.  There's really no need to get any more complex than that for a game of this type.  If you're interested in learning more about collision for these sorts of games then I recommend reading these two tutorials I wrote over at shmupdev:

Art Of Collision (part 1) : Gunbird collision

Art Of Collision (part 2) : Circles, Dynamic Testing, and Time Filtering
« Last Edit: October 14, 2011, 02:13:54 AM by motorherp » Logged

theman515
Level 0
**


The code... the code!!


View Profile WWW Email
« Reply #9 on: October 14, 2011, 01:58:33 PM »

wow guys, thanks alot. Motorherp i'll be sure to read your tutorials.  Beer!
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic