gizmo_scripting/
api_physics.rs1use crate::commands::{CommandQueue, ScriptCommand};
6use gizmo_math::Vec3;
7use mlua::prelude::*;
8use std::sync::Arc;
9
10pub fn register_physics_api(lua: &Lua, command_queue: Arc<CommandQueue>) -> Result<(), LuaError> {
12 let physics_table = lua.create_table()?;
13
14 {
16 let cq = command_queue.clone();
17 physics_table.set(
18 "apply_force",
19 lua.create_function(move |_, (id, fx, fy, fz): (u32, f32, f32, f32)| {
20 cq.push(ScriptCommand::ApplyForce(id, Vec3::new(fx, fy, fz)));
21 Ok(())
22 })?,
23 )?;
24 }
25
26 {
28 let cq = command_queue.clone();
29 physics_table.set(
30 "apply_impulse",
31 lua.create_function(move |_, (id, ix, iy, iz): (u32, f32, f32, f32)| {
32 cq.push(ScriptCommand::ApplyImpulse(id, Vec3::new(ix, iy, iz)));
33 Ok(())
34 })?,
35 )?;
36 }
37
38 {
40 let cq = command_queue.clone();
41 physics_table.set(
42 "add_rigidbody",
43 lua.create_function(
44 move |_,
45 (id, mass, restitution, friction, use_gravity): (
46 u32,
47 f32,
48 f32,
49 f32,
50 bool,
51 )| {
52 cq.push(ScriptCommand::AddRigidBody {
53 id,
54 mass,
55 restitution,
56 friction,
57 use_gravity,
58 });
59 Ok(())
60 },
61 )?,
62 )?;
63 }
64
65 {
67 let cq = command_queue.clone();
68 physics_table.set(
69 "add_box_collider",
70 lua.create_function(move |_, (id, hx, hy, hz): (u32, f32, f32, f32)| {
71 cq.push(ScriptCommand::AddBoxCollider { id, hx, hy, hz });
72 Ok(())
73 })?,
74 )?;
75 }
76
77 {
78 let cq = command_queue.clone();
79 physics_table.set(
80 "add_sphere_collider",
81 lua.create_function(move |_, (id, radius): (u32, f32)| {
82 cq.push(ScriptCommand::AddSphereCollider { id, radius });
83 Ok(())
84 })?,
85 )?;
86 }
87
88 lua.globals().set("physics", physics_table)?;
89
90 Ok(())
91}