diff options
Diffstat (limited to 'core/math/math_2d.h')
-rw-r--r-- | core/math/math_2d.h | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/core/math/math_2d.h b/core/math/math_2d.h index 5bc1b5f0be..5e6cefd114 100644 --- a/core/math/math_2d.h +++ b/core/math/math_2d.h @@ -5,7 +5,7 @@ /* GODOT ENGINE */ /* http://www.godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -214,18 +214,41 @@ struct Rect2 { float get_area() const { return size.width*size.height; } inline bool intersects(const Rect2& p_rect) const { - if ( pos.x > (p_rect.pos.x + p_rect.size.width) ) + if ( pos.x >= (p_rect.pos.x + p_rect.size.width) ) return false; - if ( (pos.x+size.width) < p_rect.pos.x ) + if ( (pos.x+size.width) <= p_rect.pos.x ) return false; - if ( pos.y > (p_rect.pos.y + p_rect.size.height) ) + if ( pos.y >= (p_rect.pos.y + p_rect.size.height) ) return false; - if ( (pos.y+size.height) < p_rect.pos.y ) + if ( (pos.y+size.height) <= p_rect.pos.y ) return false; return true; } + inline float distance_to(const Vector2& p_point) const { + + float dist = 1e20; + + if (p_point.x < pos.x) { + dist=MIN(dist,pos.x-p_point.x); + } + if (p_point.y < pos.y) { + dist=MIN(dist,pos.y-p_point.y); + } + if (p_point.x >= (pos.x+size.x) ) { + dist=MIN(p_point.x-(pos.x+size.x),dist); + } + if (p_point.y >= (pos.y+size.y) ) { + dist=MIN(p_point.y-(pos.y+size.y),dist); + } + + if (dist==1e20) + return 0; + else + return dist; + } + _FORCE_INLINE_ bool intersects_transformed(const Matrix32& p_xform, const Rect2& p_rect) const; bool intersects_segment(const Point2& p_from, const Point2& p_to, Point2* r_pos=NULL, Point2* r_normal=NULL) const; |