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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
extern crate rg3d_core;
#[macro_use]
extern crate bitflags;

use rg3d_core::{
    math::{
        vec3::Vec3,
        ray::Ray,
    },
    pool::{
        Pool,
        Handle,
    },
    visitor::{
        Visit,
        VisitResult,
        Visitor,
    },
};
use std::{
    cmp::Ordering,
    cell::RefCell
};
use crate::{
    rigid_body::RigidBody,
    static_geometry::StaticGeometry,
    convex_shape::{
        ConvexShape,
        CircumRadius
    }
};
use rg3d_core::pool::Ticket;

pub mod gjk_epa;
pub mod convex_shape;
pub mod rigid_body;
pub mod contact;
pub mod static_geometry;

pub enum HitKind {
    Body(Handle<RigidBody>),
    StaticTriangle {
        static_geometry: Handle<StaticGeometry>,
        triangle_index: usize,
    },
}

pub struct RayCastOptions {
    pub ignore_bodies: bool,
    pub ignore_static_geometries: bool,
    pub sort_results: bool,
}

impl Default for RayCastOptions {
    fn default() -> Self {
        Self {
            ignore_bodies: false,
            ignore_static_geometries: false,
            sort_results: true,
        }
    }
}

pub struct RayCastResult {
    pub kind: HitKind,
    pub position: Vec3,
    pub normal: Vec3,
    pub sqr_distance: f32,
}

#[derive(Debug)]
pub struct Physics {
    bodies: Pool<RigidBody>,
    static_geoms: Pool<StaticGeometry>,
    query_buffer: RefCell<Vec<u32>>,
    enabled: bool
}

impl Visit for Physics {
    fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
        visitor.enter_region(name)?;

        self.bodies.visit("Bodies", visitor)?;
        self.static_geoms.visit("StaticGeoms", visitor)?;
        let _ = self.enabled.visit("Enabled", visitor); // let _ for backward compatibility.


        visitor.leave_region()
    }
}

impl Default for Physics {
    fn default() -> Self {
        Self::new()
    }
}

impl Clone for Physics {
    fn clone(&self) -> Self {
        Self {
            bodies: self.bodies.clone(),
            static_geoms: self.static_geoms.clone(),
            query_buffer: Default::default(),
            enabled: self.enabled
        }
    }
}

impl Physics {
    pub fn new() -> Self {
        Self {
            bodies: Pool::new(),
            static_geoms: Pool::new(),
            query_buffer: Default::default(),
            enabled: true
        }
    }

    pub fn add_body(&mut self, body: RigidBody) -> Handle<RigidBody> {
        self.bodies.spawn(body)
    }

    pub fn remove_body(&mut self, body_handle: Handle<RigidBody>) {
        self.bodies.free(body_handle);
    }

    pub fn add_static_geometry(&mut self, static_geom: StaticGeometry) -> Handle<StaticGeometry> {
        self.static_geoms.spawn(static_geom)
    }

    pub fn borrow_static_geometry(&self, static_geom: Handle<StaticGeometry>) -> &StaticGeometry {
        &self.static_geoms[static_geom]
    }

    pub fn borrow_static_geometry_mut(&mut self, static_geom: Handle<StaticGeometry>) -> &mut StaticGeometry {
        &mut self.static_geoms[static_geom]
    }

    pub fn is_static_geometry_handle_valid(&self, static_geom: Handle<StaticGeometry>) -> bool {
        self.static_geoms.is_valid_handle(static_geom)
    }

    pub fn remove_static_geometry(&mut self, static_geom: Handle<StaticGeometry>) {
        self.static_geoms.free(static_geom);
    }

    pub fn borrow_body(&self, handle: Handle<RigidBody>) -> &RigidBody {
        self.bodies.borrow(handle)
    }

    pub fn borrow_body_mut(&mut self, handle: Handle<RigidBody>) -> &mut RigidBody {
        self.bodies.borrow_mut(handle)
    }

    pub fn is_valid_body_handle(&self, handle: Handle<RigidBody>) -> bool {
        self.bodies.is_valid_handle(handle)
    }

    pub fn take_reserve_body(&mut self, handle: Handle<RigidBody>) -> (Ticket<RigidBody>, RigidBody) {
        self.bodies.take_reserve(handle)
    }

    pub fn put_body_back(&mut self, ticket: Ticket<RigidBody>, body: RigidBody) -> Handle<RigidBody> {
        self.bodies.put_back(ticket, body)
    }

    pub fn forget_body_ticket(&mut self, ticket: Ticket<RigidBody>) {
        self.bodies.forget_ticket(ticket)
    }

    pub fn take_reserve_static_geometry(&mut self, handle: Handle<StaticGeometry>) -> (Ticket<StaticGeometry>, StaticGeometry) {
        self.static_geoms.take_reserve(handle)
    }

    pub fn put_static_geometry_back(&mut self, ticket: Ticket<StaticGeometry>, geom: StaticGeometry) -> Handle<StaticGeometry> {
        self.static_geoms.put_back(ticket, geom)
    }

    pub fn forget_static_geometry_ticket(&mut self, ticket: Ticket<StaticGeometry>) {
        self.static_geoms.forget_ticket(ticket)
    }

    pub fn set_enabled(&mut self, enabled: bool) {
        self.enabled = enabled;
    }

    pub fn enabled(&self) -> bool {
        self.enabled
    }

