Skip to main content
GameDev.net gamedev.net
🔒 Locked

Point-Based Physics Tutorials

Started by brandonpelfrey May 25, 2009 at 6:28 AM 19 replies 9.4k views
Original Post
brandonpelfrey
brandonpelfrey
Hello all. UPDATES: The First Tutorial: Basic Physics Stuff The Second Tutorial: Constraints and Springs The Third Tutorial: Fluids in Games I have recently begun a series of tutorials on point-based physics programming for beginners, writing together with Grant Kot (kotsoft on youtube/gmail). We are in general trying to give good explanations to the physics underlying things and to give very easy-to-read code as examples. Our first tutorial is out, covering very simple concepts in forces. (Its implementation is shared with the same great technique as Thomas Jakobsen's paper and talk on "Advanced Character Physics" which I am sure many of you know) The second tutorial, in writing at the moment, is a introduction to the concept of spring forces and iterative constraint solving, their implementation in Verlet, and will build a small room with stacking circles (Or spheres as a very simple extension). The third tutorial will cover one of my favorite topics: fluid simulation using SPH. We will cover the equation splitting concepts, ideas behind Navier-Stokes, and hopefully nail down a great explanation to the implementation. I hope this isn't seen as some ridiculous advertisement. We really want to create a series geared toward developers of real-time applications like games, so I thought I would try to give back to this great community from whom I have learned much. I hope you enjoy! This is the first tutorial. Any critiques are welcome. The First tutorial [Edited by - brandonpelfrey on June 7, 2009 11:26:05 AM]
Morrandir
Morrandir
Neat tutorial, good introduction to the topic and I liked the hands-on approach, definitely better than all theory, or all code with some comments thrown in.

Maybe a short discussion on calculating proper timesteps wouldn't have hurt, since that's the only thing missing from it for someone to be able to incorporate some basic physics simulation into their own project based on the tutorial. But I guess there's plenty of material available on that topic already.

This one didn't contain too much news for me personally, but I'm looking forward to the fluid simulation tutorial, was planning on learning about that eventually, and a tutorial will give me a nice jumpstart :)

Keep up the good work!
h4tt3n
h4tt3n
This sounds very promising! Will read the tutorial tonight...
Sneftel
Sneftel
Wow! For once, a tutorial written by someone who knows what they're talking about. Might be a little densely mathy for beginners (in particular, assuming knowledge of the gradient operator), but that's a minor point. Great work, keep it up!
brandonpelfrey
brandonpelfrey
The second tutorial is now complete! We go over the concepts of springs and constraints with projection, weighting by mass, and give a little hint to mesh-based deformation that will be discussed in the fourth tutorial. I hope you enjoy this! The next (third) tutorial will be on fluid motion via Smoothed Particle Hydrodynamics.

http://blog.brandonpelfrey.com/?p=242

A small video of the product of this tutorial is available at

h4tt3n
h4tt3n
On tutorial 101 I have a few ideas for improvement...

(1) In the numerical integration introduction it says:

"A system’s “state” is a collection of information that describes all of the positions and other variables that you would need in order to find the positions and others in the next frame."

I think you should point out that all we nedd to know about a particle to calculate the next frame are its state vectors, ie. the position and velocity vectors. I mean what "other variables" would there be? :-)

(2) I really think you should edit out the "nerdy explanation on what forces are". It doesn't really add anything to the tutorial, and it will only confuse the math/physics novice, which you are really trying to help.

(3) When writing physics tutorials for programmers it's often a good idea to put small code samles into the text every time you've introduced a new concept, just to show what all the math thats been babbled on about actually looks like when implemented in a program. This way the physics novice programmer will say "ah ok, thats what he means!" and move on in the text instead of getting confused.

Cheers,
Mike
brandonpelfrey
brandonpelfrey
Quote:
Original post by h4tt3n
On tutorial 101 I have a few ideas for improvement...

(1) In the numerical integration introduction it says:

