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 ;-)
#202
engine\game\rigidshape.cc(1630) : error C2660: 'ShapeBase::renderImage' : function does not take 2 arguments
This is the same error above from addiktive (Feb 11, 2007 at 20:41)
The answer:
Tom Spilman (Feb 11, 2007 at 21:23)
@addiktive - It's an easy conversion... you should be able to figure it out. Try to fix it, you may learn something. ;)
Well that leaves me completely clueless..... I know nothing of C++
Was this solved or anyone know the ... obvious answer that was implied here...?
05/13/2007 (6:36 pm)
I followed all that but I get this:engine\game\rigidshape.cc(1630) : error C2660: 'ShapeBase::renderImage' : function does not take 2 arguments
This is the same error above from addiktive (Feb 11, 2007 at 20:41)
The answer:
Tom Spilman (Feb 11, 2007 at 21:23)
@addiktive - It's an easy conversion... you should be able to figure it out. Try to fix it, you may learn something. ;)
Well that leaves me completely clueless..... I know nothing of C++
Was this solved or anyone know the ... obvious answer that was implied here...?
#203
I know it's an outrageous hack, but it does let you build the engine and use rigidshapes.
05/13/2007 (7:10 pm)
Oh yeah, I ran into that, too. Just look for the place where it's calling that function, look at the parameters the function takes, and get rid of the one it doesn't use.I know it's an outrageous hack, but it does let you build the engine and use rigidshapes.
#204
05/19/2007 (2:10 pm)
Fixed with warnings and I combined... I think the updated and the conversion (good commenting would have helped).
#205
thanks
06/18/2007 (8:28 pm)
when i try to extract the files to the engine/game folder, it says that a file already exist, is it ok to overwrite that file?thanks
#206
06/18/2007 (9:57 pm)
nevermind i got it to work, it works great!
#207
06/18/2007 (10:28 pm)
The resource is included in current versions (1.5.x) of TGE, so you don't need to download it.
#208
You can get the files here. Don't forget to make the changes to shapeBase.h, as documented above.
Let me know if you have any problems. Like Thomas, I haven't optimized the code, but it does seem to work rather well.
-- M. Cooper
07/09/2007 (1:47 am)
If anyone needs the TGEA version of this, I've modified Thomas' files for use with it.You can get the files here. Don't forget to make the changes to shapeBase.h, as documented above.
Let me know if you have any problems. Like Thomas, I haven't optimized the code, but it does seem to work rather well.
-- M. Cooper
#209
Looking for a physics solution for TGEA and was just about to convert it myself for testing and looked down here. Thanks!
07/10/2007 (2:03 pm)
Thanks Milo,Looking for a physics solution for TGEA and was just about to convert it myself for testing and looked down here. Thanks!
#210
Error 1 error C2039: 'vRigidShape' : is not a member of 'RigidShape' c:\torque\tge_1_5_2\engine\game\rigidshape.cc 514
Can someone help?
07/14/2007 (8:47 pm)
Hey? I get an error like this.Error 1 error C2039: 'vRigidShape' : is not a member of 'RigidShape' c:\torque\tge_1_5_2\engine\game\rigidshape.cc 514
Can someone help?
#211
if somebody has it, i'd be grateful if they could pass it on. thankyou...
08/17/2007 (9:18 am)
both TGEA links are now dead ? is this resource still available anywhere?if somebody has it, i'd be grateful if they could pass it on. thankyou...
#212
%hit = getWords($got.getTransform(), 0, 2);
$got.applyImpulse(%hit, %Vel);
does not move the object at all, but
%hit = $got.getWorldBoxCenter();
$got.applyImpulse(%hit, %Vel);
Does, but it spins out of control as it seems to have hit a rather exact point, rather then just moving it from the center. any thoughts?
09/12/2007 (1:42 pm)
I have the working Rigid shape working in the TSE, but there seems to be an issue with the applyImpulse, using the %hit = getWords($got.getTransform(), 0, 2);
$got.applyImpulse(%hit, %Vel);
does not move the object at all, but
%hit = $got.getWorldBoxCenter();
$got.applyImpulse(%hit, %Vel);
Does, but it spins out of control as it seems to have hit a rather exact point, rather then just moving it from the center. any thoughts?
#213
You'll have to search around for the code, though, because it's all over the place. Hope you got Torsion. Feel free to email me if you have any questions.
09/12/2007 (2:45 pm)
There are issues with rigidshape physics currently in Torque, is part of the problem. As for details, I do quite a lot of applyImpulse on Rigidshapes in my game www.singularityfps.com. The test version is available for download and the scripts are all there. I don't purport it to be excellent, but it does work. You'll have to search around for the code, though, because it's all over the place. Hope you got Torsion. Feel free to email me if you have any questions.
#214
Use:
%hit = %col.Position();
Instead of:
%hit = getWords($got.getTransform(), 0, 2);
09/19/2007 (12:20 pm)
@Paul Ash:Use:
%hit = %col.Position();
Instead of:
%hit = getWords($got.getTransform(), 0, 2);
#215
10/12/2007 (1:35 pm)
It crashes for some reason when a rigidshape touches an interior. Has anyone else had this problem?
#216
11/22/2007 (4:23 pm)
Shapes go trough terrain, move a bit strangely at high speed but not that bad (impulses work great) and make the framerate drop like hell when two collide. I know it all been asked before but nobody answered if there's some parameter other than the mass I could change.
#217
www.garagegames.com/mg/forums/result.thread.php?qt=63305
11/23/2007 (2:38 pm)
The collision issue is a longstanding one in Torque, and there is no public fix out there, yet. There is a thread where people are working on it, and some fixes are there (for lockups), but the sinking through terrain and stuff isn't, although a fix is described: www.garagegames.com/mg/forums/result.thread.php?qt=63305
#218
02/10/2008 (11:54 am)
Has anybody managed to get mounting working with RigidShapes? Tom Spilman had something going, but it seems like it's still unfinished.
#219
02/10/2008 (1:50 pm)
Yep, it's easy. I have examples in my game (check my profile) with the torquescript there for you to peruse. You'll need to put rigidshape.cs back in to game.cs exec's. Check out the portopotty and the cokecan. Feel free to email me directly if you have questions.
#220
note that im using the rts starter kit, I just checked it with the starter kit 1.4.2 and it works fine. Any ideas why it wouldnt render the model in rts 1.5.1 (i think its 1.5.1 as it has the lighting kit.)
Regards,
Luke
06/05/2008 (5:20 pm)
I dont know if anyone still checks this resource. I'm having loads of trouble getting the dts file to actually show up. I can add the rigid shape in the world editor and can see the bounding box etc and it moves and rotates like it should but there is no mesh. I'm using the boulder from this resource so the dts file is fine. I created a new rigidshape and boulder .cs file and used the reference code from here. Any Ideas why its not working?note that im using the rts starter kit, I just checked it with the starter kit 1.4.2 and it works fine. Any ideas why it wouldnt render the model in rts 1.5.1 (i think its 1.5.1 as it has the lighting kit.)
Regards,
Luke

Torque Owner Lee Latham
Default Studio Name
Brian Richardson (bzztbomb) (Nov 27, 2004 at 22:31)