Skip to content

Commit 497b60a

Browse files
committed
mafs
1 parent 5d48c0d commit 497b60a

File tree

4 files changed

+52
-68
lines changed

4 files changed

+52
-68
lines changed

current-scripts/Demos/useful-scripts/objects/obj_laser_demo/Draw_0.gml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ if (can_reflect) {
1515
draw_circle(reflected_laser.a.x, reflected_laser.a.y, 4, false);
1616
draw_line(laser.a.x, laser.a.y, reflected_laser.a.x, reflected_laser.a.y);
1717
draw_line(reflected_laser.a.x, reflected_laser.a.y, reflected_laser.b.x, reflected_laser.b.y);
18+
draw_circle(reflected_laser.b.x, reflected_laser.b.y, 8, false);
1819
} else {
1920
draw_set_alpha(1);
2021
draw_line(laser.a.x, laser.a.y, laser.b.x, laser.b.y);

current-scripts/Demos/useful-scripts/objects/obj_laser_demo/Step_0.gml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ if (_laser_reflect_point != -1) {
1515
can_reflect = true;
1616
reflected_laser.a.x = _laser_reflect_point.x;
1717
reflected_laser.a.y = _laser_reflect_point.y;
18-
reflected_laser.b.x = _laser_reflect_point.x + lengthdir_x(reflect_laser_length, laser_reflect_angle);
19-
reflected_laser.b.y = _laser_reflect_point.y + lengthdir_y(reflect_laser_length, laser_reflect_angle);
18+
reflected_laser.b.x = _laser_reflect_point.x + lengthdir_x(1, laser_reflect_angle);
19+
reflected_laser.b.y = _laser_reflect_point.y + lengthdir_y(1, laser_reflect_angle);
20+
21+
var _edge_pos = get_edge_position(reflected_laser, room_bb);
22+
reflected_laser.b.x = _edge_pos.x;
23+
reflected_laser.b.y = _edge_pos.y;
2024
} else {
2125
can_reflect = false;
2226
}

current-scripts/Demos/useful-scripts/objects/obj_laser_demo/obj_laser_demo.yy

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

current-scripts/Demos/useful-scripts/scripts/geometry_functions/geometry_functions.gml

Lines changed: 45 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,66 @@
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;
137
var _cur_dist;
14-
var _edge = new Vector2(0, 0);
158
var _closest_dist = 1000000;
169
var _closest_pos = new Vector3(-1, -1, -1);
1710

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());
2413

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);
2916

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);
3622
}
23+
}
3724

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());
4027

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);
4530

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);
5236
}
37+
}
5338

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());
5641

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);
6144

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);
6850
}
51+
}
6952

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());
7255

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);
7758

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);
8464
}
8565
}
8666

0 commit comments

Comments
 (0)