Search Unity

Air Resistance with push-back supported?

Discussion in 'Physics' started by DragonCoder, Jul 3, 2015.

  1. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,698
    Hello Community,

    currently I'm preparing for a rather big software project in college.
    For this, I need a 3D Physics Environment/engine which supports more or less realistic air friction, because what I want to achieve is to enable a skeletal model of a bird to fly on it's own wings (driven by a genetic algorithm).
    That means I probably cannot give the whole model a simple friction according to the movement but the wings need to be able to push off "air" of some sort, depending on their angle, and so on.

    Right now I'm still looking for the right environment and Unity3D is an amazing thing I'd like to work with :) But that would only work if this is somehow supported natively, since I do not have the time to implement actual physic-algorithms.
    So would Unity3D be the right environment for this?

    Physical correctness (like in professional airflow simulations) is not required, but the system needs to be real-time capable.
    The programming language is also open (but it'd be a point as I know C#, Java and Lua already..)

    Hope someone can help me :)
    Thanks in advance!

    Greetings,
    Dra
     
  2. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    I think Unity is great for that, but of course there isn`t any advanced air simulation, just simple drag parameter which acts as sum of all frictions in air (there is also friction per object to object, but air isn`t count as that ;) ). But You can adjust this drag per single component, so You will have to simulate or rather calculate add force from wings movement, drag, angle and so on.

    Unity doesn`t have any tools to calculate friction with air by it`s real size, sadly, but I didn`t heard of any game engine which has it.
     
  3. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,698
    Thank you very much for your answer!
    From what I've read so far, "drag" wont really fulfill this purpose though, I think.
    I mean, does it apply on angular rotation too and doesn't it just base on mass?
    Let's say I have a simple plate attached with a hinge-joint to an Object.
    When I apply a torque to the horizontal plate so it's loose end moves downwards, will the attached object gain a vertical force?
    And if I make the plate smaller but give it the same mass and same torque-speed, will the vertical force on the other object be smaller?

    Those two features would be required and I'm not sure whether I could add them by myself...
     
  4. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    Yes, as far as my own experiments on Unity engine showed me that Rigidbodies, mass and colliders (colliders are bodies/mass of rigidodies) are enough nicely done - I particular do experiments on hits with artillery shells - final rotation of target after hit is dependant on shell mass, how it is build (colliders) - so size matters, angles matters, speed, etc. It works good enough, at least for my needs, and for collage bird simulation should be good enough too.

    About second question - You have drag and angular drag , one is for velocity, second one for torque :) And of course, drag is mass dependant, so it is physical accurate.
     
  5. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,698
    Not sure whether you understood right what I meant.
    Mass-dependency of course, but I also need it to be size-dependent as in dependent on the surface which moves. When the bird flies, it will bend its wings to rise them and then open them again to flap them down.
    The mass of the wing stays the same, so if the engine doesn't take the actual surface into account, the bird could impossibly fly, since the forces applied on the body would be identical when rising and when flapping the wings...

    No, I'm pretty sure that "drag" is not what I need here.. :/
     
  6. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    Yes, I thought about it some. As I sad at beginning - there is no air pressure in any game engine, and drag only slows down rigidbody, so no easy solution here. But if You want to achieve that, You have to do some calculations by Yourself. But most important for You are forces - if You calculate them, You have working function - addforceatposition - and You can apply that force anywhere You want. So, if You want to simulate underwing force up - You add transform.up force at bottom of wings (as it is point force You should do it multiple times along whole wing). If You want to simulate drag, friction, calculate force and add it in right position.

    So there is no simulation of air pressure, but if You can calculate forces, You can do everything You need to, to simulate flying of a bird.
     
    Roni92pl likes this.
  7. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    DragonCoder, you'll have to write your own stuff to do that. Unity doesn't do aerodynamic stuff on the meshes, it just uses that simple drag function on the CG as far as I know.

    I'm doing boats with my own approach that works like you probably would want your birds to work, where the forces are all done on the individual polys of the meshes:

    http://www.speedboatsim.com/

    I spent probably two months writing the physics end of the boat sim, but there's some special stuff there that you might not need, and I spent a lot of time optimizing it doing things like this:

    http://www.performancesimulations.c...ses-in-unitys-physics-or-any-math-heavy-code/

    If all you want to do is a simple normal force on each triangle, it's not really that hard. My boats will fly (and subsequently crash :p ) if they're going fast enough, so I think this would work just fine for birds and so on if you don't care about stall effects at high angles of attack. Unity and PhysX won't do it out of the box though.
     
  8. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    This is nice article on performance! Thx! (It most propably won`t be needed with one bird, but never the less, very informative article on easing CPU usage)
     
  9. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,698
    Thank you for your replies, guys!

    Looks like I wont get around programming this part myself.
    So since I didn't have to celebrate my country all day yesterday xD I spontaneously programmed a small simulation!

    The following idea came to my mind before:
    What if instead of trying to think about what the air does to the wings, I just calculate the volume which the wings travel each step through the air?
    I mean if I can sum up the whole "difference" from one step to another, the result will be all the virtual "air" which the wing has pushed within this step.
    In the end this value tells how strong and in what direction the force has to be applied.

    Well, for this prototype I reduced the whole concept to simple 2D wings made of elements which are connected to each other with hinges. The hinges are constantly alternating like a sinus wave between two angles. For this example I used -25° and 40° for the left wing and the negated values for the right wing:

    Here you can see the wing movement but without applied force. Therefore the center doesn't move.
    Full Static.gif
    The wing is the darker blue line and the bright quadrangles symbolize the amount of air pushed off by each element. They are just a visualization and not part of the actual physic simulation.

    Next I applied the summed area to the object, simply changing its y-value on the screen. The result was a bird flying perfectly up and down but since the way all segments were doing were identical when swinging and and when swinging down. the bird simply remained in place after a whole flap of the wings.

    However, it was surprisingly easy to break this balance. Only the two inner elements have a headstart of 30% (that's already seen in the upper animation) and it already caused a difference of the maximal force between swinging up and swinging down.
    You can also see this slight difference as the graph moves more in the green area (= force upwards) than in the red area (negative force downwards).

    And well, what shall I say? It flies! It flies!!! Muahaha! XD

    With the movement "unleashed" (yet still without gravity) it looks like this:


    To be honest I'm amazed how well that all worked. This conecpt allows already a lot of variables to use as "genomes" in the genetic algorithm (which I'll probably do directly in 3D).

    If someone wants to try this himself, you can find the zipped GM81 file down there. This is for Game Maker 8.1 (the program I'm the most fond of for prototypes, lol) but you can also open import it in the free version of Game Maker Studio.
    The code is hopefully easy to be understood and mostly commented (as I'll probably show it to my prof too).


    Still a question to the physic experts here around: Is this form of calculating the force "realistic"?
    Of course it's only an assumption as interactions between moving air is not taken into account (every segment works on its own) and air-pressure is not being taken into account either. In this example I applied the resulting forces directly to the root/center. In 3D with a rigid physics engine (msot likely the one in Unity3D), I'll apply it individually to each segment of the wing.
    So would you say that those are too many breaks with the physic laws for my purpose already?


    Again thank you for your help :)
     

    Attached Files:

    Karwoch likes this.
  10. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    Try to implement gravity, and see if bird will still be flying :). Very nice work, I must say. But in this simulation You do not take to account one of the most important forces, which lets bird stay in air - the difference of pressure below and on top of the wing - the exactly same force which use planes on wings to stay in air, or sail boats on their sails to move forward (sail is a kind of wing, but rotated on one of its axis).

    If You would create working this model, You would simulate a pretty good hommingbird - this unique bird can still be in air without previously mention force. But rest of birds mostly use difference between pressures to stay in air. So, if You now will create a flying model of a bird without moving wings, but only with some speed (in real this speed is reduced timely by air friction, that is why most birds uses Your movement to speed up again), and of course speed is directly related to its "stay in air" capabilities. So, if You do this, and than combine those two forces, You should get pretty good simulation of flying bird.

    And of course there is a lot more to it, most of them You did mention in Your post, but most important is to create approximate simulation from most important forces, and eventually then create more precise model. Keep up good work, this looks like fun :)
     
  11. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Yep, that ought to work and is quite realistic. What you're doing then is essentially a momentum transfer which is what's really going on even in the case Karwoch mentioned of the wing surface pressure differentials. I.e., you impart a momentum (mass * velocity) to the air and the reaction is the lift. I saw a NASA article that kind of debunks the whole Bernoulli effect thing and attributes all the lift to molecular collisions very much like you are imagining, so I wouldn't worry too much about that. In the end it's all action/reaction and can be treated as a momentum transfer. That's essentially how my boat simulation works and things do indeed fly pretty accurately, more so than I thought they would. :)

    http://speedboatsim.com
     
  12. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Another way which is the same thing you're doing is this:

    force = 1/2 * air density * velocity^2 * area

    Apply the force in the direction of the triangle normal and take the "velocity" above to be triangles velocity component along the triangle normal. That's the same thing as what you appear to be thinking about. That's how one of the forces (the main one, really) in my boat simulator is computed.
     
  13. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,698
    @Karwoch
    Yes, you are right, that's the part missing indeed!
    In fact since I'm thinking about making a dragon, it might work without that since I imagine their build to be pretty similar to the one of a bat and they don't have this characteristic curve like the wings of planes or of most birds.
    However, it would be interesting to simulate this too. Not sure how to realize that though since that requires to take into account a whole area and not only the segment (or the triangle in 3D) alone.

    Perhaps I could compute it by finding the opposite segment/triangle of the wing and then compare it's angle relative to the movement-direction. This difference would give a hint of the distance the air has been traveling along the the upper side of the wing and along its underside. After all this distance-difference is the whole reason for the pressure difference on a typical plane wing sides.
    Not sure how much performance this would require.
    (Argh, hope this paragraph can be understood.. I'm not used to describing such details in English :( )


    @Todd Wasson
    Interesting formula! Where exactly do you have it from? o:
    I think that my method is a bit more accurate than the formula though. Yours doesn't take into account how the normal of the triangle changes within one step, does it?
    Guess a boat doesn't change its direction that fast though, so that probably wouldn't matter in your case and yours costs a lot less performance. Computing an area, not to mention a volume needs quite a lot of calculations.
    Is it possible to do such calculations with Unity 3D in a compiled language? C# or JS might be too slow...

    I'll experiment with both methods in 3D :)
     
    Last edited: Jul 6, 2015
  14. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    It's standard fluid dynamics that's been around since the dinosaurs. In this case it's just dynamic pressure times area to compute force: https://en.wikipedia.org/wiki/Dynamic_pressure


    Computational methods like these are always approximations to one degree or another. It's the nature of computing one time step at a time like this where no analytical solution differentiated in time is workable.

    The normal vector changes each time step. I just use the "current" normal. You could go further with interpolation or a midpoint algorithm or something but it's doubtful you'll get much improvement. Normally what you do is just decrease the time step until it's close enough. We are making other approximations here anyway, right? Are you computing turbulence, skin friction, and the rest? Probably not, so this small error is probably not any bigger than the other errors.

    Your formula is probably equivalent in the end. It's guided by the same basic thought process.

    My propellers work that way too. The individual blades change direction a lot faster than a birds' wings. It works fine. Because the propeller spins so quickly I solve it at a higher frequency. I.e., inside FixedUpdate() I move and solve the propeller multiple times (10 right now, so it's done at 1000 or 2000 Hz). It works without doing that, but in my case it's a little specialized because it's important that the propellers can operate well when they're partly surfaced, where one or more blades (or parts of a blade) are in the air and the rest is in the water.

    If the triangle sizes don't change then their areas only need to be computed one time at the start of the simulation. Computing a volume needs a lot of computations, yes, but I don't do that.

    Both C# and JS are compiled. C# is plenty fast for this kind of thing, and if you want to get really carried away with forces you could always do it in a compute shader. I'm getting away with thousands of forces right now with C# and only using 5% or 10% of the processing time for the physics, easily enough to do a bird like yours, and I'm not using a compute shader.

    See that "Unity Physics Optimizations" link I posted earlier for more info. I describe there some of the ways to get really huge speed increases. This takes some work and thought, but what you want to do is entirely possible. I've thought of doing the same thing you are with the birds and such. :)

    I assume you've seen this?



    I'm pretty sure he was using the dynamic pressure/force formula like I am. If you want to compute the force of a surface moving through a fluid, that's the way to do it. It'd certainly be a lot faster computationally than computing the volume, and you'd know you're using the same formula used in engineering and CFD.
     
    Last edited: Jul 7, 2015
  15. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,698
    Ahh, thank you for clarification!

    C# is JIT-Compiled, from what I know. So there's usually still at least the factor two to any truly compiled language on average.
    Yes, most likely it suffices.. still a reason I'm a bit undecided between going with Unity3D or with the Unreal Engine 4 (C++) though...
    Thank you for the hints in every case - great work! I'll look into them closer when I start with the actual programming.

    However, well, you are right, I should probably go the easiest way with the formula, especially since my main focus is on the genetic algorithm itself after all.
    Still I will try to realize a method to take pressure-difference into account too since that might play a more significant role here after all.
    And.. I want to see the dragon glide too xD

    Yep, I saw a vid with a part of this in a class at the MIT:

    Somewhere around minute 40.
    Quite interesting!
     
  16. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Paper airplanes glide just fine without airfoils and Bernoulli. ;)
     
  17. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    I don't know for sure so maybe somebody can set me straight on this if I'm wrong, but I really doubt that this matters a whole lot with math code. Doesn't the compiler just break it down to assembly in the end? If it was interpreted, sure, that'd be a good point. Otherwise I don't understand why it'd make any difference. An addition op is an addition op, isn't it? If not, how does it being JIT-Compiled change things?

    I think you'll probably find that if your birds can learn to fly, they'll do it just fine without worrying about the air pressure difference across the bottom and top of the wing. Like I said before, there's an article at the NASA site that deals with this and it basically says it's sort of nonsense when you look at it all from a momentum perspective because most of the treaties on wings are attributing it to the Bernoulli effect which is only really valid in a closed system, like a venturi. So don't let yourself get too worked about that. The high pressure area under the lifting surface is mainly due to the dynamic pressure effect described in the link where the air is being redirected downward. The particular flow details might be a little different but in the end it's all just a momentum transfer. Action/reaction.

    In other words, by doing it the way I'm talking about, you will indeed get higher pressure below the wing (especially as it flaps downward) than you will above it. Just treat the top side (or any surface with the normal facing away from the velocity) as being atmospheric and don't worry too much about the little discrepancies there. It's kind of minor in the end. To make up the difference your birds will just end up with a slightly larger angle of attack (assuming they learn to fly). Then you'll have > atm pressure under the wing (dictated by dynamic pressure) and atmospheric above it in the presence of any angle of attack whatsoever. So you will indeed have a pressure difference there just doing it the way I described.

    I've written a couple of genetic algorithms, they're pretty cool for this type of stuff. They're especially interesting because they're modelled after evolution. I wrote a particle swarm algorithm which is used to solve similar problems that genetic algorithms are aimed at which I used to train neural networks to drive AI race cars. It's rather amazing to watch the system learn to do a better job than I can tune by hand, and do it pretty quickly.

    Your birds sound fun. Keep me posted on how it goes, I'd like to hear all about it. :)
     
    Last edited: Jul 7, 2015
  18. Karearea

    Karearea

    Joined:
    Sep 3, 2012
    Posts:
    386
    Wish I could find it again, but I remember reading an article somewhere that had an aerodynamicist describing how something the size of a Skyhawk (A4 rather than C172!) would probably fly 'ok' with a wing cross-section similar to a sheet of plywood and a high enough rotate speed. Really enjoyed this thread, also interested to see the outcome of the birds :D
     
    Todd-Wasson likes this.
  19. Glurth

    Glurth

    Joined:
    Dec 29, 2014
    Posts:
    109
    So, the OTHER component of the velocity, the one perpendicular to your normal, is the speed of the air over the surface. Could this velocity, over the triangles area, be useful in computing a pressure differential for an airfoil?
    Since (relatively)faster moving air, of the same density, has lower pressure: I THINK we can use this velocity component and the area to generate a "pressure force" (ALSO in the direction of the surface normal). Then sum up the forces of all the faces in the given solid, to see if any "lift" (or whatever) is left over, after most of the "pressure force" cancels out.

    Todd: when you compute your normal force, do you do it on all faces, or just the ones that face "forward"? What about faces that have a normal pointing away from the direction of movement "backwards"? If you DO use all faces, what keeps them from canceling out? Is it just the shape of the object?

    Sorry about posting on a months old thread, figured it was better than starting new one.
     
  20. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    In my solution the back faces are ignored. Most of the triangles are computed but when one is found that faces away from the velocity vector, the force goes to 0. In effect, any triangle with the normal facing away from the velocity vector is operating at atmospheric pressure. So you would indeed get a pressure differential on an airfoil operating at some angle of attack because the opposite side (the bottom of the wing with positive angle of attack) is greater than atmospheric. You just wouldn't get the pressure reduction in the top part of the airfoil, so the lift might be a little bit less than it would in the real wing. You could just scale up the force a bit through a coefficient to make up for the difference though.

    You can see it working pretty well right here on something not airfoil shaped at all:



    That's actually not that far off from reality, high speed boats blowover quite a bit like this. The speeds and general behavior and so forth are pretty reasonable. It works better than I thought it would.

    I use the component of the velocity perpendicular to the normal too, but I use it only for skin friction. If it's an airfoil shape you'd have to somehow get information from the opposite side of the wing in order to compute something like a Bernoulli effect. This is a fairly minor effect though, my understanding is that most of the lift from a wing isn't actually from that. The value of adding that might be questionable considering the airflow around the entire body is not really computed. The CPU use would go WAY up too. My simulation is all done on a per triangle basis assuming each triangle is in a free stream which is a pretty big approximation already. I.e., what happens at the front of the boat doesn't affect what happens at the rear anyway. It seems to work pretty well for the boats at least, although I do have to ignore certain triangles in the mesh completely for it to work this accurately.

    I haven't tried any of this with an airfoil shape so don't know how accurately it would work there. A lot of real airfoils are symmetrical anyway. I have a sneaking suspicion that DCS works something like this too, but don't know for sure.
     
  21. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Oh, one important detail if you're considering this for a flight sim: With this approach an airfoil would not stall at some angle of attack. If I were doing a flight simulator I'd probably just use wing data directly so stall effects were included.