"A system’s “state” is a collection of information that describes all of the positions and other variables that you would need in order to find the positions and others in the next frame."

I think you should point out that all we nedd to know about a particle to calculate the next frame are its state vectors, ie. the position and velocity vectors. I mean what "other variables" would there be? :-)

(2) I really think you should edit out the "nerdy explanation on what forces are". It doesn't really add anything to the tutorial, and it will only confuse the math/physics novice, which you are really trying to help.

(3) When writing physics tutorials for programmers it's often a good idea to put small code samles into the text every time you've introduced a new concept, just to show what all the math thats been babbled on about actually looks like when implemented in a program. This way the physics novice programmer will say "ah ok, thats what he means!" and move on in the text instead of getting confused.

Cheers,
Mike


Thanks Mike for the input =]
In the simple point physics stuff right now, you're absolutely spot on: we do only need position and velocity with our 2nd order integrator. Later on we will need more information to define state, especially when it spreads onto multiple grids at different resolutions and when we need the stress-strain information of the current frame to compute the next. But you're right. For now, that's all we need and maybe it is a little bit confusing.

The nerdy explanation stuff has to stay. One of the reasons I had to write these things is the fact that no other tutorials gave any basis for why things worked, even if you wanted to know. The reasons tend to be complicated, but they're there if you want to know just in case ;] Also, if people really want to make something new, they need to know at least a little dab of the theory so that they can correctly branch into something new. Just a personal thought, really.

On the subject of code, I totally agree. I'm looking for a good plugin right now to handle the syntax and all while not being a kludge of outdated &#106avascripts and other do-dads. <br><br>I hope you check out the fluid tutorial coming soon. It's the reason I really started all of this. These first two tutorials are more or less a little bit of fundamental information before we delve into the (admittedly complicated) topic of fluid simulation for games.<br><br>Thanks again for the response and critiques!
h4tt3n
h4tt3n
Glad you liked the input. You bet I'll read the sph tutorial - been looking for a tutorial on that topic for some time now!

Cheers,
Mike
tori
tori
Looks pretty wicked.

As for the editing out of the "nerdy stuff", here's an alternative - just make the text grey-ish, so that it's obvious that it's non-essential to a copy paster :)
brandonpelfrey
brandonpelfrey
Quote:
Original post by tori
Looks pretty wicked.

As for the editing out of the "nerdy stuff", here's an alternative - just make the text grey-ish, so that it's obvious that it's non-essential to a copy paster :)


If I could make it one of those expandable accordion things... How about that? It would just say "Nerdy Explanation" and you can click to get the lowdown.
Stani R
Stani R
Looks interesting, unlike many tutorials out there. Must've been a real pain getting the figures formatted nicely.

Minor nitpicks I have for article 101:

g is used for gravity, G is usually reserved for the universal constant of gravitation, which is something completely different. Also units, m/s², not m³/s.

The explanation about "exploding" is a bit vague and could use more detail.

velocity’s “affect” -> "effect".

more deep -> more in-depth?
brandonpelfrey
brandonpelfrey
Quote:
Original post by lightbringer
Looks interesting, unlike many tutorials out there. Must've been a real pain getting the figures formatted nicely.

Minor nitpicks I have for article 101:

g is used for gravity, G is usually reserved for the universal constant of gravitation, which is something completely different. Also units, m/s², not m³/s.

The explanation about "exploding" is a bit vague and could use more detail.

velocity’s “affect” -> "effect".

more deep -> more in-depth?


Fixed the grammatical mistakes. The explanation on exploding is coming in a later secondary article where I'm going to go into a little more in depth on integration as a whole topic. Also, the equations aren't too bad if you know LaTeX. Thanks!
brandonpelfrey
brandonpelfrey
The third tutorial in the series is up. This time, I'm actually bringing something semi-new. Fluids. Learn to use SPH to add fluids to your games using a (relatively) simple algorithm along with some of the related math behind it. Sample code is provided that will produce the video on the page. There isn't a guide or tutorial on this that I'm aware of, so it might be kind of unique!

