#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct GpuBox {
pub position: [f32; 3],
pub mass: f32,
pub velocity: [f32; 3],
pub state: u32,
pub rotation: [f32; 4],
pub angular_velocity: [f32; 3],
pub sleep_counter: u32,
pub color: [f32; 4],
pub half_extents: [f32; 3],
pub _pad: u32,
}
impl GpuBox {
pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<GpuBox>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Instance,
attributes: &[
wgpu::VertexAttribute {
offset: 0,
shader_location: 8,
format: wgpu::VertexFormat::Float32x4,
},
wgpu::VertexAttribute {
offset: 16,
shader_location: 9,
format: wgpu::VertexFormat::Float32x4,
},
wgpu::VertexAttribute {
offset: 32,
shader_location: 10,
format: wgpu::VertexFormat::Float32x4,
},
wgpu::VertexAttribute {
offset: 48,
shader_location: 11,
format: wgpu::VertexFormat::Float32x4,
},
wgpu::VertexAttribute {
offset: 64,
shader_location: 12,
format: wgpu::VertexFormat::Float32x4,
},
wgpu::VertexAttribute {
offset: 80,
shader_location: 13,
format: wgpu::VertexFormat::Float32x4,
},
],
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct GpuCollider {
pub shape_type: u32, pub _pad1: [u32; 3],
pub data1: [f32; 4], pub data2: [f32; 4], }
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct GpuJoint {
pub body_a: u32, pub body_b: u32, pub joint_type: u32, pub flags: u32, pub anchor_a: [f32; 3], pub compliance: f32, pub anchor_b: [f32; 3], pub damping: f32, pub axis: [f32; 3], pub max_force: f32, }
impl GpuJoint {
pub fn ball(body_a: u32, body_b: u32, anchor_a: [f32; 3], anchor_b: [f32; 3]) -> Self {
Self {
body_a,
body_b,
joint_type: 0,
flags: 1, anchor_a,
compliance: 0.0,
anchor_b,
damping: 0.0,
axis: [0.0, 1.0, 0.0],
max_force: 0.0,
}
}
pub fn hinge(
body_a: u32,
body_b: u32,
anchor_a: [f32; 3],
anchor_b: [f32; 3],
axis: [f32; 3],
) -> Self {
Self {
body_a,
body_b,
joint_type: 1,
flags: 1,
anchor_a,
compliance: 0.0,
anchor_b,
damping: 0.0,
axis,
max_force: 0.0,
}
}
pub fn fixed(body_a: u32, body_b: u32, anchor_a: [f32; 3], anchor_b: [f32; 3]) -> Self {
Self {
body_a,
body_b,
joint_type: 2,
flags: 1,
anchor_a,
compliance: 0.0,
anchor_b,
damping: 0.0,
axis: [0.0, 1.0, 0.0],
max_force: 0.0,
}
}
pub fn spring(
body_a: u32,
body_b: u32,
anchor_a: [f32; 3],
anchor_b: [f32; 3],
stiffness: f32,
damping: f32,
) -> Self {
let compliance = if stiffness > 0.0 {
1.0 / stiffness
} else {
0.0
};
Self {
body_a,
body_b,
joint_type: 3,
flags: 1,
anchor_a,
compliance,
anchor_b,
damping,
axis: [0.0, 1.0, 0.0],
max_force: 0.0,
}
}
pub fn slider(body_a: u32, body_b: u32, axis: [f32; 3]) -> Self {
Self {
body_a,
body_b,
joint_type: 4,
flags: 1,
anchor_a: [0.0; 3],
compliance: 0.0,
anchor_b: [0.0; 3],
damping: 0.0,
axis,
max_force: 0.0,
}
}
pub fn breakable(mut self, force: f32) -> Self {
self.max_force = force;
self.flags |= 2; self
}
}
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct PhysicsSimParams {
pub dt: f32, pub _pad0: [u32; 3], pub _pad1: [f32; 3], pub _pad1b: u32, pub gravity: [f32; 3], pub damping: f32, pub num_boxes: u32, pub num_colliders: u32, pub num_joints: u32, pub _pad2: u32, }
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct DebugVertex {
pub position: [f32; 3],
pub color: u32, }
impl DebugVertex {
pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<DebugVertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float32x3,
},
wgpu::VertexAttribute {
offset: 12,
shader_location: 1,
format: wgpu::VertexFormat::Uint32,
},
],
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct DebugParams {
pub num_boxes: u32,
pub num_joints: u32,
pub show_wireframes: u32, pub _pad: u32,
}