Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vec2 Min(vec2 a, vec2 b) {
- vec2 r;
- r.x = Min(a.x, b.x);
- r.y = Min(a.y, b.y);
- return r;
- }
- vec2 Max(vec2 a, vec2 b) {
- vec2 r;
- r.x = Max(a.x, b.x);
- r.y = Max(a.y, b.y);
- return r;
- }
- int32 Sign(real32 a) {
- if (a < 0) { return -1; }
- else if (a > 0) { return 1; }
- else { return 0; }
- }
- // This function takes a pointer to a vec2. That means when we change the values of dir, we are
- // changing the values at the memory address we passed in!
- // We WANT the value of dir to change based on the computation of this function. IMPORTANT
- // This uses the SeparatingAxisTheorem
- // The dir value will tell us which direction to move to push rectangle A out of rectangle B
- bool RectTest(Rect a, Rect b, vec2 aPosition, vec2 bPosition, vec2 *dir) {
- Rect aGlobal;
- aGlobal.min = Add(a.min, aPosition);
- aGlobal.max = Add(a.max, aPosition);
- Rect bGlobal;
- bGlobal.min = Add(b.min, bPosition);
- bGlobal.max = Add(b.max, bPosition);
- r32 lengthX = Min(aGlobal.max.x, bGlobal.max.x) - Max(aGlobal.min.x, bGlobal.min.x);
- r32 lengthY = Min(aGlobal.max.y, bGlobal.max.y) - Max(aGlobal.min.y, bGlobal.min.y);
- // This tells us if there is separation on either axis
- if (lengthX < 0) { return false; }
- if (lengthY < 0) { return false; }
- // If we get here there is no separation, and we want to find the axis with the least length
- r32 posDiffX = bPosition.x - aPosition.x;
- r32 posDiffY = bPosition.y - aPosition.y;
- if (lengthX < lengthY) {
- dir->x = lengthX * -Sign(posDiffX);
- }
- else {
- dir->y = lengthY * -Sign(posDiffY);
- }
- return true;
- }
- / *
- USAGE CODE FOR RectTest()
- // @NOTE: this is the rectangle defined RELATIVE to the position of our guy
- // We can use these relative min/max to compute the global min and max points using them
- // and position
- vec2 dir;
- bool collides = RectTest(gameMem->guy1.rect, gameMem->guy2.rect,
- gameMem->guy1.pos, gameMem->guy2.pos, &dir);
- // @TODO: draw guy2 with a different color, if it's colldiing with guy1
- uint8 guy2Color = 128;
- // The dir variable tells us which direction to move to push the first rectangle out of the second.
- // so if we want to push guy2 away from guy1 we would subtract the direction, if we want to push guy1
- // away from guy2 we'd Add the direction to the position.
- if (collides) {
- //gameMem->guy1.pos = Add(gameMem->guy1.pos, dir);
- gameMem->guy2.pos = Sub(gameMem->guy2.pos, dir);
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement