Modifying Gravity
by Kamal Syed · in Torque Game Engine · 12/09/2003 (9:20 pm) · 20 replies
Is there anywhere in the script files or even in the engine files where the force of gravity for RW and Racing can be modified?
Thanks.
Thanks.
About the author
#2
12/10/2003 (8:33 am)
I have searched the entire Torque module and found four classes where mGravity is declared. But nowhere did I find an assignment for mGravity. Can anyone give more specific details?
#3
12/10/2003 (8:50 am)
A quick grep turned up the below in engine\game\player.cc at line #733 (using the current HEAD):F32 Player::mGravity = -20;
#4
12/14/2003 (1:42 pm)
OK, that worked, except I believe this only applies to RealmWars, and not racing. Can anyone verify or refute this? If there is a way to make it apply to Racing, can anyone tell me how?
#5
Flying vehicles :
Wheeled vehicles :
Bit odd that these are all seperate. Perhaps it would be a good idea making a nice Gravity management system? =)
12/14/2003 (1:51 pm)
For vehicles :const F32 sVehicleGravity = -20;(vehicle.cc)
Flying vehicles :
static F32 sFlyingVehicleGravity = -20;(flyingVehicle.cc)
Wheeled vehicles :
static F32 sFlyingVehicleGravity = -20;(wheeledVehicle.cc)
Bit odd that these are all seperate. Perhaps it would be a good idea making a nice Gravity management system? =)
#6
Perhaps we could setup an object that would have the gravity accelleration set in it? Instead of having this:
we could have:
Change all of the above items listed by James to this.
"gravity." is the "object" of class Gravity. You'll have to setup some sort of object class that will hold the variable. Anyone provide input as to whether this would work as a global variable: (This has not been tested FYI this is entirely a Theory)
When you create the object you'll have to do something like this:
This can be done anywhere in the game. I would recommend setting this in the inital game code start. Somewhere before the console is initialized (or after if you want to also have it display the gravity). More importantly you could also use the "setGravity()" command in your .mis file. Just stick it somewhere at the bottom like this:
Technically you could even set this to a positive number and make a "repulsor Gravity" where it makes you fly rather forcefully.
Remember this is mainly theory. My syntax may not be 100% accurate. I plan on testing this myself later. I've been mulling around the forums and code for how they have the gravity setup in the TGE. Apparently this is the WORST part of the code since it is not a global variable where it should be acting on all objects equally anyway.
Maybe this post will get someone else's creative juices flowing and make the actual resource post. This IS something that is needed BADLY as there are plenty of projects out there that would like to be able to manipulate the laws of gravity. If I recall correctly, Tribes 2 (The game this engine was used for initially) already had this setup as a global variable...so how come TGE's HEAD today doesn't?? Maybe this is a question for the GG folks more than the community?
________________
GokouZWAR
02/02/2004 (5:31 pm)
I would think that would be a good idea. If it's not already IN the HEAD version. Someone should suggest this...hint hint...maybe I'll try to make one myself. Anyone have an idea of setting up a "GLOBAL VARIABLE" that will set this? The fact that this is set through the engine and not the scripting code makes it rather difficult to edit.Perhaps we could setup an object that would have the gravity accelleration set in it? Instead of having this:
static F32 sFlyingVehicleGravity = -20;
we could have:
static F32 sFlyingVehicleGravity = gravity.getGravity();
Change all of the above items listed by James to this.
"gravity." is the "object" of class Gravity. You'll have to setup some sort of object class that will hold the variable. Anyone provide input as to whether this would work as a global variable: (This has not been tested FYI this is entirely a Theory)
class Gravity
{
private:
int gravity = -20; //sets gravitational pull (or fall of all objects)
public:
int getGravity(); //control function that will let you retrieve the gravity from anywhere in the game code
int setGravity(); //control function that will let you set the gravity
};
//constructor
Gravity::Gravity(int grav)
{
int Gravity = grav;
}
void Gravity::setGravity(int Grav)
{
Gravity = grav; //sets the grav to gravity
}
int Gravity::getGravity()
{
return Gravity; //returns the gravity to wherever you need it to go
}When you create the object you'll have to do something like this:
Gravity Gravity(-20);
This can be done anywhere in the game. I would recommend setting this in the inital game code start. Somewhere before the console is initialized (or after if you want to also have it display the gravity). More importantly you could also use the "setGravity()" command in your .mis file. Just stick it somewhere at the bottom like this:
Gravity.setGravity(0); //null gravity or Gravity.setGravity(-20); //normal Gravity
Technically you could even set this to a positive number and make a "repulsor Gravity" where it makes you fly rather forcefully.
Remember this is mainly theory. My syntax may not be 100% accurate. I plan on testing this myself later. I've been mulling around the forums and code for how they have the gravity setup in the TGE. Apparently this is the WORST part of the code since it is not a global variable where it should be acting on all objects equally anyway.
Maybe this post will get someone else's creative juices flowing and make the actual resource post. This IS something that is needed BADLY as there are plenty of projects out there that would like to be able to manipulate the laws of gravity. If I recall correctly, Tribes 2 (The game this engine was used for initially) already had this setup as a global variable...so how come TGE's HEAD today doesn't?? Maybe this is a question for the GG folks more than the community?
________________
GokouZWAR
#7
_____________________________
GokouZWAR
02/02/2004 (7:03 pm)
As expected this didn't really work very well. My main problem is the syntax of this. I can't get it to compile in VC++.net. If anyone can help get the code proper for me please do so. Or if someone knows an easier way to what I'm trying to do. Please post!_____________________________
GokouZWAR
#9
I haven't coded this and haven't thought over the gravity concept, but the above code will not compile.
As mentioned by Tyler, he did not post real code.
If you want to compile the above stuff, use the following.
(Haven't comiled it myself, so post the error message, if you have questions.)
-- Markus
02/02/2004 (11:43 pm)
Hi guys.I haven't coded this and haven't thought over the gravity concept, but the above code will not compile.
As mentioned by Tyler, he did not post real code.
If you want to compile the above stuff, use the following.
(Haven't comiled it myself, so post the error message, if you have questions.)
// This goes to gravity.h
class Gravity
{
private:
int mGravity = -20; //sets gravitational pull (or fall of all objects)
public:
Gravity(int initialGrav);
int getGravity(); //control function that will let you retrieve the gravity from anywhere in the game code
int setGravity(); //control function that will let you set the gravity
};
// This goes to gravity.cc
// Constructor
Gravity::Gravity(int grav)
{
int mGravity = grav;
}
// Setter
void Gravity::setGravity(int grav)
{
mGravity = grav; //sets the grav to gravity
}
// Getter
int Gravity::getGravity()
{
return mGravity; //returns the gravity to wherever you need it to go
}-- Markus
#10
______________________
GokouZWAR
02/03/2004 (7:27 am)
Ok, so the class definition goes to gravity.h and the class functions go in gravity.cc? I'll test this and send any error messages. What about declaring the object? I get a compile error stating the object has not been declared or identified whenever I try to setup the object. Also when I try to set the mGravity to grav.getGravity() it doesn't like it. It says that everything before the .getGravity needs a ; or something...I'll paste the actual errors if I can in here.______________________
GokouZWAR
#11
Gravity.h:
Gravity.cc
Do I have that much correct?
Then I added in main.cc
No errors from that, as a matter of fact, VS.net identified the class gravity for me and the constructor. So I know that it understands what I'm typing there and therefore I assume it's called properly.
Next:
I edited this code in the player.cc file:
Now, when I type this in, VS.Net states: "Type of expression before the . or -> is not a class, struct, or union". I'm probably calling the object incorretly...
_______________________
GokouZWAR
02/03/2004 (7:35 am)
Ok here's what I have in the program now:Gravity.h:
class Gravity
{
private:
int mGravity = -20; //sets gravitational pull (or fall of all objects)
public:
Gravity(int initialGrav);
int getGravity(); //control function that will let you retrieve the gravity from anywhere in the game code
int setGravity(); //control function that will let you set the gravity
};Gravity.cc
#include <Gravity.h>
Gravity::Gravity(int grav)
{
int mGravity = grav;
}
// Setter
void Gravity::setGravity(int grav)
{
mGravity = grav; //sets the grav to gravity
}
// Getter
int Gravity::getGravity()
{
return mGravity; //returns the gravity to wherever you need it to go
}Do I have that much correct?
Then I added in main.cc
#include "core/fileStream.h" #include "dgl/gTexManager.h" #include "dgl/gFont.h" [b]#include "game/gravity.h" [/b] //include Gravity header file #include "console/console.h" #include "console/simBase.h" #include "gui/guiCanvas.h"As well as:
/// Initalize game, run main.cs startup script
bool initGame()
{
printf("InitGame Ran here..");
[b]grav Gravity(-20);[/b]
Con::setFloatVariable("Video::texResidentPercentage", -1.0f);
Con::setIntVariable("Video::textureCacheMisses", -1);No errors from that, as a matter of fact, VS.net identified the class gravity for me and the constructor. So I know that it understands what I'm typing there and therefore I assume it's called properly.
Next:
I edited this code in the player.cc file:
//---------------------------------------------------------------------------- IMPLEMENT_CO_NETOBJECT_V1(Player); F32 Player::mGravity = [b]grav.getGravity();[/b] //used to be -20 //----------------------------------------------------------------------------
Now, when I type this in, VS.Net states: "Type of expression before the . or -> is not a class, struct, or union". I'm probably calling the object incorretly...
_______________________
GokouZWAR
#12
Because the class is called Gravity not grav.
02/03/2004 (7:53 am)
Shouldnt you have intiailised/called the constructor with Gravity grav(-20);
Because the class is called Gravity not grav.
#13
Relative code lines from above for reference:
player.cc:
gravity.cc
gravity.h
Any ideas on what these are meaning exactly. Remember I'm using VS.Net. - Edit: Let me ask you this, do you need to have the object declared in player.cc instead of main.cc? It says it doesn't know what "grav" is so I'm thinking that's what I'm doing wrong at this point. Remember we're looking to have a global variable. If I have to declare it in each of the object type variables, how is this going to be globally used? We need 1 object/variable that has the gravity constant in it.
_____________________
GokouZWAR
02/03/2004 (7:55 am)
Ok here's the errors that come up. Yes the object declaration was backwards. Thanx :P I did fix a few items from the above code. And here's what's left:------ Build started: Project: Torque, Configuration: Release Win32 ------
Compiling...
player.cc
\torque\engine\game\player.cc(733) : error C2065: 'grav' : undeclared identifier
\torque\engine\game\player.cc(733) : error C2228: left of '.getGravity' must have class/struct/union type
gravity.cc
\torque\engine\game\gravity.cc(9) : error C2511: 'void Gravity::setGravity(int)' : overloaded member function not found in 'Gravity'
../engine\game\gravity.h(2) : see declaration of 'Gravity'
Build log was saved at "file://e:\torque\engine\out.VC6.RELEASE\BuildLog.htm"
Torque - 3 error(s), 0 warning(s)Relative code lines from above for reference:
player.cc:
733 F32 Player::mGravity = grav.Gravity();
gravity.cc
1 #include "game/gravity.h"
2
3 Gravity::Gravity(int grav)
4 {
5 int mGravity = grav;
6 }
7 // Setter
8 void Gravity::setGravity(int grav)
9 {
10 mGravity = grav; //sets the grav to gravity
11 }
12 // Getter
13 int Gravity::getGravity()
14 {
15 return mGravity; //returns the gravity to wherever you need it to go
16 }gravity.h
1 class Gravity
2 {
3 private:
4 int mGravity; //sets gravitational pull (or fall of all objects)
5 public:
6 Gravity(int initialGrav);
7 int getGravity(); //control function that will let you retrieve the gravity from anywhere in the game code
8 int setGravity(); //control function that will let you set the gravity
9 };Any ideas on what these are meaning exactly. Remember I'm using VS.Net. - Edit: Let me ask you this, do you need to have the object declared in player.cc instead of main.cc? It says it doesn't know what "grav" is so I'm thinking that's what I'm doing wrong at this point. Remember we're looking to have a global variable. If I have to declare it in each of the object type variables, how is this going to be globally used? We need 1 object/variable that has the gravity constant in it.
_____________________
GokouZWAR
#14
This can be set in your .mis file if you want. But it would be easier to have this setup in your main game initialization code instead. Use the "setGravity(X)" in your MIS file. If you decide not to use the latter you'll have to remove the Gravity Grav = new Gravity(X) from your game initialization.
I'll still have to test this, but if anyone can verify that this would work please post. I'll have to test it late tonight so for the sake of others I'd hope you could confirm this if you know and you don't see an update to this. Assuming I can get this to work, I'll setup a resource for those who whould like to manipulate gravity values so that we can have this in the game.
A problem you'll run into is that if you use the function setGravity() in mid running you'll also have to have another function in there to reset the gravity value for all objects currently in the active mission. This is NOT intended to be used dynamically where you have one gravity value for half a mission then for some reason all the gravity goes away! This will however allow you to set varying gravity values for different mission maps, even allow you to have random gravity each time the map loads. It would be as simple as calling the gravity object in your .mis file. Just remember that if you have vehicles or something that are very sensitive to damage, setting gravity values of very high value can have rather fatal results over even the smallest bumps. Remember also that having a positive value as gravity will make you float. Setting the value to 1 makes you fly up at about 1 meter per second or so. (I haven't actually calculated it but it seems to be a pretty good guess) The speed of gravitational pull on earth is 32 meters per second per second. I think -20 is actually heavier than earth, I would like to think we're at like -16 or something.
Another problem you'll run into is not just the gravity but when your player is not in contact with the ground he actually doesn't do anything because of the lack of ability to move. I know there's a resource on the site that tells you how to setup the control factors when your player is jumping. When your player is not in contact with the ground, he's considered jumping so all you'll have to do to control your player's directional movement in zero gravity is to follow that resource. I'll look it up and post it with my resource so that it will allow you to control your player as well.
I will also have to figure out the exact methods of reading the simGroups and looping through each object to look for the gravity value and change it when setGravity() is called. It isn't that hard to do I've done stuff similar to that in Tribes 2. I'm sure it's the same here but this will also be included in the resource.
________________________
GokouZWAR
02/03/2004 (9:39 am)
Ok I think I figured out what went wrong here! What needs to happen is not only just Gravity grav(-20); but you also need to use the constructor. Here's what I mean:Gravity grav = new Gravity(-20);
This can be set in your .mis file if you want. But it would be easier to have this setup in your main game initialization code instead. Use the "setGravity(X)" in your MIS file. If you decide not to use the latter you'll have to remove the Gravity Grav = new Gravity(X) from your game initialization.
I'll still have to test this, but if anyone can verify that this would work please post. I'll have to test it late tonight so for the sake of others I'd hope you could confirm this if you know and you don't see an update to this. Assuming I can get this to work, I'll setup a resource for those who whould like to manipulate gravity values so that we can have this in the game.
A problem you'll run into is that if you use the function setGravity() in mid running you'll also have to have another function in there to reset the gravity value for all objects currently in the active mission. This is NOT intended to be used dynamically where you have one gravity value for half a mission then for some reason all the gravity goes away! This will however allow you to set varying gravity values for different mission maps, even allow you to have random gravity each time the map loads. It would be as simple as calling the gravity object in your .mis file. Just remember that if you have vehicles or something that are very sensitive to damage, setting gravity values of very high value can have rather fatal results over even the smallest bumps. Remember also that having a positive value as gravity will make you float. Setting the value to 1 makes you fly up at about 1 meter per second or so. (I haven't actually calculated it but it seems to be a pretty good guess) The speed of gravitational pull on earth is 32 meters per second per second. I think -20 is actually heavier than earth, I would like to think we're at like -16 or something.
Another problem you'll run into is not just the gravity but when your player is not in contact with the ground he actually doesn't do anything because of the lack of ability to move. I know there's a resource on the site that tells you how to setup the control factors when your player is jumping. When your player is not in contact with the ground, he's considered jumping so all you'll have to do to control your player's directional movement in zero gravity is to follow that resource. I'll look it up and post it with my resource so that it will allow you to control your player as well.
I will also have to figure out the exact methods of reading the simGroups and looping through each object to look for the gravity value and change it when setGravity() is called. It isn't that hard to do I've done stuff similar to that in Tribes 2. I'm sure it's the same here but this will also be included in the resource.
________________________
GokouZWAR
#15
Problems:
- You cannot inititalize the member function in the declaration.
(haha no Java here ;) )
- The setter function was declared wrong.
- The comment were wrong, since the access a single instance only
- In the constructer we scoped the meber by a local variable
(hoho copy paste error)
- A extern declaration for a global access was missing.
Haven't compiled this one, too.
But this will fix most of the problems of the previous posts.
That's for the theory, but the above code does not take network issues into account.
-- Markus
02/03/2004 (11:48 am)
Ok. There are some typos in the code:Problems:
- You cannot inititalize the member function in the declaration.
(haha no Java here ;) )
- The setter function was declared wrong.
- The comment were wrong, since the access a single instance only
- In the constructer we scoped the meber by a local variable
(hoho copy paste error)
- A extern declaration for a global access was missing.
// In gravity.h
#ifndef _GRAVITY_H_
#define _GRAVITY_H_
class Gravity
{
protected:
// Main gravity value
int mGravity;
public:
Gravity(int initialGrav);
int getGravity(); // Getter for gravitiy value
void setGravity(int nNewGrav); // Setter for gravitiy value
}
extern Gravity g_Gravity;
#endif // _GRAVITY_H_
// in gravity.cc
#include <Gravity.h>
Gravity::Gravity(int initialGrav)
{
// Init member
mGravity = initialGrav;
}
// Setter
void Gravity::setGravity(int grav)
{
mGravity = grav;
//sets the grav to gravity
}
// Getter
int Gravity::getGravity()
{
return mGravity;
//returns the gravity to wherever you need it to go
}
// Single gravity instance.
Gravity g_Gravity(-20);
// Everywhere you need the gravity do the following
// #include <gravity.h>
// int nGrav = g_Gravity.getGravity();Haven't compiled this one, too.
But this will fix most of the problems of the previous posts.
That's for the theory, but the above code does not take network issues into account.
-- Markus
#16
Everytime I call the g_Gravity.getGravity it says that g_Gravity is an undefined value or something...
Gravity.h
gravity.cc
I do have some problems seperating my java from C++ though :P Remember i'm using Visual Studio .Net I hope there's no real differences...but I know some things changed with .net from v6.0
_____________________
GokouZWAR
02/04/2004 (11:39 am)
Well I think my problem is no longer calling the stuff, but actually getting the g_Gravity thing to be accepted anywhere I need it. My problem is defining the object where it can be retrieved from anywhere in the program.Everytime I call the g_Gravity.getGravity it says that g_Gravity is an undefined value or something...
------ Build started: Project: Torque, Configuration: Release Win32 ------ Compiling... console.cc Linking... player.obj : error LNK2019: unresolved external symbol "class Gravity g_Gravity" (?g_Gravity@@3VGravity@@A) referenced in function "public: __thiscall Player::Player(void)" (??0Player@@QAE@XZ) ../example/torqueDemo.exe : fatal error LNK1120: 1 unresolved externals Build log was saved at "file://e:\torque\engine\out.VC6.RELEASE\BuildLog.htm" Torque - 2 error(s), 0 warning(s) ---------------------- Done ----------------------Something new to ponder over...?
Gravity.h
#ifndef _GRAVITY_H_
#define _GRAVITY_H_
class Gravity
{
protected: // Main gravity value
int mGravity;
public:
Gravity(int initialGrav);
int getGravity(); // Getter for gravitiy value
void setGravity(int nNewGrav); // Setter for gravitiy value
};
extern Gravity g_Gravity;
#endif // _GRAVITY_H_gravity.cc
#include <game/Gravity.h>
Gravity::Gravity(int initialGrav)
{
// Init member
mGravity = initialGrav;
}
// Setter
void Gravity::setGravity(int grav)
{
mGravity = grav;
//sets the grav to gravity
}
// Getter
int Gravity::getGravity()
{
return mGravity;
//returns the gravity to wherever you need it to go
}player.ccPlayer::Player()
{
printf("Player initialized");
mTypeMask |= PlayerObjectType;
[b]int grav = g_Gravity.getGravity();
mGravity = grav;[/b]
delta.pos = mAnchorPoint = Point3F(0,0,100);
delta.rot = delta.head = Point3F(0,0,0);player.cc#include "game/missionArea.h" #include "game/fx/particleEngine.h" #include "game/fx/splash.h" #include "game/fx/cameraFXMgr.h" [b]#include <game/gravity.h>[/b] //----------------------------------------------------------------------------
I do have some problems seperating my java from C++ though :P Remember i'm using Visual Studio .Net I hope there's no real differences...but I know some things changed with .net from v6.0
_____________________
GokouZWAR
#17
Add
to the end of your gravity.cc
-- Markus
02/04/2004 (10:53 pm)
In you above posted gravity.cc you are missing the single Gravity instance that is declared as extern in your gravity.hAdd
// Single gravity instance. Gravity g_Gravity(-20);
to the end of your gravity.cc
-- Markus
#18
Gravity.cc:
I think I'm going to start a new thread for this topic as we're way off track as far as what was originally asked in here...I'll call it "Setting a Global Gravity Controller" or something...
Link to other post:
www.garagegames.com/mg/forums/result.thread.php?qt=15999
______________
GokouZWAR
02/05/2004 (6:56 am)
So I should have:Gravity.cc:
#include <game/Gravity.h>
Gravity::Gravity(int initialGrav)
{
// Init member
mGravity = initialGrav;
}
// Setter
void Gravity::setGravity(int grav)
{
mGravity = grav; //sets the grav to gravity
}
// Getterint Gravity::getGravity()
{
return mGravity; //returns the gravity to wherever you need it to go
}
// Single gravity instance.
Gravity g_Gravity(-20);That won't work...you can't call an object from within the object you're trying to call...? I assume you mean I need this in the game code (probably just before the main game loop.I think I'm going to start a new thread for this topic as we're way off track as far as what was originally asked in here...I'll call it "Setting a Global Gravity Controller" or something...
Link to other post:
www.garagegames.com/mg/forums/result.thread.php?qt=15999
______________
GokouZWAR
#19
The file gravity.cc is not the object, just a source file.
Declaring an instance of your class at the end of gravity.cc is totally legal.
If your code does not compile, it may be an other problem.
Post your error messages.
-- Markus
02/09/2004 (3:05 am)
No.The file gravity.cc is not the object, just a source file.
Declaring an instance of your class at the end of gravity.cc is totally legal.
If your code does not compile, it may be an other problem.
Post your error messages.
-- Markus
#20
________________
GokouZWAR
02/09/2004 (8:40 am)
See the other post Markus... We did it a better way. :P________________
GokouZWAR
Torque Owner Mark Harmon