Sign in to follow this  

Unity Bare-bones physics tutorial for games?

This topic is 368 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, what's up?

 

I recently discovered both Unity and Duality, and it's got me back into developing games.  I know C# well and my graphic design skills (which are like SNES at best lol) are just right for some old-fashioned 2D awesomeness.  But there's one big problem: physics.  At this point I'm working mainly in Duality, but the problem applies to Unity as well.  I don't know Jack about physics; in college I aced all things language, so learning programming languages was easy - but I s*cked big time at algebra (in fact I had to take a remedial course and almost flunked that).  It was always super-abstract, super-confusing, and super-pointless IMO.  Now 15 years later I'm an experienced programmer - who still doesn't know Jack about physics.  :D

 

And unfortunately, everything I've found on Google seems to be geared toward aliens - its explanations use so much cryptic lingo that it may as well be written in an extraterrestrial dialect.  I'm sorry, but I don't see how "angular damping", "restitution", "linear velocity" and other Jargonian phrases can be translated into how to make a player walk (forget stop), an enemy gun fire player-seeking missile, or an object continue moving at the same speed after the game has been "un-paused".  All of the game-specific scenarios mentioned above are things I've been able to do when building a game from scratch, and I know a few oversimplifications (mass means weight, velocity means speed, etc.), but in Duality I'm finding I'm completely lost.  I'm not trying to build a Tardis, for cryin' out loud!  :D

 

Are there any tutorials available out there that explain this stuff in a way that makes sense to the average Earthling?  Someone asked a similar question on their forum, but it looks like it was pretty much unanswered.  I think it's partly because Duality itself is a fairly new name in the game dev world, and probably doesn't get many second looks since it's UI makes it look like a Unity wanna-be. But it really is a great engine; just the fact that it has physics is pretty sweet.  But I'd do a lot better with X's and Y's lol... unless somehow I can find a way to decrypt this mess lol.

 

Share this post


Link to post
Share on other sites
Check out Randy Gaul's articles, e.g. https://gamedevelopment.tutsplus.com/tutorials/how-to-create-a-custom-2d-physics-engine-the-basics-and-impulse-resolution--gamedev-6331 They're about building a 2D physics engine from scratch but he does go through things in a very step-by-step fashion and is very gentle on terminology, introducing terms as needed and explaining them. And of course you can always ask for clarification.

For the linear algebra, this is a must; game development is essentially impossible without a solid understanding of the basics. You can try articles like http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/ or one of a number of "math for game developers" books (the one I liked appears to no longer be in print, but Amazon has more books on the topic now than I can shake a stick at).

Regarding Duality... I think it's a terrible idea to delve into fringe tools when you're just starting out. A vibrant community with boatloads of tutorials, videos, forums, stackexchange posts, experts, and content is going to do you a lot more good than _any_ possible merits said fringe tool might have.

Share this post


Link to post
Share on other sites

In addition to the good advice Sean gave there, I would say this:

 

I can see where one might go through algebra with that attitude. I probably did the first few times I went through algebra. They teach everyone basic math of adding, multiplying, dividing, and subtracting, then they throw them off the deep end with a completely different concept like algebra. You kind of have to leave everything you knew before behind and start moving into the world of abstract mathematics. It's more of a world about logic than one about memorizing tables of facts. More specifically, it's about using logic to solve problems whereas addition and subtraction are just an extremely small part of that although fundamental to all of it. It's also about learning the higher truths about the universe we live in where mathematics becomes a "language" that allows you discuss the rules of the universe we live in as well as imaginary universes like the one you are creating in your game. This all become more readily apparent the higher you get in this subject where Algebra is just the first step; by the time you get past Calculus everything I'm saying here should be self evident.

 

But the whole point of algebra is to manipulate and twist math formulas into different, and hopefully more useful, shapes. Basic algebra comes up in physics all the time. You may have a formula for force or something, and without algebra you have to memorize say 9 different formulas related to it. But I never did because I know algebra. I could memorize one formula and use algebra to derive the other 8 any time I needed them. Algebra also taught me things such as fractions and division are really the same thing, so that now I do division in my head by turning the problem into a fraction and reducing it. There was some good stuff in algebra. But honestly, I didn't pay that much attention in high school and they made me start over from basic algebra through the trig I took in high school when I got to college. By that time I was ready to learn and got a lot more out of it

 

In general, there's not a lot of algebra beyond basic algebra in game programming. It's probably most useful when studying other things like physics, trig, linear algebra where you're using algebra to rearrange those formulas for various reasons. An extremely good example of how Algebra is used in game programming is in my HLSL series on Phong shading. Shader coding gets very deep into all this math even for relatively simple shaders like the Blinn-Phong shader I'm teaching in that series. But that whole video is to explain why Phong shading works. And in order to do that, I have to walk through the math step by step. Because in the shader you have this very tight little specular function that might as well be written in an alien language, as you say, if you don't understand the meaning behind it. I take an hour on video to explain the meaning behind it and that involves a lot of Algebra to rewrite the same formula in different ways that make it bigger but easier to understand; I'm expanding the formula for understanding using Algebra whereas in the actual code you need to reduce it as tight as possible so that the shader is efficient and runs fast but also makes it nearly impossible to understand what it's doing at a glance if you don't already know this stuff. There's a lot of trig and vector algebra in there too as there always is with shaders.

 

