Game Development Community

ODEScript Physics

by Gary "ChunkyKs" Briggs · 12/08/2006 (10:29 am) · 106 comments

Download Code File

ODEScript
The Torque C++ modification

This code is an implementation of the ODE API, making it available to torquescript.

As a whole, the API is a "t" [for Torque] prefix on the normal ODE objects, and converted to think in terms of torque and objects rather than functions;

In C:
dBodyID b = dBodyCreate(world);
dBodySetLinearVel(b, x, y, z);

In TorqueScript:
datablock tdBodyData(bodydb) { foo="bar"; }; // foo="bar" is ignored
$b = new tdBody() { worldsim = $odeworld; datablock = bodydb; } ;
$b.SetLinearVel(x,y,z); // C Style
OR
$b.SetLinearVel("x y z"); // TorqueScript Style
Note that both calling styles are supported.


Build Instructions:

1) Edit shapeBase.h, and find this [around like 70 or so]:
friend class ShapeBase;
friend class Vehicle;
and add:
friend class ODEShape;
2) Add all the .cc/.h files in odescript/ to your build, and remember to link with ODE.
3) If you want projectiles to collide with ODEShapes, you'll need to edit game/projectile.cc and add "StaticShapeObjectType |" to your Projectile::csmStaticCollisionMask.

For more detailed instructions for each platform, check buildinstructions.lyx in odescript/.


Thanks to Ray "Noolness" Gebhardt for all his Torque help, including specifically, writing ExampleGameBase and ExampleCollider, from which all my objects derive.


ODERocks
The Torquescript demo of ODEScript

This is a mod for TGE, that should be run with starter.fps or similar, once you have ODEScript [the c++ source modification] built into your Torque install. Run the game with "-mod oderocks" on the command line

Once you're in-game, hold down the C or V button to see a small UI of physics buttons.

This isn't really anything exciting, it just serves to demo some of the objects in ODEScript, and has been used extensively by me during development


Here's a few videos of what it looks like in action:
First Demo [basic chain, single bodies]: youtube.com/watch?v=cA-ncyU51G8
Second Demo ["ragdolls", buoyancy]: youtube.com/watch?v=gjEeN5er6aM
Third Demo [Juggling, Inverse Kinematics]: www.youtube.com/watch?v=PEe55nibKHA

