Skip to main content

dynamis_abi/
param.rs

1use crate::StepParamsRecord;
2use bytemuck::Zeroable;
3use dynamis_model::{MaterialCombine, PhysicsConfig};
4
5impl StepParamsRecord {
6    pub fn new(
7        config: &PhysicsConfig,
8        dt: f32,
9        counts: FrameCounts,
10        streams: RowStreams,
11        event_slot: u32,
12    ) -> Self {
13        let FrameCounts {
14            dynamic_bodies,
15            bodies,
16            body_ids: _,
17            colliders,
18            constraints,
19            particles,
20            elements,
21            soft_bodies,
22            attachments,
23        } = counts;
24        Self {
25            gravity: [config.gravity[0], config.gravity[1], config.gravity[2], 0.0],
26            dt,
27            substep_dt: dt / config.substeps as f32,
28            damping: config.damping,
29            angular_damping: config.angular_damping,
30            body_count: bodies,
31            substeps: config.substeps,
32            solve_iterations: config.solve_iterations,
33            position_iterations: config.position_iterations,
34            soft_iterations: config.soft_iterations,
35            soft_substeps: config.soft_substeps,
36            constraint_count: constraints,
37            relaxation: config.relaxation,
38            slop: config.slop,
39            contact_margin: config.contact_margin,
40            restitution_threshold: config.restitution_threshold,
41            max_velocity: config.max_velocity,
42            max_angular_velocity: config.max_angular_velocity,
43            dynamic_count: dynamic_bodies,
44            collider_count: colliders,
45            sleep_velocity: config.sleep_velocity,
46            sleep_angular_velocity: config.sleep_angular_velocity,
47            sleep_time: config.sleep_time,
48            friction_combine: combine_code(config.friction_combine),
49            restitution_combine: combine_code(config.restitution_combine),
50            edit_run_count: streams.edit_runs,
51            body_move_count: streams.body_moves,
52            constraint_move_count: streams.constraint_moves,
53            observed_count: streams.observed,
54            event_slot,
55            particle_count: particles,
56            element_count: elements,
57            soft_substep_dt: dt / config.soft_substeps as f32,
58            soft_body_count: soft_bodies,
59            attachment_count: attachments,
60            settle_velocity: config.settle_velocity,
61            ..Self::zeroed()
62        }
63    }
64}
65
66#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
67pub struct FrameCounts {
68    pub dynamic_bodies: u32,
69    pub bodies: u32,
70    pub body_ids: u32,
71    pub colliders: u32,
72    pub constraints: u32,
73    pub particles: u32,
74    pub elements: u32,
75    pub attachments: u32,
76    pub soft_bodies: u32,
77}
78
79#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
80pub struct RowStreams {
81    pub edit_runs: u32,
82    pub body_moves: u32,
83    pub constraint_moves: u32,
84    pub observed: u32,
85}
86
87#[derive(Clone, Copy)]
88pub enum Count {
89    Bodies,
90    Dynamic,
91    Colliders,
92    Constraints,
93    Particles,
94    Elements,
95    Attachments,
96    SoftBodies,
97    EditRuns,
98    BodyMoves,
99    ConstraintMoves,
100    Observed,
101}
102
103impl Count {
104    pub const fn field(self) -> &'static str {
105        match self {
106            Self::Bodies => "body_count",
107            Self::Dynamic => "dynamic_count",
108            Self::Colliders => "collider_count",
109            Self::Constraints => "constraint_count",
110            Self::Particles => "particle_count",
111            Self::Elements => "element_count",
112            Self::Attachments => "attachment_count",
113            Self::SoftBodies => "soft_body_count",
114            Self::EditRuns => "edit_run_count",
115            Self::BodyMoves => "body_move_count",
116            Self::ConstraintMoves => "constraint_move_count",
117            Self::Observed => "observed_count",
118        }
119    }
120
121    pub const fn rows(self, params: &StepParamsRecord) -> u32 {
122        match self {
123            Self::Bodies => params.body_count,
124            Self::Dynamic => params.dynamic_count,
125            Self::Colliders => params.collider_count,
126            Self::Constraints => params.constraint_count,
127            Self::Particles => params.particle_count,
128            Self::Elements => params.element_count,
129            Self::Attachments => params.attachment_count,
130            Self::SoftBodies => params.soft_body_count,
131            Self::EditRuns => params.edit_run_count,
132            Self::BodyMoves => params.body_move_count,
133            Self::ConstraintMoves => params.constraint_move_count,
134            Self::Observed => params.observed_count,
135        }
136    }
137}
138
139fn combine_code(combine: MaterialCombine) -> u32 {
140    match combine {
141        MaterialCombine::Multiply => 0,
142        MaterialCombine::Min => 1,
143        MaterialCombine::Max => 2,
144        MaterialCombine::Average => 3,
145    }
146}