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 ;-)
#102
[edit]
moving the PIVOT point fixed that... but now it's transform is in the middle... that's no good. that mass node didn't seem to make any difference whatso ever....
10/24/2005 (4:14 am)
I'm trying to make a pillar... and it's acting like a bowling pin.... it's hard to knock it down... it wobbles and returns strait up. even after falling on its side, it slowly finds its way up again...[edit]
moving the PIVOT point fixed that... but now it's transform is in the middle... that's no good. that mass node didn't seem to make any difference whatso ever....
#103
Edit:
Nevermind, I was a moron and had the game.cs script calling the files in the wrong directory.
11/02/2005 (12:32 pm)
I'm having difficulty adding the boulder from the Shape menu. Of course there's no problem adding it as a static shape. I have compiled it successfully (after commenting out the vRigidShape line), and the two scripts containing the datablock declaration and the mission editor hook compile without error. Any ideas?Edit:
Nevermind, I was a moron and had the game.cs script calling the files in the wrong directory.
#104
Edit: Ok, I've determined that the object movement occurs in Rigid::integrate. Now I just need to figure out how to modify the models...
11/08/2005 (7:38 am)
This is a great resource. I'm attempting to base a soft body physics class off of the rigidshape class. To do this I need to figure out how the collisions work and obtain new directions and velocities, etc. While I thought this should be easy, it appears my earlier guess was incorrect that the implementations would occurin resolveCollision and resolveContact. Would someone be able to provide me with some assistance here?Edit: Ok, I've determined that the object movement occurs in Rigid::integrate. Now I just need to figure out how to modify the models...
#105
Went into TGE 1.3 with no problems.
Thanks once again Thomas!
Ari
11/11/2005 (4:11 am)
Great resource!Went into TGE 1.3 with no problems.
Thanks once again Thomas!
Ari
#107
11/13/2005 (10:31 am)
Hmm after playing with this resource, I wondering if you could use this to make doors that open when you push on them?
#108
rigidshape.cc(514) error c2039 vrigidshape is not a member of rigidshape
why you guys all work it out well? am I the only people having this error.
thanks
11/30/2005 (7:58 am)
I have a compile error here:rigidshape.cc(514) error c2039 vrigidshape is not a member of rigidshape
why you guys all work it out well? am I the only people having this error.
thanks
#109
Guys, I'm having a problem when my Rigid Shapes are hit by something.
I've switched on the Network debugging code within the engine so it will tell me if Network packets are mismatched.
Everytime I collide with a rigid shape, I get a message saying that the packets don't match.
Does anyone have any ideas why this could be happening?
11/30/2005 (8:01 am)
@Yin, I just commented that line out and it compiled. As far as I could tell, vriginshape wasn't used. But don't take my word for it :-)Guys, I'm having a problem when my Rigid Shapes are hit by something.
I've switched on the Network debugging code within the engine so it will tell me if Network packets are mismatched.
Everytime I collide with a rigid shape, I get a message saying that the packets don't match.
Does anyone have any ideas why this could be happening?
#110
12/02/2005 (12:53 pm)
I have implimented this resource and everything works great except when the boulder is in motion it just slides instead of rotating. How can I make the boulder rotate?
#111
**EDIT: Never mind got it working... something with the object I was using. GREAT RESOURCE!!!!! this opens up many possibilities :D
12/12/2005 (11:43 pm)
Anyone get this to work with the SG light pack? I've got the code added to the engine and recompiled but it crashes in-game when I add the rigidshape in the world editor.**EDIT: Never mind got it working... something with the object I was using. GREAT RESOURCE!!!!! this opens up many possibilities :D
#112
12/13/2005 (8:03 am)
Am I misunderstanding Thomas, or does the class support both collision meshes and bounding boxes? I'm still fairly confused with respect to the dofference amongst the two. I would really appreciate anyone's help, as I'm using the rigid shape stuff as a basis for a deformable object type class and I am stuck updating the collision aspect of the newly deformed object. Thanks!
#113
Thanks
Todd
01/19/2006 (9:14 am)
I'm probably missing something, but I don't think the "integration" variable on the dataBlock is implemented. When I comment it out (to see everywhere it is implemented) it is added as a persistent field, packed, and unpacked, but that is it.Thanks
Todd
#114
01/19/2006 (6:42 pm)
Khalid, I'm having the same issue where the ball slides instead of rolls when an impulse force is applied. Can you give a brief rundown of what you did to solve this? Thanks.
#116
01/21/2006 (8:23 am)
Thanks Yuzairee...that worked really well...I should have putzed around more before posting.
#117
I copied and pasted this code at the end of function Armor::onCollision(%this,%obj,%col)
When Mr. Lund had these ... ... does he mean to only copy that inbetween those and pasted at the end.
When I made the boulder in Maya. I created a mount point and renamed to mass. I have parented it to all the nodes in maya and nothig. I have tried many ways ways but no luck. Can someone here this forum help me. Im little confused. Is there a special way Im overlooking over. Thanks
01/28/2006 (12:05 pm)
I have gotten the boulder(I made my own boluder) to run/roll down a hill. But I cant push it. I copied and pasted this code at the end of 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 objects position and initial velocity
%trans = %col.getTransform();
// Heres the position and rotation.
%pos = getWords(%trans, 0, 2);
%col.applyImpulse(%pos,%vec);
}When Mr. Lund had these ... ... does he mean to only copy that inbetween those and pasted at the end.
When I made the boulder in Maya. I created a mount point and renamed to mass. I have parented it to all the nodes in maya and nothig. I have tried many ways ways but no luck. Can someone here this forum help me. Im little confused. Is there a special way Im overlooking over. Thanks
#118
C:\Torque\SDK\engine\game\rigidShape.cc(514): error C2039: 'vRigidShape' : is not a member of 'RigidShape'
I have looked in the rigidShape.h and can't finc vRigidShape any where. Can someone help me with this?
Harrison
02/07/2006 (8:59 pm)
I got this error:C:\Torque\SDK\engine\game\rigidShape.cc(514): error C2039: 'vRigidShape' : is not a member of 'RigidShape'
I have looked in the rigidShape.h and can't finc vRigidShape any where. Can someone help me with this?
Harrison
#119
02/07/2006 (9:31 pm)
I fixed it.
#120
thanks for the great resource!
02/23/2006 (12:17 am)
hey thomas, i have been jockeying with values and i have run into a weird problem: the objects keep righting themselves if they tip over. Has this come up before? And if so, has anyone come across a fix?thanks for the great resource!

Torque Owner kc_0045
Default Studio Name
or change the massCenter = "0 0 0"; // Center of mass for rigid body
so its centered, and near the top more