Our Properties: Gamasutra GameCareerGuide IndieGames Indie Royale GDC IGF Game Developer Magazine GAO
My Message close
Latest News
spacer View All spacer
 
February 10, 2012
 
What drives the developers of Unity?
 
Analyst questions validity of unusual January NPD results [17]
 
Skyrim wins big at 15th Annual Interactive Achievement Awards
spacer
Latest Features
spacer View All spacer
 
February 10, 2012
 
arrow Virtual Goods - An Excerpt from Social Game Design: Monetization Methods and Mechanics
 
arrow Principles of an Indie Game Bottom Feeder [21]
 
arrow Postmortem: CyberConnect 2's Solatorobo: Red the Hunter [1]
spacer
Latest Jobs
spacer View All     Post a Job     RSS spacer
 
February 10, 2012
 
Capcom Game Studio Vancouver, Inc
Producers & Designers Wanted
 
Rockstar San Diego
Tools Programmer
 
Rockstar San Diego
Gameplay Programmer
 
EEDAR
Business Analyst
 
Irrational Games
Systems Designer
 
CCP - North America
Sr VFX Artist
spacer
Blogs

  Building An Analytical Physics Engine - Pt.1
by Stuart Evans on 11/05/09 10:13:00 pm   Featured Blogs
6 comments Share on Twitter Share on Facebook RSS
 
 
  Posted 11/05/09 10:13:00 pm
 

Hello Gamasutra, this is my first post and I'm going to talk about a different approach I've taken to making a physics engine. I've worked in isolation so far and I'm very interested to see the reaction of programmers/developers with experience of physics engines, even if their reaction is to recoil in horror at my ill informed approach, all comments are demanded!

Analytical Physics Engine.

Recently I've been investigating an alternative approach to making a physics engine. I'm no expert on physics engines so my approach to the problem wasn't initially influenced by the more traditional approach. The project is taking shape now and i would like to throw the idea out to the community to get some feedback on it. In future blogs I want to talk about the strengths and limitations of the analytical approach to physics engines and finally compare the applications of a traditional physics engine to my analytical one.

In this first blog post I'll try to outline how the analytical approach is taken and why I took it. I wont comment on the limitations or advantages too much yet, I'll leave that up to debate till later posts.

Analytical vs Numerical methods:

When I use the term analytical methods I'm referring to the method used to resolve a mathematical or algebraic problem. The alternative approach to the analytical method is the numerical method. A good problem to look at (and a large problem in the physics engine too) that demonstrates the differences between these two approaches is root finding. Say you have an equation ax^2 + bx + c = 0, and you want to find the roots (find the possible answers for x) you can use the well known general quadratic equation which would be the analytical approach or you could make a guess as to what the answer was, put this back into the equation, adjust your guess based on the result of the previous one, repeat until you have a satisfactory answer. This simple problem highlights the difference between the two methods, the analytical approach attempts to find an exact answer in terms of the coefficients (a, b and c in this case) where as the numerical method uses an iterative approach to slowly increase the accuracy of an answer until satisfactory. In this case I doubt there would be many people would would take the numerical approach because of the relative simplicity of the general equation but if you were to look at ax^4 + bx^3 + cx^2 + dx + c = 0 you may turn to the numerical method as the general solution for quartic equations is an order of magnitude more complicated and if you start to include terms of x^5 and higher you will be forced to take a numerical approach as no general equation exists (to my knowledge anyway, please prove me wrong here as I could find a use for such an equation).

Different Approaches:

So how does all this relate to a physics engine? A large issue in physics engines is collision detection, detecting the point at which two objects collide. The way this is generally done is to use a method that tests for overlapping objects and run this test on objects every time they are moved by a small amount. This is in essence a numerical approach to finding out if the two objects will collide. An analytical approach would be to evaluate a single equation to test if the objects collide without the need to move the objects. The equation in question would be the equation of the distance between the objects over time. For a linear path with a constant velocity this equation is reasonably simple, in the case of two circular objects it takes the form of a quadratic equation which we know how to solve easily. The result of the equation should be the time that the objects will first first start to overlap and the time that they finish overlapping (or no results if they never overlap).

At this point I would like to point out a limitation of the numerical physics engine which was one of the reasons I ventured down this analytical avenue, If two objects are far apart and will never collide the numerical physics engine wastes much time checking and rechecking (I understand that a broad-phase check will speed this up but you will still be doing many broad-phase checks) to see if the will collide every time they are moved. In the analytical approach we evaluate a single equation and from that point on we know they will never collide.

This would be a good point to talk about the differences in architecture too. A numerical physics engine will generally store the state of all objects at a specific point in time and iterate forwards through time. The analytical physics engine stores the state of objects over time, i.e. it will store the path of an object (some might point out here that the state of an object in the numerical physics engine is equivalent to the path of the object over time as you store velocity acceleration etc, however the analytical physics engine also stores the start and end point of this path and any subsequent or previous paths the object has taken).

Promising Properties:

There's one particular property I was trying to exploit with the analytical approach. In a physics engine there may be times of heavy and light load, i.e. times when not much is happening and times when a lot of things are happening. What I wanted to do was to let the physics engine get ahead when there was a light load in order to have an extra buffer of time to cope with heavy load. This means the physics engine runs ahead of viewing time and is powerful optimisation which frees the engine up to handle spikes of very heavy loads without any perceived slow down.

An astute observer will point out to me at this time that to do so would be a waste of time as the very second that a users input modifies the state of an object all of the computation made about the future state of objects in the engine would be invalidated. This is "partially" correct, the crucial term here being "partially". When the user makes some dynamic input to the engine it is theoretically possible to separate out exactly what has been invalidated and what is still valid and then only re-evaluate the invalid results.

Being able to achieve this gives us the first major advantage of the analytical engine, the ability to balance load in a scalable, general way throughout the entire engine. This advantage is mitigated very slightly in the case of dynamic input, it can't work out ahead what the user is going to do so if the user does something which instantly causes a heavy load on the engine it can't balance that load, but if the user does something with causes a heavy load to occur a second or two later (like the time it takes for a gun to charge or a rocket to reach its target) we can balance out the heavy load by working it out ahead of time.

Bad Omens:

I'll also now touch on a big limitation of the analytical approach, architectural and algorithmical complexity. It was tediously difficult to make this work in an effective way, I've rebuild the entire engine from scratch about three times so far. After each rebuild there has been an agonising period of fixing horrendously fiddly bugs. At some point I found myself implementing a general solution to quartic equations and in turn cubic equations for which I've not found any scripted examples online and require some maths knowledge to understand, this is not the last of the sparsely documented maths that is required. As most people take the numerical approach there is far less in the way of online help for the types of problems and equations I needed to solve. The difficulties I've faced are significant enough to list as a limitation of the analytical physics engine, while not limiting performance this is a significant development overhead.

I'm going to stop talking about the innards of the engine here and in my next post hopefully move onto more of the limitations, advantages and applications. Some programmers may notice that there are still technical questions to be answered but I don't want this article to be too technical. If there's any indication from comments that there are specific technical questions people want clarification on I'll try to cover them.

 
 
Comments

Dirk Broenink
profile image
Very interesting and great work!
I happen to know a little about this now, as I just finished the Physics course at my school (International Game Architecture and Design).
What you are describing is also known as the integrated method of calculating when two objects collide. This is very usefull when you want to know exactly when an object touches for example the ground, so it doesn't penetrate before finding out it collided. For example you then know it will collide in exactly 64.753 frames. Instead of 65 frames which will be your answer with the differentiated (numerical) method.
The problem is indeed complex math. And in some cases you are forced to use the numerical method, when there are too much variables, you cannot solve it mathematically anymore. A good combination of the two is then required.

Interesting work and good luck, keep us posted!

Steven Walker
profile image
Hi Stuart. Just a quick note to say keep up the good work.

Game physics is such a new art that there is a lot of room for new approaches. I work in the area and I have often considered that the fundamentals of splitting time down into slices is really a fudge, it works quite well for us but we really only do it as we don't have the processing power and understanding to solve the problem as a continuous one.

I suspect in a few console/processor generations we may be using something quite different.

A book you may find interesting is this one:

Collision detection in interactive 3D environments By Gino Johannes Apolonia van den Bergen

This deals with swept collision of any shaped objects (using GJK). In his solution the objects only move in a straight line - and I'm guessing yours have to move in curves with gravity? But it may be of some interest.

I am sure the things you are trying to solve will prove very diffcult - especially when you have lots of objects involved in lots of collisions (and therefore changing direction a lot) - like a pile of objects. But I am interested to know what progress you make.

Good Luck!



Stuart Evans
profile image
Thanks for your positive comments I'm glad this is of interest to people.

So far I've managed to solve the equations related to curved/quadratic paths. The next component I plan to add to the paths is rotation,unfortunately there is no way to solve the equations for rotating paths analytically, it is known to be mathematically impossible to analytically resolve the roots of such equations. Instead I plan to use the area cast by the rotating shape as a broad-phase test for collision, once i have the time period through which these ratation areas overlap I can apply a numerical method to iterate through that small time period and find exact collision points. Hopefully this will massively reduce the number of iterations required to find collisions in these cases.

