Projectile gravity?
by Tyler Slabinski · in Torque Game Engine · 12/20/2009 (1:03 am) · 22 replies
I am trying to implement a grenade into my game. It works fine, it has a timer, bounces, and does what it's suppose to. The problem is the projectile (grenade) glides too much, and isn't heavy enough.
I tried changing the gravityMod variable from 0.3, to 1, to 10, to 1000, and it makes no difference. I even tried adding a mass variable, but it still doesn't work!
I tried changing the gravityMod variable from 0.3, to 1, to 10, to 1000, and it makes no difference. I even tried adding a mass variable, but it still doesn't work!
#2
Or it won't have ballistic properties - eg: be affectedby gravity
12/20/2009 (11:18 am)
isBallistic=true;
Or it won't have ballistic properties - eg: be affectedby gravity
#3
Thanks for the tip Daniel, I am looking through the source now to find if there is something wrong.
EDIT: Well Daniel, you were right. I just had to change this in projectile.cc:
and replace the '1' with a '10'.
12/20/2009 (12:08 pm)
isBallistic is true.Thanks for the tip Daniel, I am looking through the source now to find if there is something wrong.
EDIT: Well Daniel, you were right. I just had to change this in projectile.cc:
addNamedFieldV(gravityMod, TypeF32, ProjectileData, new FRangeValidator(0, 1));
and replace the '1' with a '10'.
#4
So if you have a very high velocity like 1000 and a gravitymod of 0.7, it won't appear to be affected as much as something with say a velocity of 5 which will fall to your feet quite quickly - which is when you need armingdelay, lifetime and bounce sorting out.
12/20/2009 (12:41 pm)
You shouldn't have to change anything, gravitymod should work from 0.0 (no effect) to 1.0 (full gravity), however depending on how much speed/velocity the projectile has depends on how fast it'll be affected.So if you have a very high velocity like 1000 and a gravitymod of 0.7, it won't appear to be affected as much as something with say a velocity of 5 which will fall to your feet quite quickly - which is when you need armingdelay, lifetime and bounce sorting out.
#5
Full gravity is still too slow. I have a projectile velocity of 60, and a gravity mod of 10.0 now. Before I made the change, gravityMod could only lessen the gravity or make it at 1.0. Now I can make it go 10x normal gravity, but I found a good area around 8.75
Velocity does not change the gravity, if I had a velocity of 0, it would just create the grenade and then make it fall at the speed of the gravityMod.
12/20/2009 (2:58 pm)
@SteveFull gravity is still too slow. I have a projectile velocity of 60, and a gravity mod of 10.0 now. Before I made the change, gravityMod could only lessen the gravity or make it at 1.0. Now I can make it go 10x normal gravity, but I found a good area around 8.75
Velocity does not change the gravity, if I had a velocity of 0, it would just create the grenade and then make it fall at the speed of the gravityMod.
#6
12/20/2009 (6:03 pm)
Does anyone know how to make a projectile NOT face the direction it's going? The grenade flinches each time it bounces, and I would prefer it to look normal.
#7
12/20/2009 (6:25 pm)
Probably another engine mod. There should be some calls to setRenderTransform you can override, not sure where though (probably in interpolateTick). Maybe make it dependent on a datablock flag so you can decide whether a projectile orients towards its velocity or not.
#8
Do I need to do something with xform?
12/20/2009 (11:32 pm)
I don't know enough about the engine to do that, but I looked in the file and found this:setRenderTransform(xform);
Do I need to do something with xform?
#9
That's all I got so far, any help?
12/20/2009 (11:56 pm)
Ok, I don't know much about the inside of the engine, but I have done this:[b]projectile.h[/b] public: // See if it's a grenade. bool isNotGrenade;
[b]projectile.cc[/b]
[b]ProjectileData::ProjectileData()[/b]
{
// Projectile defaults to not being grenade.
isNotGrenade = true;
}
[b]void ProjectileData::initPersistFields()[/b]
{
// Allow us to check isNotGrenade in script.
addNamedField(isNotGrenade, TypeBool, ProjectileData);
}That's all I got so far, any help?
#10
RE xform, here's the whole code section:
12/21/2009 (8:51 am)
I would recommend doing a 'find all references' on isBallistic - that will show you precisely what you need to do to add a new boolean member to the datablock.RE xform, here's the whole code section:
Point3F dir = mCurrVelocity;
if(dir.isZero())
dir.set(0,0,1);
else
dir.normalize();
MatrixF xform(true);
xform = MathUtils::createOrientFromDir(dir);
xform.setPosition(interpPos);
setRenderTransform(xform);The critical line is where xform is set with createOrientFromDir - that basically creates a matrix whose forward vector is dir, which is the projectile's velocity.
#11
Projectile.cc
Well that's what I've got, but I keep getting these errors on compile:
I thought I did declare it in the header file.
12/21/2009 (11:12 pm)
Projectile.h:public: [b]// See if it's a grenade. bool isGrenade; bool mGrenade;[/b]
Projectile.cc
ProjectileData::ProjectileData()
{
isBallistic = false;
[b]isGrenade = false;[/b]
velInheritFactor = 1.0;
}void ProjectileData::initPersistFields
{
addNamedField(waterLightColor, TypeColorF, ProjectileData);
[b]addNamedField(isGrenade, TypeBool, ProjectileData);[/b]
addNamedField(isBallistic, TypeBool, ProjectileData);void ProjectileData::packData(BitStream* stream)
{
stream->writeRangedU32(fadeDelay, 0, Projectile::MaxLivingTicks);
[b]stream->writeFlag(isGrenade);[/b]
if(stream->writeFlag(isBallistic))
{
stream->write(gravityMod);
stream->write(bounceElasticity);
stream->write(bounceFriction);
}
}void ProjectileData::unpackData(BitStream* stream)
{
fadeDelay = stream->readRangedU32(0, Projectile::MaxLivingTicks);
[b]isGrenade = stream->readFlag();[/b]
isBallistic = stream->readFlag();
}void Projectile::processTick(const Move* move)
{
Point3F newPosition;
[b]if(mDataBlock->isGrenade)
{
mGrenade = true;
}[/b]
oldPosition = mCurrPosition;
}void Projectile::interpolateTick(F32 delta)
{
xform.setPosition(interpPos);
[b]if (mGrenade == 0)
{
setRenderTransform(xform);
}[/b]
// fade out the projectile imageWell that's what I've got, but I keep getting these errors on compile:
/Applications/Torque Game Engine 1.5.2 SDK/Torque SDK/xcode/../engine/game/projectile.cc:780: error: 'mGrenade' was not declared in this scope /Applications/Torque Game Engine 1.5.2 SDK/Torque SDK/xcode/../engine/game/projectile.cc:943: error: 'mGrenade' was not declared in this scope /Applications/Torque Game Engine 1.5.2 SDK/Torque SDK/xcode/../engine/game/projectile.cc:780: error: 'mGrenade' was not declared in this scope /Applications/Torque Game Engine 1.5.2 SDK/Torque SDK/xcode/../engine/game/projectile.cc:943: error: 'mGrenade' was not declared in this scope
I thought I did declare it in the header file.
#12
Also, I wouldn't set mGrenade in processTick; it's not much of a waste, but it doesn't make much sense for a static variable to be set every tick. Projectile::onAdd would probably be the best place.
12/22/2009 (7:58 am)
1. public: 2. 3. [b]// See if it's a grenade. 4. bool isGrenade; 5. bool mGrenade;[/b]Is that all inside the datablock declaration? It should look something like
struct ProjectileData : public GamebaseData {
...
};You need to move mGrenade out of the datablock class and into the Projectile class itself.Also, I wouldn't set mGrenade in processTick; it's not much of a waste, but it doesn't make much sense for a static variable to be set every tick. Projectile::onAdd would probably be the best place.
#13
And just put in:
That means I didn't need mGrenade at all. I compiled it and it works too.
The problem now, is that even though the grenade doesn't spin and spaz out when it touches the ground, it still has a spaz. As if it is still bouncing constantly. I tried turning off the elasticity, but that doesn't work, and I tried it on a perfectly flat surface, but that doesn't work either.
How can I make it stop bouncing?
12/22/2009 (5:29 pm)
I fixed that, apparently I could get rid of:void Projectile::processTick(const Move* move)
{
Point3F newPosition;
[b]if(mDataBlock->isGrenade)
{
mGrenade = true;
}[/b]
oldPosition = mCurrPosition;
}And just put in:
if(!mDataBlock->isGrenade)
{
setRenderTransform(xform);
}That means I didn't need mGrenade at all. I compiled it and it works too.
The problem now, is that even though the grenade doesn't spin and spaz out when it touches the ground, it still has a spaz. As if it is still bouncing constantly. I tried turning off the elasticity, but that doesn't work, and I tried it on a perfectly flat surface, but that doesn't work either.
How can I make it stop bouncing?
#14
12/22/2009 (5:36 pm)
Maybe getting rid of the call to setRenderTransform entirely isn't the way to go - you just need to stop the matrix from being oriented in the direction. Maybe instead, put the if-clause around this line:xform = MathUtils::createOrientFromDir(dir);
Quote:That means I didn't need mGrenade at all.Good thinking! I didn't know what else you wanted to use mGrenade for. As a general rule, you only need a member of the object (Projectile, as opposed to ProjectileData) if you want to be able to change it over the course of the object's life.
#15
Ok, I did that but it doesn't make it different than when I put it around the setRenderTransform.
Thanks, I will remember that. I thought that you could only connect scripts to certain functions, I didn't know I was allowed to get the mDataBlock-> function in that particular function. Hence the reason I used mGrenade, but I forgot that the scope of mGrenade would only be true in the processTick function, and I can't use a return for it.
Though how would I get it to stop constantly bouncing? I will post a video with what I mean.
EDIT: I can't post a video, the frames aren't fast enough in the video to see the bouncing.
12/22/2009 (5:50 pm)
Quote:Maybe getting rid of the call to setRenderTransform entirely isn't the way to go - you just need to stop the matrix from being oriented in the direction. Maybe instead, put the if-clause around this line:
Ok, I did that but it doesn't make it different than when I put it around the setRenderTransform.
Quote:Good thinking! I didn't know what else you wanted to use mGrenade for. As a general rule, you only need a member of the object (Projectile, as opposed to ProjectileData) if you want to be able to change it over the course of the object's life.
Thanks, I will remember that. I thought that you could only connect scripts to certain functions, I didn't know I was allowed to get the mDataBlock-> function in that particular function. Hence the reason I used mGrenade, but I forgot that the scope of mGrenade would only be true in the processTick function, and I can't use a return for it.
Though how would I get it to stop constantly bouncing? I will post a video with what I mean.
EDIT: I can't post a video, the frames aren't fast enough in the video to see the bouncing.
#16
EDIT: I don't see any jittering if I just comment out the line with createOrientFromDir. have you made any other changes?
12/22/2009 (5:56 pm)
I'm not sure what's causing that problem... I'll try and reproduce it on my side.EDIT: I don't see any jittering if I just comment out the line with createOrientFromDir. have you made any other changes?
#17
I have implemented a few other resources though, although I doubt they are the cause.
Here are my files:
www.mediafire.com/?1yiiczzqy0y
12/22/2009 (5:57 pm)
EDIT2: No, I haven't made any other changes to projectile.cc or .hI have implemented a few other resources though, although I doubt they are the cause.
Here are my files:
www.mediafire.com/?1yiiczzqy0y
#18
EDIT: or just email them to me or something.
12/22/2009 (6:06 pm)
Any chance you could just upload the projectile.cc/h? Slow connection means 43MB is going to take an hour to DL :P.EDIT: or just email them to me or something.
#19
12/22/2009 (6:09 pm)
Sure, I sent an email.
#20
I *think* the problem is caused because the projectile never really does stop bouncing. Even when it's 'resting' on the ground, it's actually 0.05 units above it (oldPosition = rInfo.point + rInfo.normal * 0.05;). Each tick, gravity pulls the projectile down, and it gets bumped back upwards.
EDIT: Not sure how you would go about fixing that. Maybe make a check before you apply gravity: if we're a grenade and our velocity is already 0, don't add gravity.
Not sure what other problems you'd run into with that, but it seems like a start.
This really comes about because Projectiles weren't designed to have proper physics. I'm going to be using RigidShapes for my grenades, to get a big of rigid physics action happening. For Tribes-style grenades which travel fast and only bounce once or twice, the stock code works well - just not if you expect them to come to rest.
12/22/2009 (6:31 pm)
I see what you mean now... well, if it's any consolation it seems to be a problem in stock TGE as well :P.I *think* the problem is caused because the projectile never really does stop bouncing. Even when it's 'resting' on the ground, it's actually 0.05 units above it (oldPosition = rInfo.point + rInfo.normal * 0.05;). Each tick, gravity pulls the projectile down, and it gets bumped back upwards.
EDIT: Not sure how you would go about fixing that. Maybe make a check before you apply gravity: if we're a grenade and our velocity is already 0, don't add gravity.
Not sure what other problems you'd run into with that, but it seems like a start.
This really comes about because Projectiles weren't designed to have proper physics. I'm going to be using RigidShapes for my grenades, to get a big of rigid physics action happening. For Tribes-style grenades which travel fast and only bounce once or twice, the stock code works well - just not if you expect them to come to rest.
Torque Owner Daniel Buckmaster
T3D Steering Committee
But also remember that mass is not proportional to acceleration due to gravity. Heavy objects fall at the same speed as light ones. You might want to look at implementing drag in the projectile class instead.
Or, just increase gravity and don't worry about the physics behind it as long as it looks right ;)