Reputation: 11
I have a small sprite to represent a car, with six points surrounding it for spatial awareness. How would I track the points based on their color so I could test collision with a rectangle?
The left two points are pure magenta, the front two pure red, and the right two pure green * (255, 0, 255) ; (255, 0, 0) ; (0, 255, 0)
im = Image.open('CarSprite.png')
im = im.convert('RGBA')
data = np.array(im)
red, green, blue, alpha = data.T
black_areas = (red == 0) & (blue == 0) & (green == 0)
data[..., :-1][black_areas.T] = (random.randint(50,250), random.randint(50,250), random.randint(50,250)) # Converts black to random colors
im2 = Image.fromarray(data)
mode = im2.mode
size = im2.size
data = im2.tobytes()
Upvotes: 1
Views: 41
Reputation: 14906
I realise I am not answering your question directly...
The best way forward with this sort of collision is firstly check for a collision on the bounding rectangle of the sprite. This is an efficient check, because if the base-rectangle has not collided, there is no reason to check the six control points.
The Pygame rect class already supports this sort of detection with the colliderect()
member function.
Once the code determines that a base-level collision has occurred, then use the rect.collidepoint() function with each of your six control points (obviously accounting for the current position of your sprite).
You may also want to investigate using a collision bitmask for more accurate pixel-level collisions.
Upvotes: 3