Flight Simulator in BEPU physics

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
mcmonkey
Posts: 92
Joined: Fri Apr 17, 2015 11:42 pm

Flight Simulator in BEPU physics

Post by mcmonkey »

Let's say we want to simulate flight within a BEPU physics simulation for some insane reason.

How would one go about doing this?
(This would make a great demo for BEPU btw)


In particular, conservation of velocity through a rotation. When a plane or gliding winged object rotates, it tends to have velocity in the new direction, rather than simply barreling forth in its previous direction entirely.
How would one simulate this?
Doesn't need to be incredibly accurate - in fact, a more "arcade game" like simulation is preferable for the most part.

Let's start with basics: we have:
- A plane entity
- Thrust
- Brakes
- We're either in the air with starting velocity already or can reasonably get into the air with velocity

Where would one go from here?

I've looked for flight simulator code samples, but it's always /massive/ projects (usually in C or C++) that I can't quite trace my way through :/
(NOTE: The massive part is from having model loading, rendering, control input, menus, dynamic environments, yadda yadda yadda etc. that occupy far more space than wherever the actual movement physics calculations are)

(Note: My actual project at hand is a bit more complex than this and doesn't quite require flight sim stuff, but I'm throwing this part in with the hopes of entertainment)
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Flight Simulator in BEPU physics

Post by Norbo »

For extremely basic arcade-like flight, start with the observation that wings tend to resist motion along the normal of the wing plane. You can model that easily by removing some portion of the velocity along that direction each frame:

Code: Select all

entity.LinearVelocity -= Vector3.Dot(entity.LinearVelocity, wingPlaneNormal) * wingPlaneNormal;
But you probably don't want to remove all of that velocity, so maybe some fraction of it. Or even try some extremely simple approximations of wing surface area and aerodynamics and so on to determine how much to remove.

Maybe you want to simulate some form of lift. You could extend the above:

Code: Select all

var velocityToRemove = Vector3.Dot(entity.LinearVelocity, wingPlaneNormal) * wingDragMultiplier;
var forwardSpeed = Vector3.Dot(entity.LinearVelocity, forwardDirection);
var lift = forwardSpeed * liftMultiplier;
entity.LinearVelocity +=(lift - velocityToRemove) * wingPlaneNormal;
That's pretty hacky and a few miles away from 'physically based', but it does something lift-ish. You'd probably want to include the timestep duration to ensure timestep independence, and that'll affect how you tune it. (It would probably end up looking like how damping works on entities- a coefficient representing how much effect per second that gets converted into a per frame reduction by an exponential relationship.)

To accelerate or decelerate, just apply accelerations along the forward axis. To turn, you could just use a hacky direct angular velocity modification.

Of course there's also the option of a more realistic aerodynamic simulation, but I'll leave that (and the associated research) as an exercise :P
mcmonkey
Posts: 92
Joined: Fri Apr 17, 2015 11:42 pm

Re: Flight Simulator in BEPU physics

Post by mcmonkey »

Yup, that looks about correct for the wings to stay in the air, as that's more or less the code I have already for that part...


But what I don't have is specifically the part where it rotates sanely.

My existing method was to simply rotate the body of the plane using an angular impulse, and let the engines and basic wing code do the rest of the work.


However, this misses several key simulations I want to replicate. For example:
If you fly straight down and pull up, you should have a strong forward velocity (approximately the velocity you were flying/falling down at!)

In my code, all velocity is lost at any time you rotate, and you have to fully regain it using the plane's engines :(
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Flight Simulator in BEPU physics

Post by Norbo »

If you are using something equivalent to the removed-projected-velocity-on-normal above, then that can happen if the turn is too fast. In terms of the math, it's equivalent to an object rolling down a ramp. A tall ramp with a smooth curve will transform the falling motion into a fast horizontal motion. A tall ramp with practically no curve at all (that is, a cliff) will make the object just slam into the ground with no redirection of momentum.

If you want extremely fast turns to result in a preservation of forward speed, you don't really want anything close to 'flight simulation', but rather just completely arbitrary control over velocity. So if you wanted the craft to fly forward at X no matter what kind of maneuvering you're doing, just set the linear velocity to forwardDirection * speed. For something like 'lift', make it forwardDirection * speed + upDirection * liftSpeed. If this is the direction you want to go, there isn't really any specific 'correct' thing you should do, just try crap until it does what you want :P
Post Reply