I don't know that studying Linear Algebra is all that useful to 2D game programming depending on what exactly you're calling Linear Algebra. Take my advice here with a bit of a grain of salt because I never took Linear Algebra as a class; I taught myself "Linear Algebra" through game programming where I actually put it to use. And you might say, "I thought you just said you don't need Linear Algebra?" Well, first, I don't do 2D game programming really at all. For the past decade I've done almost exclusively 3D game programming instead. Linear Algebra is more necessary for 3D game programming. But mostly what I mean is that when I look at Linear Algebra text books, the subject is much more broad than what we use in game programming. Like many parts of mathematics in game programming, we use a small subset of what's possible. Vectors might be the exception where I've gone more into vectors with game programming than any mathematics I've seen. I'm not aware of much that has to do with vectors that you do not put to use for game programming. And one might say, "Well, vectors are Linear Algebra." Not really. I learned vectors in trigonometry. They covered the subject fairly thoroughly there although the "last" time I took trig, all the students and the professor himself all had their eyes glazed over when we spent 2 weeks covering vectors. None of them could conceptually grasp vectors whereas I had been programming 3D vectors for awhile at that point because of game programming. At first it's a very difficult concept to grasp, but once you get it it's no more difficult than anything else.

 

So, I would not even really consider vectors a part of Linear Algebra, although they typically are considered a major part of it. To me, they are really part of trig. Linear Algebra seems to me (not having every actually taken the course but only knowing the subject through game programming) Matrix Algebra with other mathematics, like regular algebra and trig thrown in. I've never seen a matrix in game programming that wasn't either 3 by 3, 4 by 4, or in extremely rare cases 2 by 2 or a 1 by n version of those. In Matrix Algebra and presumably Linear Algebra, you can have a matrix of any size such as 43 by 12. So, the subject is much broader than what we would actually use in game programming. And if you take a class, you're probably going to learn things you'll never use in game programming while learning a lot of stuff you will use quite a bit.

 

Anyway, my first suggestion is to learn trig. You'll use the heck out of it in game programming with all the angles, measuring lengths and distances, and sine waves. Not to mention, that's where you should learn vectors. For one thing, vectors and trig are very closely related. And you get an introduction to complex and imaginary numbers which is the beginner version of quaternions.

 

Matrix algebra is a separate subject from Linear Algebra. I was taught it in middle school back before I was ready and forgot most of it having to relearn it just like pretty much everything else I paid no attention to growing up in school.

 

I don't see a lot of scenarios where matrices are that useful for 2D game programming. From what I've seen, they're almost never used in 2D game programming. And matrices are pretty much the only part of Linear Algebra that is not trig. In general, using the rotation formula makes a lot more sense to me in 2D than using a rotation matrix, which is really the same thing. That is unless you're actually working in 3D, which is a very real possibility because DX11 and modern OGL are really working in 3D even when you do 2D games. And since you're actually in 3D working with raw vertices and such, it would make sense to know matrices. Not to mention that because of this even shaders are working in 3D even when you make 2D games and vectors and matrices are an absolute must for working in GLSL or HLSL. Although, if you work with a shader builder program (I think Shader Forge might be the name of one example) you may not really need to understand the math so much as they are typically designed for artists to use.

 

Anyway, about learning physics. Most of the physics stuff I've found on the Internet and in books is largely unusable to me mostly because they write it in Calculus notation and I never took Calculus. However, I took physics in high school and college and never once was required to know a lick of calculus. So, I know for a fact that most of real world physics does not require a knowledge of calculus. It really begs the question why all of these authors feel the need to show off their calculus knowledge rather than teach. I'm a bit prejudiced because I don't know Calculus. If I did, then perhaps I too would want to give directions to the bank to people on the street in Calculus equations. :-)

 

So, that being said, I would recommend learning physics from a college text book such as this one by Giancoli. This is literally how I learned Physics. I read the book cover to cover. There's much in it that at first may not seem to apply to game programming, such as gas law. But one of the projects I want to work on some time in the next year or two is building a steam engine railroad simulator. And steam engines are all about gas law outside of the basic mechanics of how they work. So, it may be a bit more than what you actually need for basic game programming, but I think it's all worth while as a game programmer. That book is so old you can probably find an older edition that you can pick up for almost nothing; I doubt the subject of Physics 101 has changed that much since Issac Newton wrote the Principia, let alone in the last half century. I'm not sure why there have been that many editions of that book. But that book in particular I can highly recommend. It does not cover every subject you might encounter in game programming physics, but thus far, I've found some basic college physics pretty much qualifies you as a world class expert in the world of game programming physics. Physics is always presented as a big deal in game programming, whereas I've found a basic college level understanding of the subject is significantly beyond what is actually used in the vast majority of video games. A bit more knowledge might be required to write the next Kerbel Space Program including knowing the difference between Euclidian and non-Euclidian geometry, but it's rare that video games go this deep into physics simulations.

 

