Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ______________________ _______ ________ ___ ___
- * \__ ___/\_ _____/ \ \ / _____/| | | \
- * | | | __)_ / | \/ \ ___| | | |
- * | | | \/ | \ \_\ \ |_| |
- * |____| /_______ /\____|__ /\______ /________/
- * Game Engine. \/ \/ \/
- *
- * (c) 8BitPimp (2013)
- */
- #include "intersect.h"
- //
- // if [x1,y1] lies inside the box then this will fail
- //
- //
- bool intersect_line_box
- (
- sRayBox *i
- )
- {
- // find x1 x2 dimension style box
- float bx1 = i->bx - i->bw * 0.5f;
- float bx2 = i->bx + i->bw * 0.5f;
- float by1 = i->by - i->bh * 0.5f;
- float by2 = i->by + i->bh * 0.5f;
- //
- if ( i->x2 < i->x1 ) { swapf( &bx1, &bx2 ); }
- if ( i->y2 < i->y1 ) { swapf( &by1, &by2 ); }
- //
- float dx = (i->x2 - i->x1);
- float dy = (i->y2 - i->y1);
- //
- float t1x = (bx1 - i->x1) / dx;
- float t2x = (bx2 - i->x1) / dx;
- float t1y = (by1 - i->y1) / dy;
- float t2y = (by2 - i->y1) / dy;
- //
- if ( maxf( t1x, t1y ) < minf( t2x, t2y ) )
- {
- //
- float d = maxf( t1x, t1y );
- //
- if ( d > 1.0f ) return false;
- if ( d < 0.0f ) return false;
- //
- i->ix = i->x1 + d * (i->x2 - i->x1);
- i->iy = i->y1 + d * (i->y2 - i->y1);
- //
- return true;
- }
- //
- return false;
- }
- //
- bool intersect_ray_box
- (
- sRayBox *i
- )
- {
- // find x1 x2 dimension style box
- float bx1 = i->bx - i->bw * 0.5f;
- float bx2 = i->bx + i->bw * 0.5f;
- float by1 = i->by - i->bh * 0.5f;
- float by2 = i->by + i->bh * 0.5f;
- // swap to find the closest planes
- if ( i->x2 < i->x1 ) { swapf( &bx1, &bx2 ); }
- if ( i->y2 < i->y1 ) { swapf( &by1, &by2 ); }
- // differences
- float dx = (i->x2 - i->x1);
- float dy = (i->y2 - i->y1);
- //
- float t1x = (bx1 - i->x1) / dx;
- float t2x = (bx2 - i->x1) / dx;
- float t1y = (by1 - i->y1) / dy;
- float t2y = (by2 - i->y1) / dy;
- //
- if ( maxf( t1x, t1y ) < minf( t2x, t2y ) )
- {
- //
- float d = maxf( t1x, t1y );
- //
- i->ix = i->x1 + d * (i->x2 - i->x1);
- i->iy = i->y1 + d * (i->y2 - i->y1);
- //
- return true;
- }
- //
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement