1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#define TOTAL_OBJECTS 1000
#define TOTAL_OBSTACLES 1000
struct object{
float x;
float y;
};
object objects[1000];
struct obstacle{
float x;
float y;
};
obstacle obstacles[1000];
//RectA : Rectangle 1
//RectB: Rectangle 2
// Adaptation from https://stackoverflow.com/a/306332
int CollisionTest(int RectAX1, int RectAX2, int RectAY1, int RectAY2, int RectBX1, int RectBX2, int RectBY1, int RectBY2) {
return (RectAX1 < RectBX2 && RectAX2 > RectBX1 && RectAY1 > RectBY2 && RectAY2 < RectBY1);
}
// The code below is in a separate function called 30 times per second
for(int i = 0; i < TOTAL_OBJECTS; i++){
for(int j = 0; j < TOTAL_OBSTACLES; j++){
// 30x20 are both the obstacle and object dimensions
if(CollisionTest(objects[i].x, objects[i].x + 30, objects[i].y,
objects[i].y + 20, obstacles[j].x, obstacles[j].x + 30, obstacles[j].y,
obstacles[j].y + 20){
objects[i].y -= 1 // If this is reached, the object colliding with the obstacle should //move one unit down. However, this line of code is never executed.
}}}
| |