As always, critiques and comments are welcome.

http://blog.brandonpelfrey.com/?p=303
h4tt3n
h4tt3n
Looking forward to read the tutorial, thanks a ton for doing this!

after having a quick glance at the code sample, here's a small but important idea for speed gain...

In the DENSITY part, instead of doing:

Quote:

for(int i=0; i < particles.size(); ++i)
{
(...)
for(int j=0; j < particles.size(); ++j)
{
if(j >= i) continue;
(do particle stuff here)
}
}

It is more optimized to do like this, since you'll get rid of all useless iterations and the number-of-particles-squared if-then statements in the inner loop:
Quote:

for(int i=0; i < particles.size()-1; ++i)
{
(...)
for(int j=i+1; j < particles.size(); ++j)
{
(do particle stuff here)
}
}


Cheers,
Mike
brandonpelfrey
brandonpelfrey
Quote:
Original post by h4tt3n
Looking forward to read the tutorial, thanks a ton for doing this!

after having a quick glance at the code sample, here's a small but important idea for speed gain...

In the DENSITY part, instead of doing:

Quote:

for(int i=0; i < particles.size(); ++i)
{
(...)
for(int j=0; j < particles.size(); ++j)
{
if(j >= i) continue;
(do particle stuff here)
}
}

It is more optimized to do like this, since you'll get rid of all useless iterations and the number-of-particles-squared if-then statements in the inner loop:
Quote:

for(int i=0; i < particles.size()-1; ++i)
{
(...)
for(int j=i+1; j < particles.size(); ++j)
{
(do particle stuff here)
}
}


Cheers,
Mike


Kind of embarrassing. Thanks a lot. I'll change it shortly. =] Thanks for the interest!
h4tt3n
h4tt3n
Hi again,

I had some trouble making your 103 code sample work until I added

Quote:

glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);


to the main function. Looking good btw.

Is the tutorial vid based on the exact same source as is posted after the tutorial? the liquid in the vid seem s more sticky... ?

Cheers,
Mike
h4tt3n
h4tt3n
I took the liberty to improve your code a bit. It's commented in the source. Download full code::blocks project here:

http://www.jernmager.dk/stuff/cpp/physics_103.zip

cheers,
Mike
NickGravelyn
NickGravelyn
Just thought you should know that you forgot to add your 101 post to your Physics Tutorial category.

Looks good, though. I'm planning on utilizing these for some fun little demos in XNA in the next week. I'm especially excited to play with the water. Combine those physics with some blob rendering to make it look more watery and that could look really nice.
brandonpelfrey
brandonpelfrey
Quote:
Original post by h4tt3n
I took the liberty to improve your code a bit. It's commented in the source. Download full code::blocks project here:

http://www.jernmager.dk/stuff/cpp/physics_103.zip

cheers,
Mike


Looks really nice. I had forgotten to put the sqrt op in there and I had never really thought about the x-y radius check before. Thanks for interest in all of this. I think will be a little more interesd if someone (probably end up being me) ports it to flash so it can be in all kinds of crazy online stuff... That's what comments have said anyways. I may do that soon.

I'm actually implementing a three dimensional version at the moment so that I can continue a certain academic direction and hopefully publish a paper before I finish my undergrad.

Thanks again!
brandonpelfrey
brandonpelfrey
Quote:
Original post by NickGravelyn
Just thought you should know that you forgot to add your 101 post to your Physics Tutorial category.

Looks good, though. I'm planning on utilizing these for some fun little demos in XNA in the next week. I'm especially excited to play with the water. Combine those physics with some blob rendering to make it look more watery and that could look really nice.


That sounds like an awesome idea. If you could leave a one line comment or something leading back to my blog, it would be really appreciated. I will be providing more material very soon. Thanks for the support!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.