    pub fn step(&mut self, delta_time: f32) {
        if !self.enabled {
            return;
        }

        let dt2 = delta_time * delta_time;
        let air_friction = 0.003;

        // Take second mutable reference to bodies, this is safe because:

        // 1. We won't modify collection while iterating over it.

        // 2. Simultaneous access to a body won't happen because of

        //    pointer equality check down below.

        let other_bodies = unsafe { &mut *(&mut self.bodies as *mut Pool<RigidBody>) };

        for (body_handle, body) in self.bodies.pair_iter_mut() {
            if let Some(ref mut lifetime) = body.lifetime {
                *lifetime -= delta_time;
            }

            body.acceleration += body.gravity;
            body.verlet(dt2, air_friction);

            body.contacts.clear();

            for (other_body_handle, other_body) in other_bodies.pair_iter_mut() {
                // Enforce borrowing rules at runtime.

                if !std::ptr::eq(body, other_body) &&
                    ((other_body.collision_group & body.collision_mask) != 0) &&
                    ((body.collision_group & other_body.collision_mask) != 0) {
                    body.solve_rigid_body_collision(body_handle,other_body, other_body_handle);
                }
            }

            for (handle, static_geometry) in self.static_geoms.pair_iter() {
                let mut query_buffer = self.query_buffer.borrow_mut();
                static_geometry.octree.sphere_query(body.position, body.shape.circumradius(), &mut query_buffer);

                for n in query_buffer.iter().map(|i| *i as usize) {
                    let triangle = static_geometry.triangles.get(n).unwrap();
                    body.solve_triangle_collision(&triangle, n, handle);
                }
            }
        }

        self.bodies.retain(|body| body.lifetime.is_none() || body.lifetime.unwrap() > 0.0);
    }

    pub fn ray_cast(&self, ray: &Ray, options: RayCastOptions, result: &mut Vec<RayCastResult>) -> bool {
        result.clear();

        // Check bodies

        if !options.ignore_bodies {
            for body_index in 0..self.bodies.get_capacity() {
                let body = if let Some(body) = self.bodies.at(body_index) {
                    body
                } else {
                    continue;
                };

                let body_handle = self.bodies.handle_from_index(body_index);

                match &body.shape {
                    ConvexShape::Dummy => {}
                    ConvexShape::Box(box_shape) => {
                        if let Some(points) = ray.box_intersection_points(&box_shape.get_min(), &box_shape.get_max()) {
                            for point in points.iter() {
                                result.push(RayCastResult {
                                    kind: HitKind::Body(body_handle),
                                    position: *point,
                                    normal: *point - body.position, // TODO: Fix normal

                                    sqr_distance: point.sqr_distance(&ray.origin),
                                })
                            }
                        }
                    }
                    ConvexShape::Sphere(sphere_shape) => {
                        if let Some(points) = ray.sphere_intersection_points(&body.position, sphere_shape.radius) {
                            for point in points.iter() {
                                result.push(RayCastResult {
                                    kind: HitKind::Body(body_handle),
                                    position: *point,
                                    normal: *point - body.position,
                                    sqr_distance: point.sqr_distance(&ray.origin),
                                })
                            }
                        }
                    }
                    ConvexShape::Capsule(capsule_shape) => {
                        let (pa, pb) = capsule_shape.get_cap_centers();
                        let pa = pa + body.position;
                        let pb = pb + body.position;

                        if let Some(points) = ray.capsule_intersection(&pa, &pb, capsule_shape.get_radius()) {
                            for point in points.iter() {
                                result.push(RayCastResult {
                                    kind: HitKind::Body(body_handle),
                                    position: *point,
                                    normal: *point - body.position,
                                    sqr_distance: point.sqr_distance(&ray.origin),
                                })
                            }
                        }
                    }
                    ConvexShape::Triangle(triangle_shape) => {
                        if let Some(point) = ray.triangle_intersection(&triangle_shape.vertices) {
                            result.push(RayCastResult {
                                kind: HitKind::Body(body_handle),
                                position: point,
                                normal: triangle_shape.get_normal().unwrap(),
                                sqr_distance: point.sqr_distance(&ray.origin),
                            })
                        }
                    }
                    ConvexShape::PointCloud(_point_cloud) => {
                        // TODO: Implement this. This requires to build convex hull from point cloud first

                        // i.e. by gift wrapping algorithm or some other more efficient algorithms -

                        // https://dccg.upc.edu/people/vera/wp-content/uploads/2014/11/GA2014-ConvexHulls3D-Roger-Hernando.pdf

                    }
                }
            }
        }

        // Check static geometries

        if !options.ignore_static_geometries {
            for (handle, geom) in self.static_geoms.pair_iter() {
                let mut query_buffer = self.query_buffer.borrow_mut();
                geom.octree.ray_query(ray, &mut query_buffer);

                for triangle_index in query_buffer.iter().map(|i| *i as usize) {
                    let triangle = geom.triangles.get(triangle_index).unwrap();
                    if let Some(point) = ray.triangle_intersection(&triangle.points) {
                        result.push(RayCastResult {
                            kind: HitKind::StaticTriangle {
                                static_geometry: handle,
                                triangle_index,
                            },
                            position: point,
                            normal: triangle.plane.normal,
                            sqr_distance: point.sqr_distance(&ray.origin),
                        })
                    }
                }
            }
        }

        if options.sort_results {
            result.sort_by(|a, b| {
                if a.sqr_distance > b.sqr_distance {
                    Ordering::Greater
                } else if a.sqr_distance < b.sqr_distance {
                    Ordering::Less
                } else {
                    Ordering::Equal
                }
            })
        }

        !result.is_empty()
    }
}