Forum Archive

Accurate intersection detection for sprite nodes?

Drizzel

Hi again,
I am trying to detect collisions between 2 spritenodes, and currently I am using this:

If self.node1.frame.intersects(self.node2.frame) == True: 
   collision = True

Of course this is not ideal when using PNGs, as even when the image displays a triangle, the actual frame is quadratic, resulting in inaccurate intersection detection. Is there any better (more accurate) solution to this?

Apologies if I mixed up some terms, I'm a 15 yr old who only received about 1 year of slow education in the absolute basics such as the print() command and if statements. Everything else (such as using the scene module) is totally self taught.

JonB

yeah, that is a bit tricky.

You might find things look better if you check intersection to radius. i.e for square sprites, compute the radius of each sprite, then check

if abs(sprite1.position-sprite2.position) <= (sprite1.bbox.width+sprite2.bbox.width)/2.0

that wont be perfect, but will be a little better.

As an alternative, you have to implement your own simple shape collision detection

mikael

@Drizzel, is there anything more we know about the images? Mainly, is the background always black or transparent?

Drizzel

@mikael the background is always transparent.
I for now added smaller, invisible squares as a child that function as the hitbox

JonB

Great idea! You could even add multiple rectangles to fill out the shape

mikael

@Drizzel, I was thinking about getting 100% accurate detection by first doing the rectangle intersection, and if that indicates a possible hit, then placing shape 1 on a canvas in memory, then ”deleting” the non-transparent pixels of shape 2 (also properly placed). If the end result does not equal the original, you have a hit.