Skip to content

Commit 5d48c0d

Browse files
committed
jometree
1 parent 5059902 commit 5d48c0d

File tree

10 files changed

+206
-43
lines changed

10 files changed

+206
-43
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
reflector_angle = new DynamicValue(0, 0.1, 0, 360, DVLimitMode.WRAP);
2+
reflector = new LineSegment(0, 0, 0, 0);
3+
reflector.a.x = x + dcos(reflector_angle.v) * reflector_half_length;
4+
reflector.a.y = y - dsin(reflector_angle.v) * reflector_half_length;
5+
reflector.b.x = x + dcos(reflector_angle.v) * -reflector_half_length;
6+
reflector.b.y = y - dsin(reflector_angle.v) * -reflector_half_length;
7+
8+
can_reflect = false;
9+
laser = new LineSegment(room_width/2, 0, room_width/2, room_height);
10+
reflected_laser = new LineSegment(0, 0, 0, 0);
11+
laser_reflect_angle = 0;
12+
13+
room_bb = new Rectangle(0, 0, room_width, room_height);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
draw_set_colour(c_white);
2+
draw_set_alpha(1);
3+
draw_line(reflector.a.x, reflector.a.y, reflector.b.x, reflector.b.y);
4+
5+
draw_set_colour(c_lime);
6+
draw_circle(laser.a.x, laser.a.y, 8, false);
7+
8+
if (can_reflect) {
9+
draw_set_alpha(0.25);
10+
draw_line(laser.a.x, laser.a.y, laser.b.x, laser.b.y);
11+
draw_set_colour(c_white);
12+
draw_circle(laser.b.x, laser.b.y, 4, false);
13+
draw_set_colour(c_lime);
14+
draw_set_alpha(1);
15+
draw_circle(reflected_laser.a.x, reflected_laser.a.y, 4, false);
16+
draw_line(laser.a.x, laser.a.y, reflected_laser.a.x, reflected_laser.a.y);
17+
draw_line(reflected_laser.a.x, reflected_laser.a.y, reflected_laser.b.x, reflected_laser.b.y);
18+
} else {
19+
draw_set_alpha(1);
20+
draw_line(laser.a.x, laser.a.y, laser.b.x, laser.b.y);
21+
draw_set_colour(c_white);
22+
draw_circle(laser.b.x, laser.b.y, 4, false);
23+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
laser.a.x = mouse_x;
2+
laser.a.y = mouse_y;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
laser.b.x = mouse_x;
2+
laser.b.y = mouse_y;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
reflector_angle.update();
2+
3+
reflector.a.x = x + dcos(reflector_angle.v) * reflector_half_length;
4+
reflector.a.y = y - dsin(reflector_angle.v) * reflector_half_length;
5+
reflector.b.x = x + dcos(reflector_angle.v) * -reflector_half_length;
6+
reflector.b.y = y - dsin(reflector_angle.v) * -reflector_half_length;
7+
8+
var _laser_reflect_point = ray_line_intersect(laser, reflector);
9+
laser_reflect_angle = ray_reflect(
10+
point_direction(laser.a.x, laser.a.y, laser.b.x, laser.b.y),
11+
reflector_angle.v - 90
12+
);
13+
14+
if (_laser_reflect_point != -1) {
15+
can_reflect = true;
16+
reflected_laser.a.x = _laser_reflect_point.x;
17+
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);
20+
} else {
21+
can_reflect = false;
22+
}

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

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

current-scripts/Demos/useful-scripts/rooms/room_laser_demo/room_laser_demo.yy

Lines changed: 56 additions & 0 deletions
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: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ function Rectangle(_left, _top, _right, _bottom) constructor {
3030
}
3131

3232
function bottom_edge() {
33-
return new LineSegment(left, _bottom, right, bottom);
33+
return new LineSegment(left, bottom, right, bottom);
3434
}
3535
}

0 commit comments

Comments
 (0)