Rigid Shape Class
by Thomas \"Man of Ice\" Lund · 04/07/2004 (9:57 am) · 256 comments
Download Code File
History
2005-09-08 Updated with performance fix from XoCluTch
What is this?
For my game I needed a boulder that rolls down a hill, and that one needs to evade. Initial attempts to use the existing classes (items, shapes and especially projectiles) fail because of too simple physics and/or lack of collision box support. I was faced with either implementing collision boxes in the projectile class, look into ODE or try to code my own rigid body class.
Thanks to Ben Garney I didnt have to do any of these (thanks ;-) ), as there already exists rigid physics in the engine. The rigid.cc/h files have everything needed, but they are only available as con/net objects using the vehicle classes.
So I simply took the base vehicle class and the hover vehicle class, merged them together and removed most of the unneeded code. The result is actually fantastic, and I cant believe this isnt standard part of Torque.
As the code basically is a hover vehicle there might be parts of the code that should be removed further. If you spot any of those, then post here and I'll update the codebase.
What does it look like?
No resource without a movie :-)
Here is the final result from a tech demo level in my upcomming action adventure game.
www.codejar.com/rigidshape4.wmv
I also did a few early tests and I've included the movies here. The lack of realism is totally due to way too much impulse applied with a large vertical factor + low mass for the chests.
www.codejar.com/rigidshape.wmv
www.codejar.com/rigidshape2.wmv
www.codejar.com/rigidshape3.wmv
All movies are approx 3 MB
How to add
First thing is to add the 2 attached files in the zip to your engine\game dir and to your project. Then open the shapebase.h and find this
and add
Also in the same file down the bottom find this
and add
Recompile your engine and thats it.
If you want to do this "for real", then one also needs to define a new objectType. I have reused the ShapeBaseObjectType - change that if you want or go through tons of code and add a RigidShapeObjectType.
How to use
From script you now got access to a RigidShapeData datablock and a RigidShape object type. Your DTS object is required to have a mass node, but nothing else. The code still includes dusttrail and splash emitters from the vehicle code, as well as impact + water sound. This example does not use any of those.
An example datablock for a rigid shape is included below
By including the category="" it now shows up in the world editor under the Shapes, and its fully working. Spawn the object on a hilltop and see it roll down :-)
For the mission editor to work you will need to add a create() function that hooks into the mission editor. You can either put this into a rigidShape.cs file (dont forget to load it from game.cs) or put it into any .cs file in the server\script
If you want to have a little fun pushing things around then add this to your player onCollision:
I've done a few tests with forces, masses and such - and as you saw from my movies with varying results.
The objects are fully network aware, and I bet this code can replace ODE for most purposes if you dont need total realism.
As I wrote earlier, the code can be stripped down further. There are various pieces of the physics that I do not understand fully and left in. I would be very happy if someone could run through those parts and see if any of it can be taken out.
Regarding performance, I've tried to add approx 20 shapes to a scene and then letting all collide with each other (its in one of the movies actually). I noticed no performance degradation at all with those simple shapes (they had a 6 sided box as collision mesh.
The boulders in the last movie have an approx 25 faced hedra inside it as a collision mesh. I think I had 15 of those in an scene without performance degradation.
These shapes do have 1 problem though, and that is the collision detection that sometimes fails - exactly as vehicles. If they gain too much speed they will dip into the terrain or go through it. I think a lot of people are looking into that problem at the moment here www.garagegames.com/mg/forums/result.thread.php?qt=17384
Enjoy ;-)
History
2005-09-08 Updated with performance fix from XoCluTch
What is this?
For my game I needed a boulder that rolls down a hill, and that one needs to evade. Initial attempts to use the existing classes (items, shapes and especially projectiles) fail because of too simple physics and/or lack of collision box support. I was faced with either implementing collision boxes in the projectile class, look into ODE or try to code my own rigid body class.
Thanks to Ben Garney I didnt have to do any of these (thanks ;-) ), as there already exists rigid physics in the engine. The rigid.cc/h files have everything needed, but they are only available as con/net objects using the vehicle classes.
So I simply took the base vehicle class and the hover vehicle class, merged them together and removed most of the unneeded code. The result is actually fantastic, and I cant believe this isnt standard part of Torque.
As the code basically is a hover vehicle there might be parts of the code that should be removed further. If you spot any of those, then post here and I'll update the codebase.
What does it look like?
No resource without a movie :-)
Here is the final result from a tech demo level in my upcomming action adventure game.
www.codejar.com/rigidshape4.wmv
I also did a few early tests and I've included the movies here. The lack of realism is totally due to way too much impulse applied with a large vertical factor + low mass for the chests.
www.codejar.com/rigidshape.wmv
www.codejar.com/rigidshape2.wmv
www.codejar.com/rigidshape3.wmv
All movies are approx 3 MB
How to add
First thing is to add the 2 attached files in the zip to your engine\game dir and to your project. Then open the shapebase.h and find this
class ShapeBaseConvex : public Convex
{
typedef Convex Parent;
friend class ShapeBase;
friend class Vehicle;and add
friend class RigidShape;
Also in the same file down the bottom find this
#define StaticShape_GenericShadowLevel 2.0f #define StaticShape_NoShadowLevel 2.0f
and add
#define RigidShape_GenericShadowLevel 0.7f #define RigidShape_NoShadowLevel 0.2f
Recompile your engine and thats it.
If you want to do this "for real", then one also needs to define a new objectType. I have reused the ShapeBaseObjectType - change that if you want or go through tons of code and add a RigidShapeObjectType.
How to use
From script you now got access to a RigidShapeData datablock and a RigidShape object type. Your DTS object is required to have a mass node, but nothing else. The code still includes dusttrail and splash emitters from the vehicle code, as well as impact + water sound. This example does not use any of those.
An example datablock for a rigid shape is included below
datablock RigidShapeData( BouncingBoulder )
{
category = "RigidShape";
shapeFile = "~/data/shapes/boulder/boulder.dts";
emap = true;
// Rigid Body
mass = 500;
massCenter = "0 0 0"; // Center of mass for rigid body
massBox = "0 0 0"; // Size of box used for moment of inertia,
// if zero it defaults to object bounding box
drag = 0.2; // Drag coefficient
bodyFriction = 0.2;
bodyRestitution = 0.1;
minImpactSpeed = 5; // Impacts over this invoke the script callback
softImpactSpeed = 5; // Play SoftImpact Sound
hardImpactSpeed = 15; // Play HardImpact Sound
integration = 4; // Physics integration: TickSec/Rate
collisionTol = 0.1; // Collision distance tolerance
contactTol = 0.1; // Contact velocity tolerance
minRollSpeed = 10;
maxDrag = 0.5;
minDrag = 0.01;
triggerDustHeight = 1;
dustHeight = 10;
dragForce = 0.05;
vertFactor = 0.05;
normalForce = 0.05;
restorativeForce = 0.05;
rollForce = 0.05;
pitchForce = 0.05;
};By including the category="" it now shows up in the world editor under the Shapes, and its fully working. Spawn the object on a hilltop and see it roll down :-)
For the mission editor to work you will need to add a create() function that hooks into the mission editor. You can either put this into a rigidShape.cs file (dont forget to load it from game.cs) or put it into any .cs file in the server\script
// Hook into the mission editor.
function RigidShapeData::create(%data)
{
// The mission editor invokes this method when it wants to create
// an object of the given datablock type.
%obj = new RigidShape() {
dataBlock = %data;
};
return %obj;
}If you want to have a little fun pushing things around then add this to your player onCollision:
function Armor::onCollision(%this,%obj,%col)
{
...
if (%col.getDataBlock().getName() $= "BouncingBoulder") {
// Apply an impulse to the object we collided with
%eye = %obj.getEyeVector();
%vec = vectorScale(%eye, 10);
// Add a vertical component to give the item a better arc
%dot = vectorDot("0 0 1",%eye);
if (%dot < 0)
%dot = -%dot;
%vec = vectorAdd(%vec,vectorScale("0 0 2",1 - %dot));
// Set the object's position and initial velocity
%trans = %col.getTransform();
// Heres the position and rotation.
%pos = getWords(%trans, 0, 2);
%col.applyImpulse(%pos,%vec);
}
...
}I've done a few tests with forces, masses and such - and as you saw from my movies with varying results.
The objects are fully network aware, and I bet this code can replace ODE for most purposes if you dont need total realism.
As I wrote earlier, the code can be stripped down further. There are various pieces of the physics that I do not understand fully and left in. I would be very happy if someone could run through those parts and see if any of it can be taken out.
Regarding performance, I've tried to add approx 20 shapes to a scene and then letting all collide with each other (its in one of the movies actually). I noticed no performance degradation at all with those simple shapes (they had a 6 sided box as collision mesh.
The boulders in the last movie have an approx 25 faced hedra inside it as a collision mesh. I think I had 15 of those in an scene without performance degradation.
These shapes do have 1 problem though, and that is the collision detection that sometimes fails - exactly as vehicles. If they gain too much speed they will dip into the terrain or go through it. I think a lot of people are looking into that problem at the moment here www.garagegames.com/mg/forums/result.thread.php?qt=17384
Enjoy ;-)
#222
06/06/2008 (6:02 am)
Ill give it a shot. Its strange because I didnt think rts kit changed much in the game. Also ive replaced the rigidshape.cc files with these ones so thats not the problem. The boulder does show up as a static shape.
#223
06/06/2008 (10:11 am)
Still no dice with the rts kit. It does work fine in 1.4.2 however. I want to make a box that you can easily push but that wont rotate like a boulder it will just slide along in the direction you push it. Any Ideas on what to change in the boulder.cs file to acheive this? I tried changing rollforce to 0 but it had little effect.
#224
06/06/2008 (7:08 pm)
As far as the physics go, try just making it really heavy. Don't know about the invisibleness, though.
#225
06/07/2008 (8:06 am)
I guess I'll have to play around with it abit I just cant seem to get it right. Ive been trying to get a simple box shape working with this. my shape loads as a static shape fine and has a mass node though the collision mesh doesnt seem to work. as I can walk through it when its a static shape. when I load it as a rigidshape the engine crashes. How exactly do you add the collision mesh. I'm using 3dsmax and Ive added a mesh called col1 thats linked to the start node and then I have a node called collision1. Is this right? it doesnt seem to work.
#226
and is just in the trigger function.
i need to delete when the Shape enter to the trigger zone. so in my trigger i get the pointer %obj, but when i try to delete , its crash.
function triggerSapo::onEnterTrigger( %this, %trigger, %obj )
{
%obj.delete();
}
06/07/2008 (8:27 am)
When i try to delete the rigidShape the application crash. i am using %obj.delete();and is just in the trigger function.
i need to delete when the Shape enter to the trigger zone. so in my trigger i get the pointer %obj, but when i try to delete , its crash.
function triggerSapo::onEnterTrigger( %this, %trigger, %obj )
{
%obj.delete();
}
#227
@Alejandro: What does the debugger tell you?
06/07/2008 (10:10 am)
@Luke: that might be your issue, I'd take it to the 3dsmax exporter forum. I'm starting to think that your other issue is in the source, however.@Alejandro: What does the debugger tell you?
#228
06/09/2008 (11:12 am)
I was using Col_1 and Collision_1 for reference it needs to be Col-1 and Collision-1.
#229
the application crash , when the rigid Shape enters to the trigger.
have you tried to delete the rigidShape in the trigger function ?
06/09/2008 (12:13 pm)
the console debugger doesnt say anything. the application crash , when the rigid Shape enters to the trigger.
have you tried to delete the rigidShape in the trigger function ?
#230
also the boulder model doesnt fall through. Could I be doing something wrong here. what I have is a big rectangle for the ground which is a dif model. Then I have a box as a rigidshape. All I want to be able to do is push the box around on the dif. Prehaps its the collision mesh on my box that is causing the trouble. is the collision mesh for the boulder spherical?
06/09/2008 (2:55 pm)
Im having alot of trouble with the rigidshape falling through the terrain and through diff models. Will it also fall through dts models out of curiosity? also the boulder model doesnt fall through. Could I be doing something wrong here. what I have is a big rectangle for the ground which is a dif model. Then I have a box as a rigidshape. All I want to be able to do is push the box around on the dif. Prehaps its the collision mesh on my box that is causing the trouble. is the collision mesh for the boulder spherical?
#231
@Luke: you are running into the famous rigid body collision bugs in Torque. Search for the forums, there are various partial fixes floating around.
06/09/2008 (6:31 pm)
@Alajandro: No, I meant the Visual Studio debugger, running a debug exe. @Luke: you are running into the famous rigid body collision bugs in Torque. Search for the forums, there are various partial fixes floating around.
#232
http://www.garagegames.com/mg/forums/result.thread.php?qt=63305
And they deal with a change in Rigid.cpp and another further down to change in Convex.cpp. I made these changes however and noticed no difference. Is there any other fixes that I'm missing?
Also Henry Todd in his post states that the algorithm really doesn't like big flat surface. Prehaps that is why Im having this trouble with the box and not with the boulder. He says that For safety, make your crates kind of like chamfer boxes. Anyone know what a chamfer box is? presumeably he meens to make the collision box like that?
Thanks lee for all your help!
06/10/2008 (5:56 am)
The only fixes I could find were located inhttp://www.garagegames.com/mg/forums/result.thread.php?qt=63305
And they deal with a change in Rigid.cpp and another further down to change in Convex.cpp. I made these changes however and noticed no difference. Is there any other fixes that I'm missing?
Also Henry Todd in his post states that the algorithm really doesn't like big flat surface. Prehaps that is why Im having this trouble with the box and not with the boulder. He says that For safety, make your crates kind of like chamfer boxes. Anyone know what a chamfer box is? presumeably he meens to make the collision box like that?
Thanks lee for all your help!
#233
First-chance exception at 0x008b98db in torqueDemo_DEBUG.exe: 0xC0000005: Access violation reading location 0xcececeda
the code stop in :
the ptr is null.
any ideas ?
06/10/2008 (2:02 pm)
ok , this is what happends in the debuggerFirst-chance exception at 0x008b98db in torqueDemo_DEBUG.exe: 0xC0000005: Access violation reading location 0xcececeda
the code stop in :
void ShapeBase::notifyCollision()
{
// Notify all the objects that were just stamped during the queueing
// process.
SimTime expireTime = Sim::getCurrentTime() + CollisionTimeoutValue;
for (CollisionTimeout* ptr = mTimeoutList; ptr; ptr = ptr->next)
{
if (ptr->expireTime == expireTime && ptr->object)
{ ....the ptr is null.
any ideas ?
#234
datablock RigidShapeData(boulder)
{
category = "Stuff";
shapeFile = "~/data/shapes/boulder.dts";
};
and when i add it with the world editor creator it falls to the ground, jitters around for a while; then the game freezes and i can't move my mouse so I have to shut down my system. what should i do?
06/11/2008 (7:36 pm)
i added the following thing to my game:datablock RigidShapeData(boulder)
{
category = "Stuff";
shapeFile = "~/data/shapes/boulder.dts";
};
and when i add it with the world editor creator it falls to the ground, jitters around for a while; then the game freezes and i can't move my mouse so I have to shut down my system. what should i do?
#235
Also, make your object much heavier seems to help. Sadly, the collision in Torque needs a lot of work (at least for rigidbody collision).
06/11/2008 (8:57 pm)
@Patrick: It's probably the aforementioned collision issues. The thread Luke found is probably the best one, and kept my game from freezing up at least, mostly (a great improvement actually).Also, make your object much heavier seems to help. Sadly, the collision in Torque needs a lot of work (at least for rigidbody collision).
#236
Anyhow I fixed the collision problem with my box by editing the collision model. It seems flat surfaces against flat surfaces dont work too well with rigid shapes so I added an edge through the middle of my collision box on the bottom face that is against the ground and moved it up. so from side on the bottom look like this ^
since then I havent come across any collision issues.
06/12/2008 (2:51 am)
I shouldnt think that its the collision bug because the boulder actually works really well for me its never fallen through once. I presume you are talking about the boulder.dts from this page? Have you tried using the datablock suggested in the code above. Anyhow I fixed the collision problem with my box by editing the collision model. It seems flat surfaces against flat surfaces dont work too well with rigid shapes so I added an edge through the middle of my collision box on the bottom face that is against the ground and moved it up. so from side on the bottom look like this ^
since then I havent come across any collision issues.
#237
06/12/2008 (8:11 am)
Patrick: Try setting collisionTol to something big like 1.0 and see if you still have trouble.
#238
06/12/2008 (8:12 am)
The default settings for the RigidShapeData datablock need to be overridden in some cases (i.e. they don't work very well for some objects).
#239
06/12/2008 (8:54 am)
Also, the contactTol setting (which is the velocity, below which, the contact resolver gets used instead of the collision resolver) can be key. Both the collisionTol and contactTol need to be set - the stock values aren't very useful in most cases.
#240
Lee: Trying driving a vehicle into a "crate" type object, pushing it around on the terrain... you'll see plenty of "collision problems" with that simple test.
06/12/2008 (9:05 am)
The collisionTol setting is basically the minimum distance which two objects need to be from each other before a collision can happen. If you set it to 1.0, there will be a whole 1.0 world unit between an object and the terrain (sitting at rest). Unfortunately, you can't set it too small either because, due to the way collision works in TGE, if two objects overlap deeper than the collisionTol distance, contact points will get missed... eventually allowing objects to interpenetrate.Lee: Trying driving a vehicle into a "crate" type object, pushing it around on the terrain... you'll see plenty of "collision problems" with that simple test.

Torque Owner Lee Latham
Default Studio Name
I've screwed around with this quite a bit--and that's really strange. Perhaps try adding it by editing the mission file directly, as opposed to the mission editor? shot in the dark....