- Home /
Glider Physics (Engine)
Hello all,
I want to build a simulation of a glider and am wondering if you think the physics engine can provide a reasonably accurate prediction? It is for an educational application so it is kind of important that it reflect reality (again reasonably).
This particular glider is launched in much the same way as a slingshot, so I think the following thread provides an excellent start ...
http://answers.unity3d.com/questions/120661/2d-gliding-physics.html
My thought is that a glider launched in this way should largely behave similarly to a projectile, but obviously there will be lift and drag applied at the wing and tail position (which I can add using AddForceAtPosition??). The magnitude of these lift and drag forces will depend on the angle of the force (angle of attack) which will be changing throughout the simulation.
The lift forces acting on the tail and wing will cause the plane to want to rotate about its center of gravity. I am assuming / hoping that the physics engine can capture this rotation and perform the lift calculations (based on the angle produced by this rotation) and just keep things rolling until the plane lands on the ground ...
So can the physics engine handle this? Any advice from you physics (engine) buffs?
Thanks!
EDITED: Thanks Aldonetto! This is a huge help!
Just a couple more silly questions (sorry but the physics engine is a new beast to me):
1) units (mass) - I read somewhere that mass was in kg, so I am using a mass of 0.01 (about 10g for a balsa glider). Sound right?
2) units (force) - what are the units in force - Newtons?
3) I can apply an initial force (using AddForce with ForceMode.Impulse) - currently using 1.4 assuming Newtons are the units, or as you suggest I could use an initial velocity. But if go the velocity route, how do I ensure that this is applied in the first frame only (i.e. analogous to ForceMode.Impulse)
Thanks!
@trobertson, please use the button add new comment to post replies or comments - the box Your answer should be used only to answer the question. UA is different from forums, but this isn't clearly explained, and many newcomers make this same mistake.
So in this very simple example, I am launching a "projectile" with a mass of 0.01 kg with an initial force of 1.4 newtons, and no lift or drag - just gravity doing its thing.
Everything looks good, but the velocity that it being reported (if I am interpreting it correctly) is between about 100 m/s and 140 m/s and the projectile lands about 20 seconds later a distance of about 2000 m from the launch position.
This seems much too fast and the distance too far for these inputs. Thinking that the mass was too low for the system, I also changed this and the launch force by a factor of ten (m = 0.1, F = 14) and get the same results ...
What am I missing? Thanks!
var m : float = 0.01; // kg
var direction : Vector3 = Vector3(1,1,0); var launchForce : float = 1.4; // newtons
function Start () { rigidbody.mass = m; }
var launched : boolean = false;
function FixedUpdate () { if (launched==false) { rigidbody.AddForce(direction.normalized*launchForce,Force$$anonymous$$ode.Impulse); launched = true; }
transform.right = rigidbody.velocity.normalized; print(rigidbody.velocity.magnitude); }
Very surprising! Try the following: remove the Force$$anonymous$$ode.Impulse - physics will use the default Force$$anonymous$$ode.Force, which should last for the physics cycle time (0.02s).
NOTE: what scale your glider use? I suspect that it can affect physics if different from (1,1,1). If the scale is different, use any other thing - a sphere or cube - just to see if the results are reasonable.
Getting something much more reasonable now ...
I am using an initial velocity (still dont quite know why the force input is not working properly) ... but the main issue is that the velocity needs to be broken up into it proper components.
So with a 30 degree launch angle these components are:
rigidbody.velocity.x=launchVelocity*$$anonymous$$athf.Cos($$anonymous$$athf.Deg2Rad*launchAngle); rigidbody.velocity.y=launchVelocity*$$anonymous$$athf.Sin($$anonymous$$athf.Deg2Rad*launchAngle); rigidbody.velocity.z=0.0;
this gives me a distance and time much more consistent with an actual projectile.
Thanks!
You could define a unitary length dir vector and multiply by the launchVelocity:
var angle = launchAngle * $$anonymous$$athf.Deg2Rad; var dir = Vector3($$anonymous$$athf.Cos(angle), $$anonymous$$athf.Sin(angle), 0); rigidbody.velocity = dir * launchVelocity;This does the same thing, but is easier to understand.
About the force: I tested a force in Force$$anonymous$$ode.Force during one physics cycle, and the resulting velocity was compatible with the theory. I never used Force$$anonymous$$ode.Impulse however, and the docs are mysterious about it - they say: In this mode, the unit of the force parameter is applied to the rigidbody as mass distance / time* ?!?!
Answer by aldonaletto · Nov 28, 2011 at 11:52 PM
The physics engine handles ballistic trajectories very well: just launch the glider by setting its rigidbody.velocity to some suitable value and direction, and it will follow a perfect ballistic trajectory. I suppose it will not automatically align its nose to the trajectory direction, what can be forced with this simple code:
function FixedUpdate(){ transform.forward = rigidbody.velocity.normalized; }This would produce a very decent ballistic flight, but to have something more consistent with a glider behaviour you should replace this code with evaluations of the lift and drag forces. You could use the rigidbody.velocity and transform.forward vectors to calculate these forces, and AddForceAtPosition to apply them (as you suspected). But AddForceAtPosition expects a world point, thus you must define the local position and convert it to world coordinates using transform.TransformPoint.
The idea is something like this:
var tailPoint = Vector3(0, 0, -1.5); // tail position relative to the glider pivot var wingPoint = Vector3(0, 0.1, 0.2); // wing relative position
function CalcTailForce(): Vector3 { // calculate the tail force vector with direction and magnitude based on the // angle between rigidbody.velocity and transform.forward return force; }
function CalcLiftForce(): Vector3 { // calculate the lift force magnitude based on the angle (in the vertical plane) // between rigidbody.velocity and transform.forward return force * transform.up; // the lift direction is always the local UP }
function FixedUpdate(){ var tailForce = CalcTailForce(); // apply the force to the tail point rigidbody.AddForceAtPosition(tailForce, transform.TransformPoint(tailPoint)); var liftForce = CalcLiftForce(); // apply the lift force to the wing point rigidbody.AddForceAtPosition(liftForce, transform.TransformPoint(wingPoint)); } I don't know exactly how to calculate these forces, and guess you'll have a hard time tweaking the equations to have a stable flight, but I believe the basic idea can work.
EDITED: About questions 1 and 2: yes, Unity uses the MKS system, thus mass is in Kg, and force in Newtons. The 0.01 mass is too low for the standard settings; you probably will have to reduce rigidbody.sleepVelocity to be compatible with such small object - the default value is 0.15, what means that below 15cm/s physics will place the rigidbody in sleep mode (the glider just gets stuck in the air!).
About question 3, the force/velocity dilemma: you usually will launch the glider with a code like this:
function Update(){ if (Input.GetKeyDown("some key")){ // use the velocity approach: rigidbody.velocity = launchVelocity * transform.forward; // or the force approach: rigidbody.AddForce(launchForce * transform.forward, ForceMode.Impulse); } }Both will have the same final effect: set the rigidbody velocity. With AddForce, physics calculates the resultant velocity after one physics cycle (20mS) of continuous acceleration, what means that the glider mass and the force intensity are taken into account: 1.4N applied to 0.01Kg mass gives an acceleration of 140m/s 2, which during 20mS results in 2.8m/s. The velocity approach is simpler - just assign a 2.8m/s speed - but in your case the force approach may be better, since it will show the individual influence of force and mass in the glider flight.
The physics engine doesn't include air resistance or ter$$anonymous$$al velocity, so you may want to add those.
Your answer
Follow this Question
Related Questions
Flight mechanics 2D, variables vs forces 1 Answer
Flying movement 0 Answers
2D 360 degress platformer example needed 0 Answers
Dynamic flight physics help. 0 Answers
Rubbery hinge joints 1 Answer