I'm wanting to use the rect:intersection method in the lua api to do exactly what it states its use case as. I have a sprite and I want to use the intersection of it and another sprite as a new rect that represents that intersection. The problem is, the result I get back is just a rect with the exact same width and height as the first rect regardless of how much intersection there is.
To debug this, I tested the rect:intersection method in an isolated case:
The result I get above is "test collision overlap: (0,0,20,20)" where I would expect to get a rect of (0,0,10,20). If I change the overlap of the sprites, I get the same result.
Hello, the reason you are seeing this result is because the getCollideRect function will return exactly what you set with setCollideRect, since both colliders (rectangles) are the same size, the result is 0, 0, 20, 20. When you call setCollideRect, the first two arguments are x and y, but they are relative to the sprite size, not the sprite position (set with moveTo, moveBy).
To achieve the result you want, you can use the sprite size directly with getBoundsRect. The example would look something like this:
EDIT: If collisions are what you are after, the easiest way to detect sprite collisions is with the integrated collision system (moveWithCollision, overlappingSprites etc.). If you want to implement your own collision system, for example through the geometry rects and still use sprites, then you can use something like this:
Thanks! I would normally just use the collision system but I need to know exactly how much overlap there is before I decide to do anything with the collision. I think your fast_intersection reference is exactly what I need.