SHOW:
|
|
- or go back to the newest paste.
1 | ///***** Cloned from badwrong: https://pastebin.com/xnbJQwKgf ****** | |
2 | /* | |
3 | A small library for collision lines with "thickness". Static sensor objects | |
4 | are used and stretched to then call built-in collision functions. | |
5 | ||
6 | The object __collision_line_thick must be created and have a sprite with the following settings: | |
7 | width = 1 | |
8 | height = 2 | |
9 | origin = middle left | |
10 | collision mask = full image, rectangle with rotation | |
11 | ||
12 | The object should also have persistent set to TRUE so that room changes will not cause the | |
13 | instance to be destroyed. | |
14 | */ | |
15 | ||
16 | /// @function collision_line_thick(_x1, _y1, _x2, _y2, _thickness, _object) | |
17 | /// @param {real} _x1 The x coordinate of the start of the line | |
18 | /// @param {real} _y1 The y coordinate of the start of the line | |
19 | /// @param {real} _x2 The x coordinate of the end of the line | |
20 | /// @param {real} _y2 The y coordinate of the end of the line | |
21 | /// @param {real} _thickness The thickness of the line to check for collisions | |
22 | /// @param {index} _object The object to check for instance collisions | |
23 | /// @description A thick collision line that returns the the first instance collided with | |
24 | ||
25 | function collision_line_thick(_x1, _y1, _x2, _y2, _thickness, _object) | |
26 | { | |
27 | static _sensor = instance_create_depth(0, 0, -16000, __collision_line_thick); | |
28 | ||
29 | with (_sensor) | |
30 | { | |
31 | x = _x1; | |
32 | y = _y1; | |
33 | image_xscale = point_distance(_x1, _y1, _x2, _y2); | |
34 | image_yscale = _thickness * 0.5; | |
35 | image_angle = point_direction(_x1, _y1, _x2, _y2); | |
36 | ||
37 | return instance_place(x, y, _object); | |
38 | } | |
39 | } | |
40 | ||
41 | ||
42 | /// @function collision_line_thick_list(_x1, _y1, _x2, _y2, _thickness, _object, _list, _ordered) | |
43 | /// @param {real} _x1 The x coordinate of the start of the line | |
44 | /// @param {real} _y1 The y coordinate of the start of the line | |
45 | /// @param {real} _x2 The x coordinate of the end of the line | |
46 | /// @param {real} _y2 The y coordinate of the end of the line | |
47 | /// @param {real} _thickness The thickness of the line to check for collisions | |
48 | /// @param {index} _object The object to check for instance collisions | |
49 | /// @param {index} _list The DS list to use to store the IDs of the colliding instances | |
50 | /// @param {boolean} _ordered Whether the list should be ordered by distance (true) or not (false) | |
51 | /// @description A thick collision line that returns the number of instances collided with | |
52 | ||
53 | function collision_line_thick_list(_x1, _y1, _x2, _y2, _thickness, _object, _list, _ordered) | |
54 | { | |
55 | static _sensor = instance_create_depth(0, 0, -16000, __collision_line_thick); | |
56 | ||
57 | with (_sensor) | |
58 | { | |
59 | x = _x1; | |
60 | y = _y1; | |
61 | image_xscale = point_distance(_x1, _y1, _x2, _y2); | |
62 | image_yscale = _thickness * 0.5; | |
63 | image_angle = point_direction(_x1, _y1, _x2, _y2); | |
64 | ||
65 | return instance_place_list(x, y, _object, _list, _ordered); | |
66 | } | |
67 | } | |
68 | ||
69 | ||
70 | /// @function collision_line_thick_impact(_x1, _y1, _x2, _y2, _thickness, _object) | |
71 | /// @param {real} _x1 The x coordinate of the start of the line | |
72 | /// @param {real} _y1 The y coordinate of the start of the line | |
73 | /// @param {real} _x2 The x coordinate of the end of the line | |
74 | /// @param {real} _y2 The y coordinate of the end of the line | |
75 | /// @param {real} _thickness The thickness of the line to check for collisions | |
76 | /// @param {index} _object The object to check for instance collisions | |
77 | /// @description A thick collision line that returns a struct with instance ID and impact x/y position | |
78 | ||
79 | function collision_line_thick_impact(_x1, _y1, _x2, _y2, _thickness, _object) | |
80 | { | |
81 | static _sensor = instance_create_depth(0, 0, -16000, __collision_line_thick); | |
82 | ||
83 | with (_sensor) | |
84 | { | |
85 | x = _x1; | |
86 | y = _y1; | |
87 | image_xscale = point_distance(_x1, _y1, _x2, _y2); | |
88 | image_yscale = _thickness * 0.5; | |
89 | image_angle = point_direction(_x1, _y1, _x2, _y2); | |
90 | ||
91 | var _inst = instance_place(x, y, _object), | |
92 | _rx = _x1, | |
93 | _ry = _y1; | |
94 | ||
95 | if (_inst != noone) { | |
96 | ||
97 | var _count = ceil(log2(image_xscale)) + 1, | |
98 | _p0 = 0, | |
99 | _p1 = 1; | |
100 | ||
101 | _x2 -= _x1; | |
102 | _y2 -= _y1; | |
103 | ||
104 | repeat (_count) { | |
105 | ||
106 | var _np = _p0 + (_p1 - _p0) * 0.5, | |
107 | _nx = _x1 + _x2 * _np, | |
108 | _ny = _y1 + _y2 * _np; | |
109 | ||
110 | image_xscale = point_distance(x, y, _nx, _ny); | |
111 | var _inst2 = instance_place(x, y, _object); | |
112 | ||
113 | if (_inst2 != noone) { | |
114 | _inst = _inst2; | |
115 | _rx = _nx; | |
116 | _ry = _ny; | |
117 | _p1 = _np; | |
118 | } else _p0 = _np; | |
119 | } | |
120 | } | |
121 | ||
122 | return { id : _inst, x : _rx, y : _ry }; | |
123 | } | |
124 | } |