Archived

This topic is now archived and is closed to further replies.

Codejoy

Another collision question...(i know i know)

Recommended Posts

Well I can do a simple collision based upon bounding rects of my sprties to decide if two bounding rects collide. What I really need though is an efficient way of testing if my one sprite collides with a specific side of my other multiple sprites (a ball sprite with a brick sprite). I had thought I just test one side of the box but apparently for me this doesn''t seem to be working. Basically, If the ball hits the top of the brick I want it to do something else than if it hits the bottom of the brick.... using a stuct like: typdef struct { int x; int y; int vx; int vy; int height; int width; } SPRITE SPRITE ball; SPRITE bricks[50]; Thanks

Share this post


Link to post
Share on other sites
I didn''t see your earlier posts, so I''ll help you...

You had this struct:

typedef struct {
int x, y;
int vx, vy;
int width;
itn height;
} SPRITE;

SPRITE ball;
SPRITE bricks[50];

My assumptions are
1) that x and y are in pixels
2) that if the ball touches ANY brick, ONLY the collision is flagged for.
3) that your code may look something like this:

// Return: 0 -> no-, 1 -> top-, 2 -> bottom-collision
char testCollision() {
int i;
char xtest;

for (i = 0; i < 50; i++) {
xtest = 0;
if (ball.x + ball.width > bricks.x) xtest = 1;
if (ball.x < bricks[i].x + bricks[i].width) xtest = 1;
if (xtest) {
if (ball.y + ball.height > bricks[i].y) return 1
if (ball.y < bricks[i].y + bricks[i].height) return 2;
}
}
return 0;
}

Didn''t test this code, hope it''s working... Don''t know if it''s optimized, wrote it from the top of my head right here!

Share this post


Link to post
Share on other sites
Ahhh I see, that is for returning if the bottom or top of the sprite was hit, if the left or the right then I can use the same code just reverse what i test in what order (y''s triggering a ytest var and then testing the x''s from there returning the proper result). Hey thanks a lot, this was the little bit of help i needed to get me thinking and focused again.

Regards,
Shane

Share this post


Link to post
Share on other sites