The pile of objects scenario is a big weakness of the analytical solution, when the time between collisions begins to tend towards zero you end up with a massive performance bottleneck. This would be the extreme version of the heavy load scenario mentioned in my article. While the load balancing can help cope with this for most situations there are some situations where objects are in constant contact and the time between collisions becomes zero. I have a solution I think will solve this problem but I'm still working out the kinks. I'll do a post about this once I've determined if it will work.

At the moment it's become obvious that there are some things the engine does very well and some things it does not do so well and this will be the topic of the next post.

Ron Newcomb
profile image
Hi Stuart,

My first question is what kind of game/application is this being used in? I see we have a lot of objects on fixed (if curvy) paths, but since they don't chase or evade the player's object, those objects obviously aren't creatures attacking or evading the player. Since they presumably move slow enough for the player to watch, they don't seem to be bullets. (Slow-mo plasma balls, maybe.) So I'm guessing we have some sort of sim with minimal player interaction. Pool/billiards, pinball, Peggle, maybe a traffic sim from The Fifth Element. Obviously any object whose motion is driven by formulae isn't terribly interactive, which also makes me wonder why you need the analytical solutions at runtime. If those things are moving platforms, say, you could pre-compute when their collisions happen at compile-time.

Anyway, setting aside the intended use of the engine: I don't think there are general solutions for higher-order polynomials. There's a quadratic equation, but there isn't one for cubic equations, etc. I believe that general solutions of higher polynomials was the very definition of NP-hard or something. Like, it's on Mathematics' "10 Most Wanted" list. This is *why* numerical methods are so prevalent in programming.

Also, is your code going so far as to generate then solve integrals on the fly?! (If so, even in limited form, I tip my hat to you.) I once investigated precise collision of curved surfaces. Integrals bit me. Hard.

I think your project is wonderful, but if it's main draw is load-balancing, well, I hope you're getting paid to break your brain on what amounts to plain ol' optimization. :) I can see the "randomness" of player input being mitigated somewhat by physics limitations. (I.e., if the player is flying a hovercar, things like momentum and thrust and cornering ability constrain how soon he'd be able to perturb an object that's already behind him.)

On the subject, *are* there other benefits to the analytical approach?



Ron Newcomb
profile image
Also, I am interested in this because, in the Fencing game I prototyped, it was analytical solutions I wanted to figure out where & when a touché was coming. The AI needed the info.

Stuart Evans
profile image
Thanks for your interest Ron and good questions,

The game I'm building has a pretty abstract outline that's largely based around what works well with the engine but is mainly concerned with shapes moving under Newtonian principles. Interaction is through static or continuous impulses applied to objects. This is how the user interaction is performed. Objects could be controlled in similar manner by an AI or the AI could just pick appropriate paths (that conform to the available equations) in whatever manner it preferred. Objects moving faster that the player can see are more easily modelled in the analytical approach as they can never jump through other objects.

"Obviously any object whose motion is driven by formulae isn't terribly interactive" I don't totally agree with this as any path is essentially a formula (or set of formulae). A traditional physics engine uses a very simple formula but changes it's parameters at regular itterations. The formuale of paths in my engine can change too but they don't have a fixed itteration length. In this manner objects can move just as in any other physics engine just. However the performance costs are different between the engines, the tradtional approach gives a much more consistant performance which the analytical approach varies with entropy/rate of change.

I've found a implemented general solutions for quadratic, cubic and quartic equations, my understanding is that none exist for the higher orders as mentioned in my post.
( http://mathworld.wolfram.com/CubicFormula.html )
( http://mathworld.wolfram.com/QuarticEquation.html )

"Also, is your code going so far as to generate then solve integrals on the fly"
I've restricted myself to a small set of shapes and nothing more complex than quadratic paths so I guess in a very limited way yes.

The load balancing feature was a large reason I started to work on this but other benefits have emerged as the project progressed. One of these is prediction, much like in your touché problem. This is something I'm building into the gameplay. I want to do a full post at some point on the other benefits so I'll stop short of talking about that now.


none
 
Comment:
 




 
UBM Techweb
Game Network
Game Developers Conference | GDC Europe | GDC Online | GDC China | Gamasutra | Game Developer Magazine | Game Advertising Online
Game Career Guide | Independent Games Festival | Indie Royale | IndieGames

Other UBM TechWeb Networks
Business Technology | Business Technology Events | Telecommunications & Communications Providers

Privacy Policy | Terms of Service | Contact Us | Copyright © UBM TechWeb, All Rights Reserved.