#### Archived

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

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

This topic is 6251 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites

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 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

1. 1
2. 2
3. 3
Rutin
16
4. 4
JoeJ
13
5. 5

• 9
• 14
• 10
• 25
• 9
• ### Forum Statistics

• Total Topics
632645
• Total Posts
3007632
• ### Who's Online (See full list)

There are no registered users currently online

×