Something else you might find worth your while is the article I wrote and posted on my old website explaining the physics of moving a 2D space ship in 2D space as if the engines and ship were real using real world physics.

 

http://virtuallyprogramming.com/XNATutorials/TwoDTutorials/Acceleration/2DAcceleration.html

 

http://virtuallyprogramming.com/XNATutorials/TwoDTutorials/Acceleration/AccelerationOf2DObjects.html

 

The way that I wrote that, might not make it clear that it's actually more than one page.

 

I think that's a reasonably good intro to game physics albeit maybe not for absolute beginners maybe.

 

Honestly, I have never seen this information taught anywhere and I made it up having never learned it myself. However, I took a real world knowledge of trig and physics and made an educated guess for this and came up with a physics simulation that I think is extremely accurate. (I'm open to anyone correcting me if they find something wrong about it.)

 

It turned out to be one of the more fun things I've done in game programming because the little 2D ship is rather difficult to control combined with the fact that the camera does not move. So, if you are not extremely careful, the ship goes off screen and it's near impossible to get it back on screen at that point. It was just a physics example program and it turned out to almost be a game in and of itself trying to pilot the ship around the screen.

 

And it's very foundational stuff in Physics that will apply to pretty much all motion. Putting it into outer space actually simplifies the motion to make it as easy to learn as possible. Various other forces are applied when you get into moving other things like drag and gravity and such. But I think it's maybe a good place to start getting into game physics.

 

It might also be worth your time watching my vector and matrix videos on my YouTube channel. It's geared towards preparing you for 3D game programming, but the subjects also apply to 2D as well to one degree or another. In 2D the math is just a little easier.

Edited by BBeck

Share this post


Link to post
Share on other sites

I am in a simular position: I am also an experienced programmer (25 years of experience) but suck at math.

Yeah sure i know a lot, like trigonometry, a ton about vectors, ratios and some very basic algebra - but i still not know how to derrive formulas or how to solve any systems. I never learned higher level math at school, my path was just not allowing me to do that. At the very least i can translate some papers to code.

 

But i know a lot about physics now, thanks to the people here which made a lot of awesome papers and tutorials, so go read that stuff, watch the videos - especially from (Randy Gaul, Dirk Gregorius, Erin Catto). Also i can recommend the handmade hero series which teaches making a game from the very beginning in a low level fashion in C - great stuff to learn from, especially the math behind it. [url]https://handmadehero.org/[/url]

 

Oh and i found a really great introduction to algebra:

[url]www.youtube.com/watch?v=xSi0lfl-31U&list=PL7F6C8576EBEDD88F[/url]

 

Last tip: If you still dont know about vectors yet, start by learning how cartesian coordinates works, unit circle + cosine and sine, perpendicular axis, the dot product, linear interpolation. Also which helps is to actually write code to visualize everything. If you understand that much, you can write your own physics system.

Edited by Finalspace

Share this post


Link to post
Share on other sites

Wow, thanks for all the great resources!  I know I've got my work cut out for me if I'm even gonna think about learning all this, but it sounds like I'm closer to the right track than I thought.  I've seen C/C++ pages call a 2-dimensional array a "matrix" (I just figured it was because of the movie, and just cuz it sounds cooler), and I know at one point I've used a C library (and I believe it's part of the standard library) called "vector"; I always figured vector was the official name for "3D point" (though listening to this YouTube video it sounds like they can be used for a lot of other stuff too).  So it sounds like this is the kind of thing that comes with experience; that's how most things are in programming, learn by doing and keep yankin' your hair out until you're a bald genius. :lol:

 

But seriously though, if I were planning to build a game engine from scratch, I wouldn't bother with physics at all and stick to basic shape intersection; IMO that's really all you need for everything short of Angry Birds - then spend that time to improve it in other areas; audio, animation, optimization, portability, a high-level API etc. etc. etc.  Those are the kinds of "problems" I'm used to solving, and just considering that there are still people doing this today kinda makes me wonder if I should consider doing that. :lol: 

 

This brings me to another comment that came up: I was unaware that Duality was a "fringe" framework; sure it looks like a Unity wannabe, but it also looks a lot more manageable for one guy.  I would love to dive into one of these big-shot engines like Unity or Unreal or whatever, but these kinda look like you need a team to create anything decent.  I'd rather create a great 2D game than a crappy 3D game, which is why my interest has been more in simpler engines... but am I way out in left field on that?  This is totally off-topic (my own topic :lol:) and I apologize for that - but what engines would you recommend and why?  I'm asking here because you guys have already put up with enough of my posts to have an idea of what I'm looking for.  I'm down with learning some extra math if necessary (which is why my original question was about that) but I'm definitely not interested in 3D (yet).

 

Anyway, thanks again! :)

Share this post


Link to post
Share on other sites

This topic is 368 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this