box2d_rust/events.rs
1// Port of the event types from include/box2d/types.h (events group).
2//
3// The C aggregate views (b2SensorEvents, b2ContactEvents, b2BodyEvents,
4// b2JointEvents) are pointer+count pairs over world-owned arrays; the Rust
5// world API returns slices instead, so only the per-event structs live here.
6//
7// SPDX-FileCopyrightText: 2023 Erin Catto
8// SPDX-License-Identifier: MIT
9
10use crate::collision::Manifold;
11use crate::id::{BodyId, ContactId, JointId, ShapeId};
12use crate::math_functions::{Pos, Vec2, WorldTransform};
13
14/// A begin touch event is generated when a shape starts to overlap a sensor
15/// shape. (b2SensorBeginTouchEvent)
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub struct SensorBeginTouchEvent {
18 /// The id of the sensor shape
19 pub sensor_shape_id: ShapeId,
20 /// The id of the shape that began touching the sensor shape
21 pub visitor_shape_id: ShapeId,
22}
23
24/// An end touch event is generated when a shape stops overlapping a sensor
25/// shape. Always confirm the shape id is valid before use — either shape may
26/// have been destroyed. (b2SensorEndTouchEvent)
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub struct SensorEndTouchEvent {
29 /// The id of the sensor shape. @warning may have been destroyed
30 pub sensor_shape_id: ShapeId,
31 /// The id of the shape that stopped touching. @warning may have been destroyed
32 pub visitor_shape_id: ShapeId,
33}
34
35/// A begin touch event is generated when two shapes begin touching.
36/// (b2ContactBeginTouchEvent)
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub struct ContactBeginTouchEvent {
39 /// Id of the first shape
40 pub shape_id_a: ShapeId,
41 /// Id of the second shape
42 pub shape_id_b: ShapeId,
43 /// The transient contact id. May be destroyed automatically when the world
44 /// is modified or simulated.
45 pub contact_id: ContactId,
46}
47
48/// An end touch event is generated when two shapes stop touching.
49/// (b2ContactEndTouchEvent)
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub struct ContactEndTouchEvent {
52 /// Id of the first shape. @warning may have been destroyed
53 pub shape_id_a: ShapeId,
54 /// Id of the second shape. @warning may have been destroyed
55 pub shape_id_b: ShapeId,
56 /// Id of the contact. @warning may have been destroyed
57 pub contact_id: ContactId,
58}
59
60/// A hit touch event is generated when two shapes collide faster than the hit
61/// speed threshold. (b2ContactHitEvent)
62#[derive(Debug, Clone, Copy, PartialEq)]
63pub struct ContactHitEvent {
64 /// Id of the first shape
65 pub shape_id_a: ShapeId,
66 /// Id of the second shape
67 pub shape_id_b: ShapeId,
68 /// Id of the contact. @warning may have been destroyed
69 pub contact_id: ContactId,
70 /// Point where the shapes hit, a mid-point between the two surfaces.
71 pub point: Pos,
72 /// Normal vector pointing from shape A to shape B
73 pub normal: Vec2,
74 /// The speed the shapes are approaching. Always positive.
75 pub approach_speed: f32,
76}
77
78/// Body move event, triggered when a body moves due to simulation. Not
79/// reported for bodies moved by the user. (b2BodyMoveEvent)
80#[derive(Debug, Clone, Copy, PartialEq)]
81pub struct BodyMoveEvent {
82 pub user_data: u64,
83 pub transform: WorldTransform,
84 pub body_id: BodyId,
85 pub fell_asleep: bool,
86}
87
88/// Joint event, reported for awake joints whose force and/or torque exceed the
89/// threshold. (b2JointEvent)
90#[derive(Debug, Clone, Copy, PartialEq, Eq)]
91pub struct JointEvent {
92 /// The joint id
93 pub joint_id: JointId,
94 /// The user data from the joint for convenience
95 pub user_data: u64,
96}
97
98/// The contact data for two shapes. By convention the manifold normal points
99/// from shape A to shape B. (b2ContactData)
100#[derive(Debug, Clone, Copy, PartialEq)]
101pub struct ContactData {
102 pub contact_id: ContactId,
103 pub shape_id_a: ShapeId,
104 pub shape_id_b: ShapeId,
105 pub manifold: Manifold,
106}