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 ;-)
#42
10/20/2004 (10:33 am)
I am new and confused, added the code compiled, dowloaded boulder and what not, but i cant for the life of me figure out how to get the boulder in the game. It shows up in the editor under static shapes but not shapes. Can you tell me step by step what needs to be done to add the bolder, thanks.
#43
That means:
1) Add the datablock to an executed script file.
2) Add mission editor hook to an executed script file.
3) Modify the Player::onCollision method in server/scripts/player.cs
Hope that helps.
10/20/2004 (2:59 pm)
Make sure you do everything under the "How to use" section.That means:
1) Add the datablock to an executed script file.
2) Add mission editor hook to an executed script file.
3) Modify the Player::onCollision method in server/scripts/player.cs
Hope that helps.
#44
10/27/2004 (5:45 am)
Still no luck with this, i have followed the steps tried this a number of different ways with no luck, the only thing i dont understand is this"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." Is there somthing that needs to be done to reuse ShapeBaseObjectType? The only other thing i can figure is it not compiling correctly, is there a way to check that it has? Any help will be greatly appreciated as this is vitile to my project. thanks
#45
10/27/2004 (6:22 am)
I got it figured out, finally, It was not compiling, i just added the two files to the project in visual studio and compiled and it work, i guesss that is was you ment by ad to project and compile, not add files into server/script and just recompile everything, duh.
#46
11/13/2004 (7:53 pm)
The networking code for this resource uses way to much bandwidth... Even siting still aprox 20+ shapes laggs the game to a stand still in debug mode, and you can notice the difference in release , but not nearly as bad... im looking into fixing this, and ill post anything i come up with here... But for those of you using this understand that it uses a lot of bandwidth and dont add to many in a map.
#47
Remember the rigid shape is derived from whats inside the vehicle classes.
11/14/2004 (1:10 am)
It might not be bandwidth only, but also the complexity of your collision shapes. If you have more than a few faces, then there is _a lot_ of collision detection going on.Remember the rigid shape is derived from whats inside the vehicle classes.
#48
11/14/2004 (9:34 am)
the same issue happens with vehicles.... its not the resource... but an issue with how torque deal with the vehicles... if you have 30 vehicles siting still, anywhere on the map... you game will become very lagged.. my thought was, if movement is zero, then there should be nothing being sent... also if not on the screen, nothing should be sent... but aparently this isn't how its done
#49
11/23/2004 (9:56 am)
Just applied to latest head of 22nd November 2004, and it still works without modifications
#50
http://www.knowhere.net/gg/rigidshapetse.zip
If you want to use it in TSE, uncomment the define at the top of rigidShape.cc
I don't know if there's a define like that already, if there is, please let me know!
11/27/2004 (7:31 pm)
I've updated rigidShape.cc to work with TSE. It's available here:http://www.knowhere.net/gg/rigidshapetse.zip
If you want to use it in TSE, uncomment the define at the top of rigidShape.cc
I don't know if there's a define like that already, if there is, please let me know!
#51
12/02/2004 (12:26 pm)
I have been playing with the collision of rigidshapes but i have run into a problem. When two of my objects collide the collision occurs twice. Once from the moving object to the static and from the static back to the moving. The 2nd collision ruins the effct of the first. Any idea what would cause this or how to fix it?
#52
01/12/2005 (9:01 pm)
Hey y'all, I've managed to implement the equivalant of Item's setCollisionTimeout in the RigidShape class. Would you care to add it to this resource?
#53
01/12/2005 (10:29 pm)
Sure thing Bryan!! Dump it to me in an email (see profile) and I'll add it. Thanks!!
#54
03/19/2005 (3:34 pm)
Excelent.. thats all..! just perfect
#55
I was able to do this easily with the hoverVehicle class because I could just use the Z normal of the hoverVehicle's transform, but because the ball rotates and spins, it doesn't work so well.
Anyone have any ideas?
03/24/2005 (5:54 am)
I implemented this and it works wonderfully, but there's one thing I need it to do and I've been struggling and struggling and can't get it to work. I'm trying to edit the updateForces() so that it will change the gravity to push it against whatever surface it is nearest. I was able to do this easily with the hoverVehicle class because I could just use the Z normal of the hoverVehicle's transform, but because the ball rotates and spins, it doesn't work so well.
Anyone have any ideas?
#56
I need that for a game where the rigid shape is the "player object" and I'm trying to attach a 3rd person camera to it. Just rotates with the shape (as expected somehow, but not desired)
I just havent gotten that far yet - time and other projects *sigh*
03/24/2005 (5:58 am)
The rigid shape needs another transformation matrix basically that is oriented like the other shapes in TGE with z as "up" based on the terrain underneath instead of rotating with the shape.I need that for a game where the rigid shape is the "player object" and I'm trying to attach a 3rd person camera to it. Just rotates with the shape (as expected somehow, but not desired)
I just havent gotten that far yet - time and other projects *sigh*
#57
You have any ETA on maybe when you could have this figured out? :)
03/24/2005 (6:00 am)
Darn, so I guess im stuck for awhile eh?You have any ETA on maybe when you could have this figured out? :)
#58
But some day!!! Unless e.g. you beat me to it and share it with the rest of us - nudge nudge ;-)
03/24/2005 (6:03 am)
Sorry - nope. At the current rate of incomming projects/contracts vs finishes projects/contracts I'm not sure when this ever will happen.But some day!!! Unless e.g. you beat me to it and share it with the rest of us - nudge nudge ;-)
#59
I do need this to continue on with my game, been playing with it for 2-3 days now :(
Edit: I also found you a bunch of the code in updateForces() could be removed, I'll post those in a little
03/24/2005 (6:11 am)
I would, but I have no clue how to do it. I'm not the sharpest bulb in the box ;pI do need this to continue on with my game, been playing with it for 2-3 days now :(
Edit: I also found you a bunch of the code in updateForces() could be removed, I'll post those in a little
#60
04/09/2005 (12:35 pm)
Does anyone know how to modify the RigidShape code to be controlled by the keyboard? Essentially, I want to make a ball that will respond to physics as well as the RigidShape class but also have it be the "player" in the game. 
Torque Owner Wayne Eversole
Default Studio Name
Any help would be greatful
Thanks