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, constraint, 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 collision         : constraint::Contact,
58  //pub restitution       : math::Normalized <f64>,
59  pub impulse_normal_a  : math::Vector3 <f64>,
60  pub impulse_normal_b  : math::Vector3 <f64>,
61  pub impulse_tangent_a : math::Vector3 <f64>,
62  pub impulse_tangent_b : math::Vector3 <f64>,
63  pub pseudo_impulse_a  : math::Vector3 <f64>,
64  pub pseudo_impulse_b  : math::Vector3 <f64>
65}
66
67/// A no-collide intersection
68#[cfg_attr(feature = "derive_serdes", derive(Deserialize, Serialize))]
69#[derive(Clone, Debug, Eq, PartialEq)]
70pub struct Overlap {
71  pub object_id_a : object::Id,
72  pub object_id_b : object::Id
73}
74
75/// A persistent contact
76#[cfg_attr(feature = "derive_serdes", derive(Deserialize, Serialize))]
77#[derive(Clone, Debug, Eq, PartialEq)]
78pub struct Contact {
79  pub object_id_a : object::Id,
80  pub object_id_b : object::Id,
81  pub contact     : constraint::Contact
82}
83
84/// Events resulting from object creation
85#[derive(Debug, PartialEq)]
86pub enum CreateObjectResult {
87  /// Success
88  Created      (object::Id),
89  /// Creating the object failed with an intersection.
90  ///
91  /// If intersections were found then the object was not successfully created.
92  Intersection (Vec <(object::Id, collision::Intersection)>)
93}