Author Topic: Outsourcing physics  (Read 4151 times)

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
Outsourcing physics
« on: November 02, 2006, 07:29:05 PM »
I'm redesigning the C++ source from the top down (the broader strokes of the code architecture), and it's pretty easy to "pop" the physics entity from a bot, shot, tie, and shape.  What I mean is it's possible to encapsulate the physics entirely from the rest of the code.

This means it's really not all that hard to move to a 3rd party physics library.  Since this isn't a commercial title, alot of possibilities are open to us:
  • PhysX (formerly novodex) is my initial pick.  It's a commercial library recently released for free to non commercial projects since they're trying to hype their new Physics card (the so called PPU).

    Which means if we used this library, we have the likely chance of actual physics hardware acceleration in the not so distant future, depending on who ends up winning the API war
  • NewtonDynamics would be the other choice in my mind.  It's also free.  For me personally this might be better since I could learn an API that could be used in a potential indie commercial game without paying licensing fees.
I'll probably pick one as I come to understand what the different APIs are like, and other factors.  They're both probably doing more than we really need anyway

Why move to a 3rd party physics solution?  Isn't it overkill?:
  • Physics is hard to get right.  It's a full time job getting any sort of physics simulation working right.  While the current physics engine in the C++ source is very pleasing (to me atleast), it took alot of time to get that way.  It's still not perfect, and it's possible a more dedicated library could do what I got DB to do but better and faster.
  • Expandibility - Really, since these physics engines are used for things a little more complex than spheres rolling around, we should be able to add some new things we think up without any problem.  Physics no longer becomes a conceptual bottleneck.
  • Programmer time - More programming time being spent programming ALIfe things instead of tweaking physics constants and algorithms.
I'd abstract the interface for the physics engine so you could swap in or out the current physics system for comparison of aesthetics and speed.  Should be fairly trivial.

What are people's thoughts?

Offline EricL

  • Administrator
  • Bot God
  • *****
  • Posts: 2266
    • View Profile
Outsourcing physics
« Reply #1 on: November 02, 2006, 10:25:59 PM »
This would be totally cool.  I probably spend half my time on physics stuff today - more effecient collision routines, etc.  I agonize over whether to add physcis code here or there, whether the perf cost would be worth it.  Wouldn't it be cool if ties were visable, were solid, could be shot, would hit a shape or prevent a multibot from going down a narrow passage, providing selection pressure towards smaller tie lengths....  All of these and each of these is new physics.  It's all circle and rectangle and point and capsule intersection code, something I feel a lot more confident writing today then I did 6 months ago  - something that would be quite ameanable to hardware acceleration I imagine - but its code I would love not to have to write....

One of the fundemental hurdles in ALife is that things don't interact unless programmed to interact.  Things don't collide, bounce, drag, appear visible, exhert force, torque, etc. unless someone writes the code to allow those interactions.  This is completly opposite of the way the physical world works.  The physical world is messy.  You have to go to tremendous lengths to prevent things from interacting in the physcial world and it's this messiness, this plethora of interactions that is critical and necessary or at least as necessary as other factors for biological evolution to occur.

So, the more interactions we can allow, the richer our virtual world will be and the more varied and complex the resulting organisms will be.  I'm a total fan of this.
Many beers....

Offline Testlund

  • Bot God
  • *****
  • Posts: 1574
    • View Profile
Outsourcing physics
« Reply #2 on: November 03, 2006, 01:34:53 AM »
So there's a physic accelerator card? I had no idea. If it whould improve performance for these kind of programs considerably it whould be worth buying one. I'm just wondering if it whould be difficult to come by. I imagine there wont be much demand for such card. Otherwise I'm all for improvements so I say go for it.  
The internet is corrupt and controlled by criminally minded people.

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
Outsourcing physics
« Reply #3 on: November 03, 2006, 01:43:16 AM »
At present the cards are about $300 and the common consensus is that there just aren't any games out yet that really need the horsepower of a dedicated physics card.

I think the company has a sort of "if you build it, they will come" mentality.

Physics can also be done on graphics cards since the advent of programmable vertex and fragment shaders (if that sounds greek to you, just nod and smile ).  At present there's a discussion over wether you need a dedicated physics card or if you're better off using the graphics card (or multiple graphics cards).

Don't know if any of this really effects Darwinbots at the moment.  Our physics needs are orders of magnitude less complex than AAA games.

Offline EricL

  • Administrator
  • Bot God
  • *****
  • Posts: 2266
    • View Profile
Outsourcing physics
« Reply #4 on: November 03, 2006, 11:48:47 AM »
Quote from: Numsgil
Our physics needs are orders of magnitude less complex than AAA games.

But orders of magnitude more parallel.  Graphics cards are optimized for drawning lots of polygons and doing projections but not necessarily for detecting collisions.  The needs of DB are different than most games.
Many beers....

