|
1 | | -/// @func get_edge_position(instance, bounding_box) |
2 | | -/// @desc Get the closest position of ans instance to the edge of rectangle |
3 | | -/// @param {Vector2} position |
4 | | -/// @param {Rectangle} bounding_box |
5 | | -function get_edge_position(_position, _bounding_box) { |
6 | | - var _pos_to_bb_mid = new LineSegment(_position.x, |
7 | | - _position.y, |
8 | | - _bounding_box.left + _bounding_box.width()/2, |
9 | | - _bounding_box.top + _bounding_box.height()/2 |
10 | | - ); |
11 | | - |
12 | | - var _cur_t1 = -1; |
| 1 | +/// @func get_edge_position(line, bounding_box) |
| 2 | +/// @desc Get the closest intersection point between a line segment and the edge of a rectangle |
| 3 | +/// @param {LineSegment} line |
| 4 | +/// @param {Rectangle} bounding_box |
| 5 | +function get_edge_position(_line, _bounding_box) { |
| 6 | + var _cur_point = -1; |
13 | 7 | var _cur_dist; |
14 | | - var _edge = new Vector2(0, 0); |
15 | 8 | var _closest_dist = 1000000; |
16 | 9 | var _closest_pos = new Vector3(-1, -1, -1); |
17 | 10 |
|
18 | | - if (!point_in_rectangle(_position.x, _position.y, |
19 | | - _bounding_box.left, _bounding_box.top, |
20 | | - _bounding_box.right, _bounding_box.bottom)) { |
21 | | - |
22 | | - // left |
23 | | - _cur_t1 = ray_line_intersect(_pos_to_bb_mid, _bounding_box.left_edge()); |
| 11 | + // left |
| 12 | + _cur_point = ray_line_intersect(_line, _bounding_box.left_edge()); |
24 | 13 |
|
25 | | - if (_cur_t1 != -1) { |
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); |
| 14 | + if (_cur_point != -1) { |
| 15 | + _cur_dist = point_distance(_line.a.x, _line.a.y, _cur_point.x, _cur_point.y); |
29 | 16 |
|
30 | | - if (_cur_dist < _closest_dist) { |
31 | | - _closest_dist = _cur_dist; |
32 | | - _closest_pos.x = _edge.x; |
33 | | - _closest_pos.y = _edge.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); |
35 | | - } |
| 17 | + if (_cur_dist < _closest_dist) { |
| 18 | + _closest_dist = _cur_dist; |
| 19 | + _closest_pos.x = _cur_point.x; |
| 20 | + _closest_pos.y = _cur_point.y; |
| 21 | + _closest_pos.z = point_direction(_line.b.x, _line.b.y, _line.a.x, _line.a.y); |
36 | 22 | } |
| 23 | + } |
37 | 24 |
|
38 | | - // right |
39 | | - _cur_t1 = ray_line_intersect(_pos_to_bb_mid, _bounding_box.right_edge()); |
| 25 | + // right |
| 26 | + _cur_point = ray_line_intersect(_line, _bounding_box.right_edge()); |
40 | 27 |
|
41 | | - if (_cur_t1 != -1) { |
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); |
| 28 | + if (_cur_point != -1) { |
| 29 | + _cur_dist = point_distance(_line.a.x, _line.a.y, _cur_point.x, _cur_point.y); |
45 | 30 |
|
46 | | - if (_cur_dist < _closest_dist) { |
47 | | - _closest_dist = _cur_dist; |
48 | | - _closest_pos.x = _edge.x; |
49 | | - _closest_pos.y = _edge.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); |
51 | | - } |
| 31 | + if (_cur_dist < _closest_dist) { |
| 32 | + _closest_dist = _cur_dist; |
| 33 | + _closest_pos.x = _cur_point.x; |
| 34 | + _closest_pos.y = _cur_point.y; |
| 35 | + _closest_pos.z = point_direction(_line.b.x, _line.b.y, _line.a.x, _line.a.y); |
52 | 36 | } |
| 37 | + } |
53 | 38 |
|
54 | | - // top |
55 | | - _cur_t1 = ray_line_intersect(_pos_to_bb_mid, _bounding_box.top_edge()); |
| 39 | + // top |
| 40 | + _cur_point = ray_line_intersect(_line, _bounding_box.top_edge()); |
56 | 41 |
|
57 | | - if (_cur_t1 != -1) { |
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); |
| 42 | + if (_cur_point != -1) { |
| 43 | + _cur_dist = point_distance(_line.a.x, _line.a.y, _cur_point.x, _cur_point.y); |
61 | 44 |
|
62 | | - if (_cur_dist < _closest_dist) { |
63 | | - _closest_dist = _cur_dist; |
64 | | - _closest_pos.x = _edge.x; |
65 | | - _closest_pos.y = _edge.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); |
67 | | - } |
| 45 | + if (_cur_dist < _closest_dist) { |
| 46 | + _closest_dist = _cur_dist; |
| 47 | + _closest_pos.x = _cur_point.x; |
| 48 | + _closest_pos.y = _cur_point.y; |
| 49 | + _closest_pos.z = point_direction(_line.b.x, _line.b.y, _line.a.x, _line.a.y); |
68 | 50 | } |
| 51 | + } |
69 | 52 |
|
70 | | - // bottom |
71 | | - _cur_t1 = ray_line_intersect(_pos_to_bb_mid, _bounding_box.bottom_edge()); |
| 53 | + // bottom |
| 54 | + _cur_point = ray_line_intersect(_line, _bounding_box.bottom_edge()); |
72 | 55 |
|
73 | | - if (_cur_t1 != -1) { |
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); |
| 56 | + if (_cur_point != -1) { |
| 57 | + _cur_dist = point_distance(_line.a.x, _line.a.y, _cur_point.x, _cur_point.y); |
77 | 58 |
|
78 | | - if (_cur_dist < _closest_dist) { |
79 | | - _closest_dist = _cur_dist; |
80 | | - _closest_pos.x = _edge.x; |
81 | | - _closest_pos.y = _edge.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); |
83 | | - } |
| 59 | + if (_cur_dist < _closest_dist) { |
| 60 | + _closest_dist = _cur_dist; |
| 61 | + _closest_pos.x = _cur_point.x; |
| 62 | + _closest_pos.y = _cur_point.y; |
| 63 | + _closest_pos.z = point_direction(_line.b.x, _line.b.y, _line.a.x, _line.a.y); |
84 | 64 | } |
85 | 65 | } |
86 | 66 |
|
|
0 commit comments