-
-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathdebug_shape_cast2d.rs
More file actions
195 lines (177 loc) · 5.16 KB
/
debug_shape_cast2d.rs
File metadata and controls
195 lines (177 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
mod common_macroquad2d;
use common_macroquad2d::draw_point;
use macroquad::prelude::*;
use nalgebra::{Isometry2, Point2};
use parry2d::math::{self, Isometry};
use parry2d::query::gjk::eps_tol;
use parry2d::query::{self, DefaultQueryDispatcher, Ray, ShapeCastOptions};
use parry2d::shape::{Ball, ConvexPolygon, Shape};
const RENDER_SCALE: f32 = 1.0;
const BALLCAST_WIDTH: f32 = 16.0;
#[macroquad::main("raycasts_animated")]
async fn main() {
for _i in 1.. {
clear_background(BLACK);
let screen_shift = Point2::new(screen_width() / 2.0, screen_height() / 2.0);
let to_cast_against = ConvexPolygon::from_convex_polyline(
[
[-24.0, 0.0].into(),
[0.0, 24.0].into(),
[24.0, 0.0].into(),
[0.0, -24.0].into(),
]
.into(),
)
.expect("Failed to create ConvexPolygon from polyline");
let to_cast_against_pose = Isometry2::rotation(0f32);
let mouse_pos = mouse_position();
let mouse_position_world =
(Point2::<f32>::new(mouse_pos.0, mouse_pos.1) - screen_shift.coords) / RENDER_SCALE;
let target_pos: Point2<f32> = [-312.0, 152.0].into();
// Those 2 fail with `min_bound >= _eps_tol`, fixed with a tolerance * 100
shape_cast_debug(
screen_shift,
[99.0, -33.0].into(),
target_pos,
to_cast_against.clone(),
);
shape_cast_debug(
screen_shift,
[98.0, -31.0].into(),
target_pos,
to_cast_against.clone(),
);
// This fails with `niter == 100` (and `niter == 100_000`), fixed with a tolerance * 10_000
shape_cast_debug(
screen_shift,
[47.0, -32.0].into(),
target_pos,
to_cast_against.clone(),
);
// For debug purposes, raycast to mouse position.
// Rendered last to be on top of the other raycasts
shape_cast_debug(
screen_shift,
target_pos,
mouse_position_world,
to_cast_against.clone(),
);
/*
*
* Render the cuboid.
*
*/
draw_polygon(
&to_cast_against.points(),
&to_cast_against_pose,
RENDER_SCALE,
screen_shift,
GREEN,
);
next_frame().await
}
}
fn shape_cast_debug(
screen_shift: Point2<f32>,
source_pos: Point2<f32>,
target_pos: Point2<f32>,
to_cast_against: ConvexPolygon,
) {
/*
*
* Prepare a Raycast and compute its result against the shape.
*
*/
let ray = Ray::new(source_pos, target_pos - source_pos);
let pos1: Point2<f32> = source_pos.into();
let vel1 = target_pos - source_pos;
let g1 = Ball::new(BALLCAST_WIDTH);
let pos2 = [0.0, 0.0];
let vel2 = [0.0, 0.0];
let g2 = to_cast_against.clone_dyn();
let toi = query::cast_shapes_with_dispatcher(
&pos1.into(),
&vel1,
&g1,
&pos2.into(),
&vel2.into(),
&*g2,
ShapeCastOptions::with_max_time_of_impact(1.0),
DefaultQueryDispatcher {
gjk_options: query::gjk::GjkOptions {
espilon_tolerance: math::DEFAULT_EPSILON * 1000f32,
nb_max_iterations: 100,
},
},
)
.unwrap();
/*
*
* Render the raycast's result.
*
*/
drawcircle_at(pos1, BALLCAST_WIDTH, RENDER_SCALE, screen_shift, ORANGE);
if let Some(toi) = toi {
if toi.time_of_impact == 0f32 {
draw_point(ray.origin, RENDER_SCALE, screen_shift, YELLOW);
} else {
drawline_from_to(
ray.origin,
(ray.point_at(toi.time_of_impact).coords).into(),
RENDER_SCALE,
screen_shift,
GREEN,
);
}
drawcircle_at(
(ray.point_at(toi.time_of_impact).coords).into(),
BALLCAST_WIDTH,
RENDER_SCALE,
screen_shift,
GREEN,
);
} else {
drawline_from_to(
ray.origin,
ray.origin + ray.dir,
RENDER_SCALE,
screen_shift,
RED,
);
}
}
fn draw_polygon(
polygon: &[Point2<f32>],
pose: &Isometry<f32>,
scale: f32,
shift: Point2<f32>,
color: Color,
) {
for i in 0..polygon.len() {
let a = pose * (scale * polygon[i]);
let b = pose * (scale * polygon[(i + 1) % polygon.len()]);
draw_line(
a.x + shift.x,
a.y + shift.y,
b.x + shift.x,
b.y + shift.y,
2.0,
color,
);
}
}
fn drawline_from_to(
from: Point2<f32>,
to: Point2<f32>,
scale: f32,
shift: Point2<f32>,
color: Color,
) {
let from = from * scale + shift.coords;
let to = to * scale + shift.coords;
draw_line(from.x, from.y, to.x, to.y, 2.0, color);
}
fn drawcircle_at(center: Point2<f32>, radius: f32, scale: f32, shift: Point2<f32>, color: Color) {
let center = center * scale + shift.coords;
draw_circle_lines(center.x, center.y, radius, 1f32, color);
}