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

Author Topic: Pixel Perfect Collision  (Read 2431 times)

0 Members and 1 Guest are viewing this topic.

SnakeChips

  • Newbie
  • *
  • Posts: 2
    • View Profile
Pixel Perfect Collision
« on: July 19, 2010, 08:03:15 pm »
I know I'm doing something wrong but I have no idea what.  I'm trying to get the Pixel Perfect Collision working with this code on the site http://www.sfml-dev.org/wiki/en/sources/simple_collision_detection and just can't seem to get it.  Can anyone provide a simple working code with this for me because apparently I am not understanding something here...

Calmatory

  • Newbie
  • *
  • Posts: 16
    • View Profile
Pixel Perfect Collision
« Reply #1 on: July 19, 2010, 10:20:51 pm »
Do you understand the fundamental idea behind the pixel perfect collision detection though?

I mean, take two rectangles, then see if they intersect. If they do, make third rectangle from the part which intersects. Then scan trough that rectangle. Per every pixel, see if both of the two inital rectangles have pixel on that coord. If yes, return true. Else keep scanning.

The idea is simple. If you get errors, then the code is wrong. If you get no errors but the code doesn't work, then the logic is wrong.
The amount of effort we put into something arbitrary we do in our everyday lives is proportional to the amount we gain from it. It's fascinating how this applies to everything in our lives. Your task is to try to enjoy and make the most out of it.

SnakeChips

  • Newbie
  • *
  • Posts: 2
    • View Profile
Pixel Perfect Collision
« Reply #2 on: July 22, 2010, 05:09:41 am »
Then to me the logic is wrong with the Collision.h and Collision.cpp file i got cause I believe I'm sending everything to it correctly, thats why I want someone to give me an example that does work so I can see where the problem lies at.  Cause I assume it's the collision code he gives me.

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Pixel Perfect Collision
« Reply #3 on: July 23, 2010, 02:07:30 am »
What sprites are you using? Do they have alpha areas? Note the alpha limit variable in the PixelPerfectTest.

More helpful than providing you an example would be you showing us your code and sprites.

Anyway, here is a typical usage example. This is how I've used it successfully.

Code: [Select]
if (Collision::PixelPerfectTest(playerSprite, missileSprite))
{
    //collision occurred
    //damage player or whatever
}


Unless the code has been changed in the wiki, which it doesn't appear to have, that should work fine.

EDIT: Just had a thought, you're having trouble getting it to return true, right? You're not expecting it to stop your sprites from overlapping are you? Also, have you had the other two collision tests working?