Jondolf / avian

ECS-driven 2D and 3D physics engine for the Bevy game engine.
https://crates.io/crates/avian3d
Apache License 2.0
1.36k stars 108 forks source link

How do I make things fly ? #106

Closed bestouff closed 1 year ago

bestouff commented 1 year ago

I'd like to grave some objects fly, i.e. I'd like to be able to compute their lift, drag, and have everything work like by magic. Is that something possible ? If yes how would I do that ? 1-entity constraints ?

Jondolf commented 1 year ago

So you want to simulate aerodynamics, like a paper slowly falling or an airplane gliding? This is not built-in functionality, but it is technically possible, although quite difficult. Games tend to fake aerodynamics since it can be expensive and it can quickly become very complex. For example, if you had a plane controller, you could apply forces and torques to the plane depending on the wing flap orientation and the plane's velocity, and ignore the precise aerodynamics of each individual part of the plane.

There was also discussion about position-based aerodynamics on the Ten Minute Physics (one of the XPBD authors) Discord channel's questions-and-help channel a couple of weeks ago. You can find a link to the Discord here (not sure if allowed to post direct link).

One of the people there has a 2D version working by forming rigid bodies from several individual particles, and computing lift/drag on a per-particle basis. I think they do something like this for every particle (in C#, not Rust):

float dotFactor = max(|v|^2 * dot(normal, vDir), 0);
float2 dampDelta = -vDir * dotFactor * dampFactor;
float2 liftDelta = Normal(vDir) * sign(Cross(normal, vDir))) * dotFactor * liftFactor;
// Apply deltas to rigid bodies via "Detailed Rigid Body Simulation with Extended Position-based Dynamics"'s
// positional constraints.

So yes, it's possible with constraints, but it requires quite a lot of work. I would recommend "faking it" by just applying forces and torques to rigid bodies based on some factor, like in the case of an airplane, the wings' orientation and velocity.

bestouff commented 1 year ago

Ok I get it. Stupid newbie question: how do I apply forces on a body ?

Jondolf commented 1 year ago

You can use ExternalForce and ExternalTorque similar to Rapier. Here are the 3D docs for it: https://docs.rs/bevy_xpbd_3d/latest/bevy_xpbd_3d/components/struct.ExternalForce.html

bestouff commented 1 year ago

Ooh that's perfect. Thank you very much !