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 ;-)
#182
%blockShape= new RigidShapeData(SuperBlock)
{
dataBlock = SuperBomb;
};
MissionCleanup.add(%blockShape);
return %blockShape;
But nothing is appearing?
Any ideas - I'm new to Torque
12/22/2006 (4:30 am)
I'm trying instantiate a RigidShapeData object through TorqueScript using:%blockShape= new RigidShapeData(SuperBlock)
{
dataBlock = SuperBomb;
};
MissionCleanup.add(%blockShape);
return %blockShape;
But nothing is appearing?
Any ideas - I'm new to Torque
#183
Thanks in advance
12/22/2006 (4:50 am)
As soon as two or more RigidShape object collide and remain at rest the frame rate drops to virtually a standstill. What is causing this and how can I avoid it?Thanks in advance
#184
12/22/2006 (5:07 am)
I have the same problem in TGE 1.5.
#185
You are instantiating the wronge class. RigidShapeData is the datablock class used to describe it, what you want to instantiate is a RigidShape class:
%blockShape= new RigidShape(SuperBlock)
{
dataBlock = SuperBomb;
};
12/22/2006 (1:40 pm)
@University of Huddersfield (#004You are instantiating the wronge class. RigidShapeData is the datablock class used to describe it, what you want to instantiate is a RigidShape class:
%blockShape= new RigidShape(SuperBlock)
{
dataBlock = SuperBomb;
};
#186
Got it working now, but still have serious performance issue when 2+ simple objects collide
12/25/2006 (5:15 am)
Thank you!Got it working now, but still have serious performance issue when 2+ simple objects collide
#187
12/26/2006 (7:25 pm)
Has anyone figured out why the rigidshapes jerk around in the air like Lee Latham and Doppler asked about earlier? I still haven't figured it out and I'm kind of ignoring it right now.
#188
12/27/2006 (8:36 am)
Generally, it's the "award-winning network code" that causes the jerky motions for objects... unfortunately.
#189
01/11/2007 (9:39 pm)
I found the reason I was getting "Register object failed for object (null) of class RigidShape". It was because I had the same shape in the level as a Static Shape already. If I delete the equivalent static shape I can then add the Rigid Shape version with no problem. Apparently they don't like sharing *.dts files!
#190
Is anyone going to be doing a conversion of these files to TGEA?
cheers
addikt
edit: This is the compile error I get:
rigidShape.cpp
..\engine\game\rigidShape.cpp(1630) : error C2660: 'ShapeBase::renderImage' : function does not take 2 arguments
02/11/2007 (7:41 pm)
Ive been trying to make this work in TGEA RC1, but I keep getting constant errors on compile. Ive tried that TSE version another guy posted, but that doesnt work either. Is anyone going to be doing a conversion of these files to TGEA?
cheers
addikt
edit: This is the compile error I get:
rigidShape.cpp
..\engine\game\rigidShape.cpp(1630) : error C2660: 'ShapeBase::renderImage' : function does not take 2 arguments
#191
02/11/2007 (8:23 pm)
@addiktive - It's an easy conversion... you should be able to figure it out. Try to fix it, you may learn something. ;)
#192
I guess what I'm asking is, does RigidShape already handle impulse vectors applied off-center (not directly in-line with the center of mass)?
02/26/2007 (7:49 pm)
I've got a quick question about RigidShape. I'm planning a space-based game, and I want to use true-to-life physics as the basis for moving/steering. I also want a hit from a weapon to impart spin For that, I need an object to drift & spin correctly when a force is applied. I'm not sure of the math involved at this point, but it seems like RigidShape should at least serve as a solid base.I guess what I'm asking is, does RigidShape already handle impulse vectors applied off-center (not directly in-line with the center of mass)?
#193
The RigidShape is just an exposure of the already existing rigid object used in all stock Torque vehicles. So this is more a matter of being able to use the rigid part without a vehicle.
With that said, the vehicles inside Torque have impulse support if I recall. So it should work here too.
02/26/2007 (10:53 pm)
Hi TheoThe RigidShape is just an exposure of the already existing rigid object used in all stock Torque vehicles. So this is more a matter of being able to use the rigid part without a vehicle.
With that said, the vehicles inside Torque have impulse support if I recall. So it should work here too.
#194
03/02/2007 (12:06 pm)
Hey guys, my engine crashes when i add my object. What am i missing?
#195
03/02/2007 (4:20 pm)
How are you adding it? Via Mission Editor? Can you bring in the example boulder?
#196
03/03/2007 (4:39 am)
@Lee: Yes, i can bring the example boulder. Actually, i defined an collision mesh and it worked, but i got an strange behaviour. My object starts to moving like an translation movement and dont stops. I dont know what to do about it.
#197
03/03/2007 (11:25 am)
Try messing with the mass and other datablock values. The example boulder, for example, is actually too heavy for a player to move by default. Yours may be way too light or something like that.
#198
04/25/2007 (8:30 pm)
Anybody have any experience with rigidshapes sinking through either dif or dts objects? Some of my rigidshapes are perfectly well behaved, others just love to sink into the dif floor of my room. I can't for the life of me find a significant difference between them. Any ideas would be greatly appreciated.
#199
I have seen this mod in action and I am very excited to add it but I am running into some problems. Let me tell you a few things before I dump the errors in here.
I'm using TGEA 1.01, and VS2005 Express for the compiler and I am not a C++ coder in the least. I know very little about the tool and I can only follow the things I have found on this site using the great google search tool here.
OK the errors:
\c:\torque\desolation_mod_5_9_07\engine\game\rigidshape.cc(9) : fatal error C1083: Cannot open include file: 'dgl/dgl.h': No such file or directory
and
BSCMAKE: error BK1506 : cannot open file '..\engine\out.vc8.win32.debug\rigidShape.sbr': No such file or directory
Now I looked and searched for a file named dgl.h and there is no such animal in the whole directory or a directory of that name. I tried to comment it out and that caused more errors....
The referance line in the rigidShap.cc file that asks for it is:
#include "dgl/dgl.h"
Which I understand to be a file needed to be included to compile this one...
Any help would be great.
(PS I searched for it and I got some deep stuff that looked dated a bit and I dont know how to circumvent this issue.)
05/12/2007 (10:24 pm)
Hello!I have seen this mod in action and I am very excited to add it but I am running into some problems. Let me tell you a few things before I dump the errors in here.
I'm using TGEA 1.01, and VS2005 Express for the compiler and I am not a C++ coder in the least. I know very little about the tool and I can only follow the things I have found on this site using the great google search tool here.
OK the errors:
\c:\torque\desolation_mod_5_9_07\engine\game\rigidshape.cc(9) : fatal error C1083: Cannot open include file: 'dgl/dgl.h': No such file or directory
and
BSCMAKE: error BK1506 : cannot open file '..\engine\out.vc8.win32.debug\rigidShape.sbr': No such file or directory
Now I looked and searched for a file named dgl.h and there is no such animal in the whole directory or a directory of that name. I tried to comment it out and that caused more errors....
The referance line in the rigidShap.cc file that asks for it is:
#include "dgl/dgl.h"
Which I understand to be a file needed to be included to compile this one...
Any help would be great.
(PS I searched for it and I got some deep stuff that looked dated a bit and I dont know how to circumvent this issue.)
#200
TGEA uses a different rendering system as TGE. The missing file is the Open GL header - which is not present (or usable for that matter) in TGEA.
I havent used rigid shapes in TGEA, but try simply to comment/remove the line and see what happens! It might not even be used.
If it does (cant remember the code from my head), then you might be out of luck without some help from a C++ coder
05/13/2007 (2:25 am)
Hi GTGEA uses a different rendering system as TGE. The missing file is the Open GL header - which is not present (or usable for that matter) in TGEA.
I havent used rigid shapes in TGEA, but try simply to comment/remove the line and see what happens! It might not even be used.
If it does (cant remember the code from my head), then you might be out of luck without some help from a C++ coder

Torque 3D Owner CodingChris