Skip to main content

linear_sim/
event.rs

1//! Simulation events
2
3use enumflags2;
4use derive_more::From;
5#[cfg(feature = "derive_serdes")]
6use serde::{Deserialize, Serialize};
7use strum::Display;
8
9use crate::{collision, component, force, math, object};
10
11/// An input event
12#[derive(Debug, PartialEq, Display, From)]
13pub enum Input {
14  Step,
15  CreateObject  (object::Variant, Option <object::Key>),
16  ModifyObject  (ObjectModify),
17  DestroyObject (object::Id),
18  SetGravity    (force::Gravity),
19  ClearGravity
20}
21
22/// An object modification event
23#[derive(Debug, PartialEq, From)]
24pub enum ObjectModify {
25  Dynamic (object::Key, ObjectModifyDynamic),
26  Static  (object::Key, ObjectModifyStatic)
27}
28
29#[derive(Debug, PartialEq)]
30pub enum ObjectModifyDynamic {
31  ApplyImpulse   (math::Vector3 <f64>),
32  SetForceFlags  (enumflags2::BitFlags <force::Flag>),
33  SetDrag        (f64),
34  SetPosition    (component::Position)
35}
36#[derive(Debug, PartialEq)]
37pub enum ObjectModifyStatic {
38  Move (math::Vector3 <f64>)
39}
40
41/// An event that results from handling an input
42#[derive(Debug, PartialEq, Display, From)]
43pub enum Output {
44  CollisionResolve   (CollisionResolve),
45  CreateObjectResult (CreateObjectResult),
46  Contact            (Contact),
47  Overlap            (Overlap)
48}
49
50/// A TOI collision
51#[cfg_attr(feature = "derive_serdes", derive(Deserialize, Serialize))]
52#[derive(Clone, Debug, PartialEq)]
53pub struct CollisionResolve {
54  pub toi               : math::Normalized <f64>,
55  pub object_id_a       : object::Id,
56  pub object_id_b       : object::Id,
57  pub contact           : collision::contact::Colliding,
58  pub impulse_normal_a  : math::Vector3 <f64>,
59  pub impulse_normal_b  : math::Vector3 <f64>,
60  pub impulse_tangent_a : math::Vector3 <f64>,
61  pub impulse_tangent_b : math::Vector3 <f64>,
62  pub pseudo_impulse_a  : math::Vector3 <f64>,
63  pub pseudo_impulse_b  : math::Vector3 <f64>
64}
65
66/// A nocollide intersection
67#[cfg_attr(feature = "derive_serdes", derive(Deserialize, Serialize))]
68#[derive(Clone, Debug, PartialEq)]
69pub struct Overlap {
70  pub object_id_a : object::Id,
71  pub object_id_b : object::Id
72}
73
74/// A persistent contact
75#[cfg_attr(feature = "derive_serdes", derive(Deserialize, Serialize))]
76#[derive(Clone, Debug, PartialEq)]
77pub struct Contact {
78  pub object_id_a : object::Id,
79  pub object_id_b : object::Id,
80  pub contact     : collision::Contact
81}
82
83/// Events resulting from object creation
84#[derive(Debug, PartialEq)]
85pub enum CreateObjectResult {
86  /// Success
87  Created      (object::Id),
88  /// Creating the object failed with an intersection.
89  ///
90  /// If intersections were found then the object was not successfully created.
91  Intersection (Vec <(object::Id, collision::Intersection)>)
92}