Chunky (-;

icculus.org/~chunky/
#41
04/04/2007 (3:13 pm)
You probably needed to make clean. Is there anything that appears in the console when you click the buttons? And are you looking in the same direction as the video I posted much much earlier in the comments? The objects will be appearing in the same place if they're working.

Gary (-;
#42
04/05/2007 (12:52 pm)
in the console:
oderocks/server/odescript/oderock.cs (14): unable to instate non-conobject class ODEShape
oderocks/server/odescript/oderock.cs (18): unable to instate non-conobject class tdMass
oderocks/server/odescript/oderock.cs (24): unable to instate non-conobject class tdBody

was i supposed to include the binary or the source? cause i tried the source and included the files that were called in ode.h which i'm guessing was very wrong.
#43
04/05/2007 (2:29 pm)
Oh. It sounds like you haven't built ODESript into the engine. ODERocks is a torquescript mod that shows off ODEScript.

First, copy odescript into your engine/ dir.

Open XCode, rightclick in the left hand pane on torque_xcode_2_2_UB, and click Add->New Group. Name it ODEScript or similar.

Then rightclick on that folder, and click Add->Existing Files. Add all the .h and .cc files. On the targets screen that comes up next, make sure the files are being added to your targets [Torque-MacCarb-Debug and Torque-MacCarb-Release] [later on, you can remove the .h files from the target if you like, but don't remove them from the project].

Then click build. The next step depends on where and how you built ODE...

Gary (-;
#44
04/05/2007 (4:38 pm)
I made a How-To install odescript for Visual C++ 2005 Express: http://www.justsnowboarding.com/hs/users/1/ODEScript Tutorial.rtf
#45
04/05/2007 (5:30 pm)
now when i try to compile i have an undefined symbols error some examples are:
_dJointAttach
_dJointCreateContact
_dBodyAddForce
_dBodyAddTorque

ect

how do i fix that?
#46
04/05/2007 (5:51 pm)
That's because it can't find ODE to link with.

Build ODE on your mac from the command line [download ode-0.8 from ode.org] with parameters like:
./configure --prefix=/Users/your-username/ode --enable-release
make && make install

Then in ODE, edit your project settings. Add /Users/your-username/ode/lib to the "Library Search Paths" option, and add "-lode" to your "Other Linker Flags".

Personally, I just delete /Users/your-username/ode/lib/libode.dylib, and leave just libode.a for the linker to find, so it'll always get statically linked.

Gary (-;
#47
04/06/2007 (9:21 am)
before i had odescript included in the project, but not in a group. can you explain to me why the compiler treats it differently when it's in a group?
#48
04/06/2007 (10:50 am)
It shouldn't, a group is merely cosmetic. It's more likely that somehow they weren't being added to your target previously?

Gary (-;
#49
04/11/2007 (12:08 pm)
Hm, i've got an exception in 1.5 TGE..

Btw oderocks dont work in TGE 1.5.1 - unhandled exception in platformMemory.cc, line 485 (Probably, cause commented " //validateTree();" ? )
I use ode-0.7. What version of ODE I should to use ?

Thanks!


Added: Oh, all is ok with ode8 which compiled with vs2005 etc.
But now is new problem: when i press "C" in my game and click "Add #any#" i get "Fatal: dSprintf wrote to more memory than the specified buffer size".
TGE 1.5.1, VC2005, ODE-0.8.

In release build i didn't get an exceptions but no physics object created too.. I clicked to button and no result have got.

What is this can be?
#50
04/11/2007 (6:14 pm)
In the release build, what appeared in the console? And in the fatal error in the debug build, what file and line number were listed? Can you get a backtrace of it? [Sorry, I don't know how to do a backtrace on windows :-/]

For what it's worth, if you're using windows visual studio, I always recommend rebuilding ODE anyways. Microsoft have been breaking their development enviroments more than linux, even, recently :-/

Also, When you say "Add #any#", you do mean that that happens whichever of the three buttons you hit, right, and there's not just one button that says "Add #any#"?

Gary (-;
#51
04/12/2007 (6:21 am)
Yea, "Add #any#" mean "Add rock" or "Add box" or "Add link".
There are no messages in console added when i click on buttons.
But I saw some interesting thing: when clicked many-many times on buttons I had big decreasing of FPS (frames-per-second), it seems like solver calculations. and also messages in console:
Two bodies the same. Uhwha? b1=1603, b2=1603
Two bodies the same. Uhwha? b1=1603, b2=1603
Two bodies the same. Uhwha? b1=1603, b2=1603
#52
04/12/2007 (12:00 pm)
Ah. The objects are being added to your world, you just can't see them. Check with the video posted above. Make sure you're in stronghold, and look in the same direction as those.

That "Two bodies the same" bug is a strange one. It occurs when two bodies overlap significantly [as happens when you spawn a bunch of rocks really fast, at the same time]. The collision code does this:
1) Ask torque to list all the bodies colliding with a specific body
2) Return a list of bodies, that for some reason includes the original specific body

This shouldn't be happening. I mentioned it above, but the code is this:
Con::warnf("Two bodies the same. Uhwha? b1=%i, b2=%i",
  getId(), mCollisionList.collision[i].object->getId());
  // Totally pretend it didn't happen

Gary (-;
#53
04/12/2007 (9:34 pm)
how do i add multiple joints/links to one mesh?
#54
04/13/2007 (12:07 pm)
By "mesh", I assume you mean what ODE calls a "body"? Just keep on creating joints and attaching them. Magically works :-)

Currently the chain in the demo works thus:
1) Create body, create joint connecting it with the world
2) Create body, create joint connecting it with previous body
3) Repeat step 2 /ad boredom/

There's no reason not to connect them all to a specific body instead of the previous one

Gary (-;
#55
04/13/2007 (2:40 pm)
i looked in it and understood that, but i couldn't find what in the script controlled the position of the joints/links. or if it was a hinge joint, ball and socket, or sliding joint... does ODE have sliding joints?

anyway i think one of the two is SetBallAnchor. so what's the other one? also i read that ODE's collision system is far more efficient than torque's is this true? and if so can you explain to me how it works, i thought it would use SetSphere for a sphere, but your code appears to use SetSphereTotal; what exactly does that do?
#56
04/13/2007 (3:11 pm)
I cannot strongly enough recommend flicking through the ode docs; Therein, you'll find Sliding joints, setSphere/setSphereTotal, and a rundown of the concepts you're asking about here.

ODE's collision system is different to torque's, and richer in features. So far, torque's collision system has been sufficient for me. As you can see, ODE handles just fine given the collision data created by torque's collision system. If you decide to work on ragdoll, you'll find that torque's collision system isn't convenient for the task. At some point, it seems like GG may have hit the same obstacle I have, because Ben Garney wrote the polysoup collision code that would fix this problem. I'm unhappy with the licensing on that page, so I haven't downloaded it to try it.


The ODE documentation kinda exaplains it, but not very clearly. You don't need bodies to be physically touching to connect them with joints in the simulation. What you see on screen is merely a human-friendly version of what's going on in the physics simulation. When you construct a ball&socket joint, you only need to set one co-ordinate because that's all that matters for ball&socket joints [check around line 63 in chain.cs].

Hope all that helps,
Gary (-;
#57
04/13/2007 (3:19 pm)
i had downloaded the wrong set ODE docs. it makes alot more sense now. thanks (before i had a thing that named a command and said how it worked mathematically instead of what it did, i'm not sure where i got it)

anyway i tried replacing "shapeFile = "./boulder.dts";" with a reference to one of the objects i made and it always crashes (they work as static shapes), but when i replace it with an object from starter.fps it works. why does that happen?
#58
04/15/2007 (6:52 am)
Hi ... I'm not really very familiar with VS2005, so when I got the following error messages, at first I was confused:

tbodyconsole.cc
..\engine\odescript\tbodyconsole.cc(2) : fatal error C1083: Cannot open include file: 'console/simBase.h': No such file or directory

constructorSimpleMesh.cc
..\engine\constructor\constructorSimpleMesh.cc(5) : fatal error C1083: Cannot open include file: 'constructor/constructorSimpleMesh.h': No such file or directory

sgLightObject.cc
..\engine\lightingSystem\sgLightObject.cc(6) : fatal error C1083: Cannot open include file: 'lightingSystem/sgLighting.h': No such file or directory


Obviously, something's messed up with the include paths. I've added "c:\torque\engine" to the additional include directories and that got rid of the compiler errors, but I'm still not sure why I had to do that. Before I started the ODE modifications, I made sure that TGE would compile without errors ... which it did. Any clue what happened here ?

BTW, thanks to MB for providing such a nice and detailed "walkthrough" !!!!!!

Cheers,
Andreas
#59
04/15/2007 (8:00 am)
Gary,

I looked through the ODE docs (following the link you provided) but did not find anything with respect to buoyancy. Does this mean that ODE does not (yet) support that ?

I'm not a "C++" coder, so I don't really understand the intricacies involved, but how difficult would it be to support another physics DLL ? Specifically, I've worked with Newton Game Dynamics before on another engine and that worked extremely well and fast (including buoyancy ;) ).

I also noticed a significant framerate drop once I created a few linked rocks and "banged 'em around" a bit. Is that to be expected (I had only added 5 linked rocks and had 4 boxes created) ? My current PC is a P4(2,66) with 768MB RAM and a Nvidia GeForce 6800LE ...

Cheers,
Andreas

And before I forget : Thank you Gary for your contribution !!!
#60
04/16/2007 (11:52 am)
@Patrick:
Uh, dunno what I can say there. If the DTS that I supplied works, and the DTS that comes in the box works, but the DTS that's being created with your workflow doesn't, perhaps you could try modifying your workflow?

Maybe try a different DTS exporter? I don't know much about creating torque-friendly art, but your problems may be answered on the forums?


@Andreas:
Visual Studio stuff is kinda outta my league; I'm a linux/unix/mac guy. Sorry, I've no idea why Visual Studio would wig out on include paths :-/.

As far as I know, ODE does not support bouancy. But thankfully using torque, it'd be pretty easy to implement. Looking in shapeBase.h [from which ODEShape is derived], we have:

F32 mDrag;                       ///< Drag.
   F32 mBuoyancy;                   ///< Buoyancy factor.
   U32 mLiquidType;                 ///< Type of liquid (if any) we are in.
   F32 mLiquidHeight;               ///< Height of liquid around us (from 0..1).
   F32 mWaterCoverage;              ///< Percent of this object covered by water

So the potential is there... I'm messing with it... Just apply an upward force in ODEShape::processTick if a body is submerged. At least, that'd be the idea...

Performance drop may be because autocontact is turned off. Autocontact is something purely named by me, where contact joints are generated in C++ instead of torquescript. It's a *lot* faster, but less flexible, hence I leave it on usually. I think I may accidentally have left it off in this release, for some reason [duh].

Quote:I'm not a "C++" coder, so I don't really understand the intricacies involved, but how difficult would it be to support another physics DLL ? Specifically, I've worked with Newton Game Dynamics before on another engine and that worked extremely well and fast (including buoyancy ;) ).

Well, ODEScript is pretty heavily tethered to ODE, and not just in nomenclature. Taking the approach that I've taken would be a thing of relative ease with *any* physics engine, since basically all I did was expose the entire ODE API to script, and throw the word that is "object-orientated" at it. It works for some stuff, and doesn't work for other stuff. Honestly, you're looking at an almost complete rewrite if you want to make this work with Newton.

Gary (-;