Offline Carlo

  • Bot Destroyer
  • ***
  • Posts: 122
    • View Profile
Outsourcing physics
« Reply #5 on: November 04, 2006, 11:49:37 AM »
Hello everybody. Seems a great idea. My only doubt is that - as EricL suggested- common physics engines may be designed for handling with great accuracy the interactions of few, complex objects in a 3d space, while in DB the need is to make thousands of simple objects interact in a 2d space. Sound like the opposite, though I don't know if this is a problem. If it's not, I surely vote yes.

Offline EricL

  • Administrator
  • Bot God
  • *****
  • Posts: 2266
    • View Profile
Outsourcing physics
« Reply #6 on: November 04, 2006, 01:28:09 PM »
Nice to see you Carlo.  I've heard so much...  

Anything that could offload even the low level vector arithmetic could make a huge difference.  Both VectorSub and VectorScalar are at the top of the list when I profile.  They each get called several million (yes, several million) times per cycle in a sim with 1000 bots.  Even a small improvement in the perf of these routines would have a large impact.

Higher level APIs that could do say, ray-capsule intersection would be huge and might be more ameanable to hardware acceleration as the routines are larger and more complex.  That is, hardware can have a larger impact when offloading larger, complex routines.  VectoSub is such a simple routine that probalby half the time is taken just by loading and poping the stack.  

Does VB6 have macros?  I'll check.

Edit:  No, no inline statements or marcos in VB6.  Too bad.
« Last Edit: November 04, 2006, 01:51:29 PM by EricL »
Many beers....

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
Outsourcing physics
« Reply #7 on: November 04, 2006, 03:21:18 PM »
I've seen alot of stress tests for physics engines that have hundreds of boxes or spheres running in real time.  They're definately built with that sort of massive numbers game in mind.

I won't know if they do it better than our own home grown engine until I compare run times.

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
Outsourcing physics
« Reply #8 on: November 05, 2006, 01:44:58 AM »
ODE is my current running favorite.  It's specifically built with our sort of physics in mind (Virtual creatures in virtual reality).  It's feature set is a very close match to exactly what our feature set needs to be.  Especially important is the stability and robustness of the simulation being edited in real time.

Better yet, it has both a OO and a pure C procedural interface.  Meaning it has the potential to be used in both the VB and C++ code without making compromises in either.

Lastly, it's open source, which is nice because we can edit it if the need arises for some reason, and I like to support open sourced projects when I can.

Really, it's the physics engine I'd make if I made a physics engine, I think.
« Last Edit: November 05, 2006, 01:47:10 AM by Numsgil »

Offline EricL

  • Administrator
  • Bot God
  • *****
  • Posts: 2266
    • View Profile
Outsourcing physics
« Reply #9 on: November 05, 2006, 11:35:12 AM »
The only probelm with using Open Source is that it makes it impossible to do anything commercial with the code.   The open source agreement, which we would implicitly agree to if we used any Open Source code in DB, is viral.  It states that whatever you use it in itself becomes Open Source.  Perhaps not a problem with DB although Carlo and others have copyrights in the code that would essentially be invalidated.  But if you ever want to do anything commercial, don't ever use anything Open Source, not even tools.  Your code won't be yours.  You will lose any copyright or patent lawsuit you get into and you won't be aquireable since you have no exclusivivity to your asset.  Beleive me, I know.

DB is open source in that the source code is available and people can use it non-commercially, but it's not Open Source in that there is no viral Open Source license agreement.  We could still do something commercial with it if we wanted.  That would change the moment we used any Open Source in the DB.
Many beers....

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
Outsourcing physics
« Reply #10 on: November 05, 2006, 01:39:11 PM »
That's true of the GPL agreement, but there's also a LGPL agreement that's used for libraries.

Basically the LGPL means that you can compile your code such that the library is a DLL that you dynamically link against and you don't have to make your code open source.  However if you change the library code in a non trivial way you're supposed to allow the source to be viewed somewhere.  The LGPL allows commercial closed source projects to be built with open source solutions.  Ogre uses the same licensing (and the BSD below I believe).

ODE also provides the even less restrictive BSD license which allows you to use the source code free of charge in your commercial products, pretty much however you want provided you acknowledge that you're using the library and provide a copy of the license.

You can see the licenses here.
« Last Edit: November 05, 2006, 01:42:09 PM by Numsgil »

Offline EricL

  • Administrator
  • Bot God
  • *****
  • Posts: 2266
    • View Profile
Outsourcing physics
« Reply #11 on: November 05, 2006, 04:13:52 PM »
ODE sounds like a fine choice then.  My comments were general in nature.  I've been burned by the GPL before...
Many beers....

Offline Numsgil

  • Administrator
  • Bot God
  • *****
  • Posts: 7742
    • View Profile
Outsourcing physics
« Reply #12 on: November 05, 2006, 06:03:32 PM »
Yeah, GPL can be very bad for softare developers trying to make a living.