11// / @func get_edge_position(instance, bounding_box)
22// / @desc Get the closest position of ans instance to the edge of rectangle
3- // / @param {Instance } instance
3+ // / @param {Vector2 } position
44// / @param {Rectangle} bounding_box
5- function get_edge_position (_instance , _bounding_box) {
6- var _pos = new Vector2 (_instance .x , _instance. y );
7- var _bb_mid = new Vector2 (
5+ function get_edge_position (_position , _bounding_box) {
6+ var _pos_to_bb_mid = new LineSegment (_position .x ,
7+ _position. y ,
88 _bounding_box.left + _bounding_box.width ()/2 ,
99 _bounding_box.top + _bounding_box.height ()/2
1010 );
@@ -15,71 +15,71 @@ function get_edge_position(_instance, _bounding_box) {
1515 var _closest_dist = 1000000 ;
1616 var _closest_pos = new Vector3 (-1 , -1 , -1 );
1717
18- if (!collision_rectangle (_bounding_box. left , _bounding_box. top ,
19- _bounding_box.right , _bounding_box.bottom ,
20- _instance, false , false )) {
18+ if (!point_in_rectangle (_position. x , _position. y ,
19+ _bounding_box.left , _bounding_box.top ,
20+ _bounding_box. right , _bounding_box. bottom )) {
2121
2222 // left
23- _cur_t1 = ray_line_intersect (_bb_mid, _pos , _bounding_box.left_edge ());
23+ _cur_t1 = ray_line_intersect (_pos_to_bb_mid , _bounding_box.left_edge ());
2424
2525 if (_cur_t1 != -1 ) {
26- _edge.x = _bb_mid. x + lengthdir_x (_cur_t1, point_direction (_bb_mid. x , _bb_mid. y , _pos. x , _pos .y ));
27- _edge.y = _bb_mid. y + lengthdir_y (_cur_t1, point_direction (_bb_mid. x , _bb_mid. y , _pos. x , _pos .y ));
28- _cur_dist = point_distance (_pos. x , _pos .y , _edge.x , _edge.y );
26+ _edge.x = _pos_to_bb_mid. b . x + lengthdir_x (_cur_t1, point_direction (_pos_to_bb_mid. b . x , _pos_to_bb_mid. b . y , _pos_to_bb_mid. a . x , _pos_to_bb_mid. a .y ));
27+ _edge.y = _pos_to_bb_mid. b . y + lengthdir_y (_cur_t1, point_direction (_pos_to_bb_mid. b . x , _pos_to_bb_mid. b . y , _pos_to_bb_mid. a . x , _pos_to_bb_mid. a .y ));
28+ _cur_dist = point_distance (_pos_to_bb_mid. a . x , _pos_to_bb_mid. a .y , _edge.x , _edge.y );
2929
3030 if (_cur_dist < _closest_dist) {
3131 _closest_dist = _cur_dist;
3232 _closest_pos.x = _edge.x ;
3333 _closest_pos.y = _edge.y ;
34- _closest_pos.z = point_direction (_bb_mid. x , _bb_mid. y , _pos. x , _pos .y );
34+ _closest_pos.z = point_direction (_pos_to_bb_mid. b . x , _pos_to_bb_mid. b . y , _pos_to_bb_mid. a . x , _pos_to_bb_mid. a .y );
3535 }
3636 }
3737
3838 // right
39- _cur_t1 = ray_line_intersect (_bb_mid, _pos , _bounding_box.right_edge ());
39+ _cur_t1 = ray_line_intersect (_pos_to_bb_mid , _bounding_box.right_edge ());
4040
4141 if (_cur_t1 != -1 ) {
42- _edge.x = _bb_mid. x + lengthdir_x (_cur_t1,point_direction (_bb_mid. x , _bb_mid. y , _pos. x , _pos .y ));
43- _edge.y = _bb_mid. y + lengthdir_y (_cur_t1,point_direction (_bb_mid. x , _bb_mid. y , _pos. x , _pos .y ));
44- _cur_dist = point_distance (_pos. x , _pos .y , _edge.x , _edge.y );
42+ _edge.x = _pos_to_bb_mid. b . x + lengthdir_x (_cur_t1,point_direction (_pos_to_bb_mid. b . x , _pos_to_bb_mid. b . y , _pos_to_bb_mid. a . x , _pos_to_bb_mid. a .y ));
43+ _edge.y = _pos_to_bb_mid. b . y + lengthdir_y (_cur_t1,point_direction (_pos_to_bb_mid. b . x , _pos_to_bb_mid. b . y , _pos_to_bb_mid. a . x , _pos_to_bb_mid. a .y ));
44+ _cur_dist = point_distance (_pos_to_bb_mid. a . x , _pos_to_bb_mid. a .y , _edge.x , _edge.y );
4545
4646 if (_cur_dist < _closest_dist) {
4747 _closest_dist = _cur_dist;
4848 _closest_pos.x = _edge.x ;
4949 _closest_pos.y = _edge.y ;
50- _closest_pos.z = point_direction (_bb_mid. x ,_bb_mid. y ,_pos. x , _pos .y );
50+ _closest_pos.z = point_direction (_pos_to_bb_mid. b . x ,_pos_to_bb_mid. b . y ,_pos_to_bb_mid. a . x , _pos_to_bb_mid. a .y );
5151 }
5252 }
5353
5454 // top
55- _cur_t1 = ray_line_intersect (_bb_mid, _pos , _bounding_box.top_edge ());
55+ _cur_t1 = ray_line_intersect (_pos_to_bb_mid , _bounding_box.top_edge ());
5656
5757 if (_cur_t1 != -1 ) {
58- _edge.x = _bb_mid. x + lengthdir_x (_cur_t1,point_direction (_bb_mid. x , _bb_mid. y , _pos. x , _pos .y ));
59- _edge.y = _bb_mid. y + lengthdir_y (_cur_t1,point_direction (_bb_mid. x , _bb_mid. y , _pos. x , _pos .y ));
60- _cur_dist = point_distance (_pos. x , _pos .y , _edge.x , _edge.y );
58+ _edge.x = _pos_to_bb_mid. b . x + lengthdir_x (_cur_t1,point_direction (_pos_to_bb_mid. b . x , _pos_to_bb_mid. b . y , _pos_to_bb_mid. a . x , _pos_to_bb_mid. a .y ));
59+ _edge.y = _pos_to_bb_mid. b . y + lengthdir_y (_cur_t1,point_direction (_pos_to_bb_mid. b . x , _pos_to_bb_mid. b . y , _pos_to_bb_mid. a . x , _pos_to_bb_mid. a .y ));
60+ _cur_dist = point_distance (_pos_to_bb_mid. a . x , _pos_to_bb_mid. a .y , _edge.x , _edge.y );
6161
6262 if (_cur_dist < _closest_dist) {
6363 _closest_dist = _cur_dist;
6464 _closest_pos.x = _edge.x ;
6565 _closest_pos.y = _edge.y ;
66- _closest_pos.z = point_direction (_bb_mid. x ,_bb_mid. y ,_pos. x , _pos .y );
66+ _closest_pos.z = point_direction (_pos_to_bb_mid. b . x ,_pos_to_bb_mid. b . y ,_pos_to_bb_mid. a . x , _pos_to_bb_mid. a .y );
6767 }
6868 }
6969
7070 // bottom
71- _cur_t1 = ray_line_intersect (_bb_mid, _pos , _bounding_box.bottom_edge ());
71+ _cur_t1 = ray_line_intersect (_pos_to_bb_mid , _bounding_box.bottom_edge ());
7272
7373 if (_cur_t1 != -1 ) {
74- _edge.x = _bb_mid. x + lengthdir_x (_cur_t1,point_direction (_bb_mid. x , _bb_mid. y , _pos. x , _pos .y ));
75- _edge.y = _bb_mid. y + lengthdir_y (_cur_t1,point_direction (_bb_mid. x , _bb_mid. y , _pos. x , _pos .y ));
76- _cur_dist = point_distance (_pos. x , _pos .gravity_direction , _edge.x , _edge.y );
74+ _edge.x = _pos_to_bb_mid. b . x + lengthdir_x (_cur_t1,point_direction (_pos_to_bb_mid. b . x , _pos_to_bb_mid. b . y , _pos_to_bb_mid. a . x , _pos_to_bb_mid. a .y ));
75+ _edge.y = _pos_to_bb_mid. b . y + lengthdir_y (_cur_t1,point_direction (_pos_to_bb_mid. b . x , _pos_to_bb_mid. b . y , _pos_to_bb_mid. a . x , _pos_to_bb_mid. a .y ));
76+ _cur_dist = point_distance (_pos_to_bb_mid. a . x , _pos_to_bb_mid. a .gravity_direction , _edge.x , _edge.y );
7777
7878 if (_cur_dist < _closest_dist) {
7979 _closest_dist = _cur_dist;
8080 _closest_pos.x = _edge.x ;
8181 _closest_pos.y = _edge.y ;
82- _closest_pos.z = point_direction (_bb_mid. x ,_bb_mid. y ,_pos. x , _pos .y );
82+ _closest_pos.z = point_direction (_pos_to_bb_mid. b . x ,_pos_to_bb_mid. b . y ,_pos_to_bb_mid. a . x , _pos_to_bb_mid. a .y );
8383 }
8484 }
8585 }
@@ -128,16 +128,15 @@ function prune_path(_path, _obstacle) {
128128}
129129
130130
131- // / @func ray_line_intersect(origin, target, a, b)
132- // / @desc Finds the intersection point between a ray and and a line segment
133- // / @param {Vector2} origin
134- // / @param {Vector2} target
135- // / @param {LineSegement} line
136- function ray_line_intersect (_origin, _target, _line) {
137- var _ray_dir = point_direction (_origin.x , _origin.y , _target.x , _target.y );
131+ // / @func line_line_intersect(line1, line2)
132+ // / @desc Finds the intersection point between two LineSegments
133+ // / @param {LineSegement} line1
134+ // / @param {LineSegement} line2
135+ function ray_line_intersect (_line1, _line2) {
136+ var _ray_dir = point_direction (_line1.a .x , _line1.a .y , _line1.b .x , _line1.b .y );
138137
139- var _v1 = new Vector2 (_origin. x - _line .a .x , _origin. y - _line .a .y );
140- var _v2 = new Vector2 (_line .b .x - _line .a .x , _line .b .y - _line .a .y );
138+ var _v1 = new Vector2 (_line1. a . x - _line2 .a .x , _line1. a . y - _line2 .a .y );
139+ var _v2 = new Vector2 (_line2 .b .x - _line2 .a .x , _line2 .b .y - _line2 .a .y );
141140 var _v3 = new Vector2 (-lengthdir_y (1 , _ray_dir), lengthdir_x (1 , _ray_dir));
142141
143142 var _dot = dot_product (_v2.x , _v2.y , _v3.x , _v3.y );
@@ -148,8 +147,10 @@ function ray_line_intersect(_origin, _target, _line) {
148147 var _t1 = cross_product_2d (_v2, _v1) / _dot;
149148 var _t2 = dot_product (_v1.x , _v1.y , _v3.x , _v3.y ) / _dot;
150149
151- if (_t1 >= 0 && (_t2 >= 0 && _t2 <= 1 ))
152- return _t1;
150+ if (_t1 >= 0 && (_t2 >= 0 && _t2 <= 1 )) {
151+ var _unit_ray = new Vector2 (lengthdir_x (1 , _ray_dir), lengthdir_y (1 , _ray_dir));
152+ return new Vector2 (_line1.a .x + _unit_ray.x * _t1, _line1.a .y + _unit_ray.y * _t1);
153+ }
153154
154155 return -1 ;
155156}
0 commit comments