Ohysics with rapier, 3d
This is the system that calculates the general thrust vector of a ship based on which engines are running:
fn apply_thrust(
mut ship: Query<(&mut ExternalForce, &ReadMassProperties), With<Ship>>,
engines: Query<(&ThrustVector, &Name, &GlobalTransform)>,
input: Res<Input<KeyCode>>,
) {
let Ok((mut ship_force, ship_mass)) = ship.get_single_mut() else {
return
};
info!("System found a ship");
let mut result_thrust = ExternalForce::default();
for (vector, name, global_transform) in engines.iter() {
let is_engine_running = match name.as_ref() {
"Engine 0" => input.pressed(KeyCode::Key1),
"Engine 1" => input.pressed(KeyCode::Key2),
"Engine 2" => input.pressed(KeyCode::Key3),
_ => unreachable!(),
};
if is_engine_running {
result_thrust += ExternalForce::at_point(
(vector.0)(global_transform) * ENGINE_POWER,
global_transform.translation(),
ship_mass.0.local_center_of_mass + global_transform.translation(),
)
}
}
*ship_force = result_thrust;
}
But for some reason it doesn't find any entities. Here's how I spawn the ship:
commands
.spawn((
SceneBundle {
scene: model,
transform: Transform::from_xyz(0.0, 0.0, 0.0),
..default()
},
Name::from("Ship"),
Collider::cuboid(3.0, 1.0, 1.5),
ColliderMassProperties::Density(1.0),
RigidBody::Dynamic,
ExternalForce::default(),
Ship,
))
.with_children(|cb| {
// ...
Each engine (child of the ship) also has a collider and collider mas properties. I tried swapping RigidBody as label component to Ship, also tried adding ExtarnalForce::default() when I spawn the ship entity, but to no avail.