Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Funcion CheckRectCollision();
- bool CheckRectCollision(s32 xa1, s32 ya1, s32 xb1, s32 yb1, s32 xa2, s32 ya2, s32 xb2, s32 yb2) {
- // Calcula anchos
- s32 width1 = (xa1 - xb1);
- if (width1 < 0) width1 = -width1;
- s32 width2 = (xa2 - xb2);
- if (width2 < 0) width2 = -width2;
- // Calcula alturas
- s32 height1 = (ya1 - yb1);
- if (height1 < 0) height1 = -height1;
- s32 height2 = (ya2 - yb2);
- if (height2 < 0) height2 = -height2;
- // Variables para el calculo
- s32 small_xa, small_xb, small_ya, small_yb;
- s32 big_xa, big_xb, big_ya, big_yb;
- // Asigna anchos
- if (width1 >= width2) {
- big_xa = xa1, big_xb = xb1;
- small_xa = xa2, small_xb = xb2;
- } else {
- big_xa = xa2, big_xb = xb2;
- small_xa = xa1, small_xb = xb1;
- }
- // Asigna altos
- if (height1 >= height2) {
- big_ya = ya1, big_yb = yb1;
- small_ya = ya2, small_yb = yb2;
- } else {
- big_ya = ya2, big_yb = yb2;
- small_ya = ya1, small_yb = yb1;
- }
- // Verifica si hay colision
- bool collision = false;
- if (
- (
- ((small_xa >= big_xa) && (small_xa <= big_xb)) // Rango horizontal
- ||
- ((small_xb >= big_xa) && (small_xb <= big_xb))
- )
- &&
- (
- ((small_ya >= big_ya) && (small_ya <= big_yb)) // Rango vertical
- ||
- ((small_yb >= big_ya) && (small_yb <= big_yb))
- )
- ) {
- // Si hay colision, devuelve verdadero
- collision = true;
- }
- // Revuelve la colision
- return collision;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement