Dynamic Gravity Modification for TGE 1.52
by Vis · 08/14/2007 (9:42 am) · 9 comments
I take no credit for this resource other than updating it to work with Torque 1.52
This is an updated version of Dynamic and Individualized Gravity by Chris Evans
and includes changes submitted by many others to the original resource.
This modification will allow you to change the default 'gravity constant' for a player,item or vehicle.
Here is a list of the steps needed to implement the modification.
1. add a new variable 'mGravity' to the ShapeBase class
2. add two new functions/methods to ShapeBase class so we can get and set the new variable
3. add two console methods so we can also get and set the new variable from script
4. add code to initialize gravity in ShapeBase::ShapeBase() and ShapeBase::onNewDataBlock()
5. add a gravity mask flag to the ShapeBaseMasks enum, (this will be used by new code in pack/unpackUpdate)
6. add code to ShapeBase's packUpdate() so changes to the objects gravity get sent over the network
7. add code to ShapeBase's unpackUpdate() so changes to the objects gravity get received over the network
8. update the player and item classes to use the new variable
9. update the vehicle class to use the new variable
10. update the flying vehicle class to use the new variable
11. update the hover vehicle class to use the new variable
12. update the wheeled vehicle class to use the new variable
and here are the details ...... Good luck :)
============================================================================================
Step 1. add a new variable 'mGravity' to the ShapeBase class
Open engine\game\shapeBase.h
Search for 'F32 mEnergy;'
Right above it enter:
============================================================================================
Step 2. add two new functions to the ShapeBase class so we can get and set the new variable
Still in engine\game\shapeBase.h
Search for 'virtual void setEnergyLevel(F32 energy);'
and below it enter:
Save the file!
Open engine\game\shapeBase.cc
search for 'ConsoleMethod( ShapeBase, setHidden, void, 3, 3, "(bool show)")'
and above it enter:
============================================================================================
Step 3. add two console methods so we can also get and set the new variable from script
Still in shapeBase.cc
At the bottom of the file you will find 'void ShapeBase::consoleInit()'
add the following above it:
============================================================================================
Step 4. add code to initialize gravity in ShapeBase::ShapeBase() and ShapeBase::onNewDataBlock()
Still in shapeBase.cc
Search for 'ShapeBase::ShapeBase()'
Scroll down and find 'mEnergy = 0;' and above it enter:
Search for 'bool ShapeBase::onNewDataBlock(GameBaseData* dptr)'
Scroll down again until you find "mEnergy = 0;" and above it enter:
Save the file!
============================================================================================
Step 5. add a gravity mask flag to the ShapeBaseMasks enum
Open/Switch to shapeBase.h
Search for 'enum ShapeBaseMasks {"
Inside it you need to add GravityMask
Before we change it it looks like this:
Insert a line after the SkinMask line containing this:
And change 8 to a 9 on the line below it so we end up with an enum that looks like this:
Save the file!
A note here, if you add any other mask bits to this enum make sure
you do not exceed 32. As it stands now (TGE 1.52) :
SceneObject uses 1 bit
GameBase uses 4 bits
axSoundThreads is 4
MaxScriptThreads is 4
MaxMountedImages is 4
this class uses 10
giving us a total of 27 bits used.
============================================================================================
Step 6. add code to ShapeBase's packUpdate() so changes to the objects gravity get sent over the network.
Open/Switch to shapeBase.cc
Search for the function: 'U32 ShapeBase::packUpdate(NetConnection *con, U32 mask, BitStream *stream)'
Find the statement below:
Add " |GravityMask" just behind "SkinMask" so it looks like the following:
Scroll down a little in the same function until you find the following:
Add " |GravityMask" just behind "SkinMask" so it looks like the following:
Now scroll down and find this:
add this below it:
============================================================================================
Step 7. add code to ShapeBase's unpackUpdate() so changes to the objects gravity get received from the network.
Still in shapeBase.cc
Search for the function: 'void ShapeBase::unpackUpdate(NetConnection *con, BitStream *stream)'
Scroll down and find the following:
and after it enter:
Save the file!
NOTE: It is important that the data is read from the stream in the correct order, in
our case just after the 'SkinMask' flag and data.
============================================================================================
Step 8. update the player and item classes to use the new variable
Or more to the point scrap the existing variables defined in the player and item classes
Open engine\game\player.h
Find and comment out the following:
Open engine\game\player.cc
Find and comment out the following:
Save both files!
--------------------------------------------------------------
Open engine\game\item.h
Find and comment out the following:
Open engine\game\item.cc
Find and comment out the following:
Save both files!
============================================================================================
Step 9. update the vehicle class to use the new variable
Open engine\game\vehicles\vehicle.cc
Find and comment out 'const F32 sVehicleGravity = -20;'
Search for 'sVehicleGravity' replace with 'mGravity'
you should only find one instance of sVehicleGravity to replace.
Save the file!
============================================================================================
Step 10. update the flying vehicle class to use the new variable
Open engine\game\vehicles\flyingVehicle.cc
Find and comment out 'static F32 sFlyingVehicleGravity = -20;'
Search for 'sFlyingVehicleGravity' replace with 'mGravity'
you should find three instances of sFlyingVehicleGravity to replace.
Save the file!
============================================================================================
Step 11. update the hover vehicle class to use the new variable
Open engine\game\vehicles\hoverVehicle.cc
Find and comment out 'const F32 sHoverVehicleGravity = -20;'
Search for 'sHoverVehicleGravity' replace with 'mGravity'
you should find two instances of sHoverVehicleGravity to replace.
Save the file!
============================================================================================
Step 12. update the wheeled vehicle class to use the new variable
Open engine\game\vehicles\wheeledVehicle.cc
Find and comment out 'static F32 sWheeledVehicleGravity = -20;'
Search for 'sWheeledVehicleGravity' replace with 'mGravity'
you should find two instances of sWheeledVehicleGravity to replace.
Save the file!
============================================================================================
Compile the changes ....
To use the changes from script:
Use %obj.setGravity(%grav);
And %obj.getGravity();
Now for a quick test assuming you have applied the above to a clean install of TGE 1.52
and you have run through the GettingStarted.pdf so you can use the GameOne Mod ...
Open \example\GameOne\server\player.cs
Find add the bold text to the following:
Save the file!
Run Torque and start a mission, open the console .... you should see Player gravity now set to -5 a few lines up.
Now go and take a run and jump (no pun intended!) :)
This is an updated version of Dynamic and Individualized Gravity by Chris Evans
and includes changes submitted by many others to the original resource.
This modification will allow you to change the default 'gravity constant' for a player,item or vehicle.
Here is a list of the steps needed to implement the modification.
1. add a new variable 'mGravity' to the ShapeBase class
2. add two new functions/methods to ShapeBase class so we can get and set the new variable
3. add two console methods so we can also get and set the new variable from script
4. add code to initialize gravity in ShapeBase::ShapeBase() and ShapeBase::onNewDataBlock()
5. add a gravity mask flag to the ShapeBaseMasks enum, (this will be used by new code in pack/unpackUpdate)
6. add code to ShapeBase's packUpdate() so changes to the objects gravity get sent over the network
7. add code to ShapeBase's unpackUpdate() so changes to the objects gravity get received over the network
8. update the player and item classes to use the new variable
9. update the vehicle class to use the new variable
10. update the flying vehicle class to use the new variable
11. update the hover vehicle class to use the new variable
12. update the wheeled vehicle class to use the new variable
and here are the details ...... Good luck :)
============================================================================================
Step 1. add a new variable 'mGravity' to the ShapeBase class
Open engine\game\shapeBase.h
Search for 'F32 mEnergy;'
Right above it enter:
F32 mGravity; ///< Gravity for this Object.
============================================================================================
Step 2. add two new functions to the ShapeBase class so we can get and set the new variable
Still in engine\game\shapeBase.h
Search for 'virtual void setEnergyLevel(F32 energy);'
and below it enter:
/// Sets the level of gravity for this object /// @param gravity Level of gravity to assign to this object void setGravity(F32 gravity); /// Returns the amount of gravity for this object F32 getGravity();
Save the file!
Open engine\game\shapeBase.cc
search for 'ConsoleMethod( ShapeBase, setHidden, void, 3, 3, "(bool show)")'
and above it enter:
F32 ShapeBase::getGravity()
{
return mGravity;
}
void ShapeBase::setGravity(F32 gravity)
{
mGravity = gravity;
if(!isGhost())
setMaskBits(GravityMask);
}============================================================================================
Step 3. add two console methods so we can also get and set the new variable from script
Still in shapeBase.cc
At the bottom of the file you will find 'void ShapeBase::consoleInit()'
add the following above it:
ConsoleMethod( ShapeBase, getGravity, F32, 2, 2, "")
{
return object->getGravity();
}
ConsoleMethod( ShapeBase, setGravity, void, 3, 3, "(float gravity)")
{
object->setGravity(dAtof(argv[2]));
}============================================================================================
Step 4. add code to initialize gravity in ShapeBase::ShapeBase() and ShapeBase::onNewDataBlock()
Still in shapeBase.cc
Search for 'ShapeBase::ShapeBase()'
Scroll down and find 'mEnergy = 0;' and above it enter:
mGravity = -20;
Search for 'bool ShapeBase::onNewDataBlock(GameBaseData* dptr)'
Scroll down again until you find "mEnergy = 0;" and above it enter:
mGravity = -20;
Save the file!
============================================================================================
Step 5. add a gravity mask flag to the ShapeBaseMasks enum
Open/Switch to shapeBase.h
Search for 'enum ShapeBaseMasks {"
Inside it you need to add GravityMask
Before we change it it looks like this:
enum ShapeBaseMasks {
NameMask = Parent::NextFreeMask,
DamageMask = Parent::NextFreeMask << 1,
NoWarpMask = Parent::NextFreeMask << 2,
MountedMask = Parent::NextFreeMask << 3,
CloakMask = Parent::NextFreeMask << 4,
ShieldMask = Parent::NextFreeMask << 5,
InvincibleMask = Parent::NextFreeMask << 6,
SkinMask = Parent::NextFreeMask << 7,
SoundMaskN = Parent::NextFreeMask << 8, ///< Extends + MaxSoundThreads bits
ThreadMaskN = SoundMaskN << MaxSoundThreads, ///< Extends + MaxScriptThreads bits
ImageMaskN = ThreadMaskN << MaxScriptThreads, ///< Extends + MaxMountedImage bits
NextFreeMask = ImageMaskN << MaxMountedImages
};Insert a line after the SkinMask line containing this:
GravityMask = Parent::NextFreeMask << 8,
And change 8 to a 9 on the line below it so we end up with an enum that looks like this:
enum ShapeBaseMasks {
NameMask = Parent::NextFreeMask,
DamageMask = Parent::NextFreeMask << 1,
NoWarpMask = Parent::NextFreeMask << 2,
MountedMask = Parent::NextFreeMask << 3,
CloakMask = Parent::NextFreeMask << 4,
ShieldMask = Parent::NextFreeMask << 5,
InvincibleMask = Parent::NextFreeMask << 6,
SkinMask = Parent::NextFreeMask << 7,
[b]GravityMask = Parent::NextFreeMask << 8,[/b]
SoundMaskN = Parent::NextFreeMask << [b]9[/b], ///< Extends + MaxSoundThreads bits
ThreadMaskN = SoundMaskN << MaxSoundThreads, ///< Extends + MaxScriptThreads bits
ImageMaskN = ThreadMaskN << MaxScriptThreads, ///< Extends + MaxMountedImage bits
NextFreeMask = ImageMaskN << MaxMountedImages
};Save the file!
A note here, if you add any other mask bits to this enum make sure
you do not exceed 32. As it stands now (TGE 1.52) :
SceneObject uses 1 bit
GameBase uses 4 bits
axSoundThreads is 4
MaxScriptThreads is 4
MaxMountedImages is 4
this class uses 10
giving us a total of 27 bits used.
============================================================================================
Step 6. add code to ShapeBase's packUpdate() so changes to the objects gravity get sent over the network.
Open/Switch to shapeBase.cc
Search for the function: 'U32 ShapeBase::packUpdate(NetConnection *con, U32 mask, BitStream *stream)'
Find the statement below:
if(!stream->writeFlag(mask & (NameMask | DamageMask | SoundMask |
ThreadMask | ImageMask | CloakMask | MountedMask | InvincibleMask |
ShieldMask | SkinMask)))
return retMask;Add " |GravityMask" just behind "SkinMask" so it looks like the following:
if(!stream->writeFlag(mask & (NameMask | DamageMask | SoundMask |
ThreadMask | ImageMask | CloakMask | MountedMask | InvincibleMask |
ShieldMask | SkinMask [b]| GravityMask[/b])))
return retMask;Scroll down a little in the same function until you find the following:
// Group some of the uncommon stuff together.
if (stream->writeFlag(mask & (NameMask | ShieldMask | CloakMask | InvincibleMask | SkinMask))) {
if (stream->writeFlag(mask & CloakMask)) {Add " |GravityMask" just behind "SkinMask" so it looks like the following:
// Group some of the uncommon stuff together.
if (stream->writeFlag(mask & (NameMask | ShieldMask | CloakMask | InvincibleMask | SkinMask [b]| GravityMask[/b]))){
if (stream->writeFlag(mask & CloakMask)) {Now scroll down and find this:
if (stream->writeFlag(mask & SkinMask)) {
con->packStringHandleU(stream, mSkinNameHandle);
}add this below it:
if (stream->writeFlag(mask & GravityMask)) {
stream->write(mGravity);
}============================================================================================
Step 7. add code to ShapeBase's unpackUpdate() so changes to the objects gravity get received from the network.
Still in shapeBase.cc
Search for the function: 'void ShapeBase::unpackUpdate(NetConnection *con, BitStream *stream)'
Scroll down and find the following:
if (stream->readFlag()) { // SkinMask
StringHandle skinDesiredNameHandle = con->unpackStringHandleU(stream);;
if (mSkinNameHandle != skinDesiredNameHandle) {
mSkinNameHandle = skinDesiredNameHandle;
if (mShapeInstance) {
mShapeInstance->reSkin(mSkinNameHandle);
if (mSkinNameHandle.isValidString()) {
mSkinHash = _StringTable::hashString(mSkinNameHandle.getString());
}
}
}
}and after it enter:
if (stream->readFlag()) { // GravityMask
F32 gravity;
stream->read(&gravity);
setGravity(gravity);
}Save the file!
NOTE: It is important that the data is read from the stream in the correct order, in
our case just after the 'SkinMask' flag and data.
============================================================================================
Step 8. update the player and item classes to use the new variable
Or more to the point scrap the existing variables defined in the player and item classes
Open engine\game\player.h
Find and comment out the following:
static F32 mGravity; ///< Gravity
Open engine\game\player.cc
Find and comment out the following:
F32 Player::mGravity = -20.0f;
Save both files!
--------------------------------------------------------------
Open engine\game\item.h
Find and comment out the following:
static F32 mGravity;
Open engine\game\item.cc
Find and comment out the following:
F32 Item::mGravity = -20;
Save both files!
============================================================================================
Step 9. update the vehicle class to use the new variable
Open engine\game\vehicles\vehicle.cc
Find and comment out 'const F32 sVehicleGravity = -20;'
Search for 'sVehicleGravity' replace with 'mGravity'
you should only find one instance of sVehicleGravity to replace.
Save the file!
============================================================================================
Step 10. update the flying vehicle class to use the new variable
Open engine\game\vehicles\flyingVehicle.cc
Find and comment out 'static F32 sFlyingVehicleGravity = -20;'
Search for 'sFlyingVehicleGravity' replace with 'mGravity'
you should find three instances of sFlyingVehicleGravity to replace.
Save the file!
============================================================================================
Step 11. update the hover vehicle class to use the new variable
Open engine\game\vehicles\hoverVehicle.cc
Find and comment out 'const F32 sHoverVehicleGravity = -20;'
Search for 'sHoverVehicleGravity' replace with 'mGravity'
you should find two instances of sHoverVehicleGravity to replace.
Save the file!
============================================================================================
Step 12. update the wheeled vehicle class to use the new variable
Open engine\game\vehicles\wheeledVehicle.cc
Find and comment out 'static F32 sWheeledVehicleGravity = -20;'
Search for 'sWheeledVehicleGravity' replace with 'mGravity'
you should find two instances of sWheeledVehicleGravity to replace.
Save the file!
============================================================================================
Compile the changes ....
To use the changes from script:
Use %obj.setGravity(%grav);
And %obj.getGravity();
Now for a quick test assuming you have applied the above to a clean install of TGE 1.52
and you have run through the GettingStarted.pdf so you can use the GameOne Mod ...
Open \example\GameOne\server\player.cs
Find add the bold text to the following:
function PlayerBody::onAdd(%this,%obj)
{
// Called when the PlayerData datablock is first 'read' by the engine (executable)
[b]%obj.setGravity(-5.0);
echo("Player gravity now set to " @ %obj.getGravity());[/b]
}Save the file!
Run Torque and start a mission, open the console .... you should see Player gravity now set to -5 a few lines up.
Now go and take a run and jump (no pun intended!) :)
About the author
#2
Just set up a function to change the $pref value and it updates instantly.
09/10/2007 (9:40 pm)
Nevermind. Figured this one out on my own. for those interested you can add this to the start of ShapeBase::ProcessTick.if(!isGhost())
mGravity = Con::getFloatVariable("$Pref::Server::WorldGravity");Just set up a function to change the $pref value and it updates instantly.
#3
of a space based game.
One very tiny small insignificant correction.
In Step 9 and Step 11 in my copies of the source "static F32" was actually "const F32".
Very well written and easy to follow steps. Thanks.
P.S.
If you modify starter.racing\server\car.cs and set the car's gravity to something between
-2 and -5 the result is pretty funny.
10/04/2007 (1:47 pm)
Excellent!! This is just what I needed to continue my investigation into the feasibilityof a space based game.
One very tiny small insignificant correction.
In Step 9 and Step 11 in my copies of the source "static F32" was actually "const F32".
Very well written and easy to follow steps. Thanks.
P.S.
If you modify starter.racing\server\car.cs and set the car's gravity to something between
-2 and -5 the result is pretty funny.
#4
11/10/2007 (10:54 am)
Does this work with TGEA by any chance? =)
#5
11/12/2007 (1:33 pm)
@ Kevin - thanks same here, now corrected above.
#6
12/25/2007 (7:06 pm)
This seems to work fairly well, but I have two dislikes/possible fixes. First, it only works on an pickup-able item if it is not a static shape. Second, on those equipable items, the actual item you see waits for the collision mesh to reach the ground before the visual item follows.
#7
07/19/2008 (1:20 pm)
For anyone wondering, yes this does work with TGEA, I haven't seen any problems yet. And I love this resource.
#8
Nicolas Buquet
www.buquet-net.com/cv/
11/07/2008 (1:55 am)
I think that you should add as step 13, the same step as 9, 10, 11 and 12, but for the rigidShape.cc/.h with the variable 'sWheeledVehicleGravity'.Nicolas Buquet
www.buquet-net.com/cv/
#9
11/25/2008 (11:55 pm)
I want player flying on the sky and collide triggers .but I only made player float on the sky .don't move ?have some easy way.please tell me ,thank you
Torque 3D Owner Ron Nelson
Any idea how I could just set up a function to hit all at once from script?
For example I call a server function to set global gravity and all shapebase object's gravity is set to that variable?