From d6c58243b7b9d90e5e23af2f3c866ddf316cb0fa Mon Sep 17 00:00:00 2001 From: nathansmith117 Date: Thu, 22 Feb 2024 12:19:43 -0700 Subject: Can now use pixel data for collision --- src/util.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'src/util.c') diff --git a/src/util.c b/src/util.c index 864fab6..819a956 100644 --- a/src/util.c +++ b/src/util.c @@ -10,24 +10,29 @@ Vector2 getScaledMousePosition() return mousePosition; } -bool doesCollideWithAnimation(Rectangle rect, Animation* animation, Vector2 point) +bool doesCollideWithAnimationData(Rectangle rect, void* data, int width, int height, int frame, Vector2 point) { - float xScale = (float)rect.width / animation->width; - float yScale = (float)rect.height / animation->height; + // Doesn't collide with rect. + if (!CheckCollisionPointRec(point, rect)) + { + return false; + } - unsigned int frameOffset = animation->width * animation->height * 4 * animation->currentFrame; + float xScale = (float)width / rect.width; + float yScale = (float)height / rect.height; - // Check each pixal. - for (int row = 0; row < rect.height; ++row) - { - for (int col = 0; col < rect.width; ++col) - { - int scaledRow = row * yScale; - int scaledCol = col * xScale; + int row = (point.y - rect.y) * yScale; + int col = (point.x - rect.x) * xScale; - int pixalPosition = frameOffset + (animation->width * scaledRow + scaledCol); + unsigned int frameOffset = width * height * frame; - } + // Position of the apha byte in the color. + unsigned int position = (frameOffset + (width * row + col)) * 4 + 3; + + // Check apha at position. + if (*(((unsigned char*)data) + position) != 0x00) + { + return true; } return false; -- cgit v1.2.3