Camera Shake on Weapon Fire
by Tim Heldna · 02/23/2006 (9:44 am) · 31 comments
This will not work across a network
Updated 25th February 2006
Refer this forum thread for some background info. Also, within that thread you will find some code that will push the players view / arms in any direction you want (recoil effect) & some other related weapon goodies. Thanks to myself, Josh Moore, C2, Max Thomas & Matt "Mr. Pig" Razzafor.
Open up engine/game/projectile.cc
After the line #include "sim/decalManager.h" add this
In ProjectileData::ProjectileData() before the last closing brace add this
In void ProjectileData::initPersistFields() before the last closing brace add this
Replace F32 Projectile::getUpdatePriority() with this
That's it for projectile.cc, now open engine/game/projectile.h
After the line ColorF waterLightColor; add this
Ok, that's the head code out of the way, recompile now.
In each of your weapon scripts, within the datablock ProjectileData() section add this to enable camera shake
Play around with those values and season to taste
If you don't want camera shake on your weapon you MUST add shakeCamera = false; to each of your weapons datablock ProjectileData() sections
Enjoy! As always all comments, feedback, suggestions for improvement are welcome. If anyone can figure out how to get this working across the network please post.
As it is, across a network the following happens:
- If you have created the game no problems
- If you have joined the game camera shake does not work. Everytime the host fires a weapon with camera shake enabled your screen will shake.
April 24th 2006:
Just found out that Matt "Mr. Pig" Razza is working on a more advanced recoil system and is working on using this resource for the camera shake, and hopefully getting it to work across a network. You may want to check it out...
Advanced Weapon Recoil System - Weapon Bounce
Updated 25th February 2006
Refer this forum thread for some background info. Also, within that thread you will find some code that will push the players view / arms in any direction you want (recoil effect) & some other related weapon goodies. Thanks to myself, Josh Moore, C2, Max Thomas & Matt "Mr. Pig" Razzafor.
Open up engine/game/projectile.cc
After the line #include "sim/decalManager.h" add this
#include "game/fx/cameraFXMgr.h"
In ProjectileData::ProjectileData() before the last closing brace add this
shakeCamera = true; camShakeFreq.set( 10.0, 10.0, 10.0 ); camShakeAmp.set( 1.0, 1.0, 1.0 ); camShakeDuration = 1.5; camShakeFalloff = 10.0;
In void ProjectileData::initPersistFields() before the last closing brace add this
addField("shakeCamera", TypeBool, Offset(shakeCamera, ProjectileData));
addField("camShakeFreq", TypePoint3F, Offset(camShakeFreq, ProjectileData));
addField("camShakeAmp", TypePoint3F, Offset(camShakeAmp, ProjectileData));
addField("camShakeDuration", TypeF32, Offset(camShakeDuration, ProjectileData));
addField("camShakeFalloff", TypeF32, Offset(camShakeFalloff, ProjectileData));Replace F32 Projectile::getUpdatePriority() with this
F32 Projectile::getUpdatePriority(CameraScopeQuery *camInfo, U32 updateMask, S32 updateSkips)
{
F32 ret = Parent::getUpdatePriority(camInfo, updateMask, updateSkips);
bool shakeCamera = false;
if (mDataBlock->shakeCamera && isServerObject())
shakeCamera = true;
if (shakeCamera == true)
{
CameraShake *camShake = new CameraShake;
camShake->setDuration( mDataBlock->camShakeDuration );
camShake->setFrequency( mDataBlock->camShakeFreq );
VectorF shakeAmp = mDataBlock->camShakeAmp;
camShake->setAmplitude( shakeAmp );
camShake->setFalloff( mDataBlock->camShakeFalloff );
camShake->init();
gCamFXMgr.addFX( camShake );
}
else
{
shakeCamera = false;
}
// if the camera "owns" this object, it should have a slightly higher priority
if(mSourceObject == camInfo->camera)
return ret + 0.2;
return ret;
}That's it for projectile.cc, now open engine/game/projectile.h
After the line ColorF waterLightColor; add this
bool shakeCamera; VectorF camShakeFreq; VectorF camShakeAmp; F32 camShakeDuration; F32 camShakeRadius; F32 camShakeFalloff;
Ok, that's the head code out of the way, recompile now.
In each of your weapon scripts, within the datablock ProjectileData() section add this to enable camera shake
// Camera Shaking shakeCamera = true; camShakeFreq = "2.0 1.0 2.0"; camShakeAmp = "0.5 0.5 0.5"; camShakeDuration = 0.2;
Play around with those values and season to taste
If you don't want camera shake on your weapon you MUST add shakeCamera = false; to each of your weapons datablock ProjectileData() sections
Enjoy! As always all comments, feedback, suggestions for improvement are welcome. If anyone can figure out how to get this working across the network please post.
As it is, across a network the following happens:
- If you have created the game no problems
- If you have joined the game camera shake does not work. Everytime the host fires a weapon with camera shake enabled your screen will shake.
April 24th 2006:
Just found out that Matt "Mr. Pig" Razza is working on a more advanced recoil system and is working on using this resource for the camera shake, and hopefully getting it to work across a network. You may want to check it out...
Advanced Weapon Recoil System - Weapon Bounce
About the author
Recent Blogs
• BCS Street props• Character Pack - Vince
• Recent Artwork
• Progress of our Weapon Pack
• Digital Speedometer
#2
Glad you got it working... (addNamedField, what were ya thinking! : p [joke])
02/19/2006 (8:45 am)
I just posted on what you did wrong C2, then you went & edited your post so now i've had to edit mine :)Glad you got it working... (addNamedField, what were ya thinking! : p [joke])
#3
02/19/2006 (8:55 am)
Actually it is addNamedField, I just had to rearrange where the "ProjectileData," went to match the rest of the TSE lines. Hehe.
#4
The section of code you originally posted from your TSE projectile.cc file was identical to mine from TGE, so I can't see why it wouldn't compile.
I don't know what the diffence between addField & addNamedField is exactly, I base my point off the fact that explosion.cc uses addField.
I'm just concerned that changing it to addNamedField might cause problems for you when you try to alter values from script. Perhaps someone who is a litte more programming savvy can explain the difference between addField & addNamedField & if having it one way or the other matters...
02/19/2006 (9:09 am)
Not trying to sound rude here but it should be just addField, though if addNamedField works for you then I guess we have nothing to argue about :) The section of code you originally posted from your TSE projectile.cc file was identical to mine from TGE, so I can't see why it wouldn't compile.
I don't know what the diffence between addField & addNamedField is exactly, I base my point off the fact that explosion.cc uses addField.
I'm just concerned that changing it to addNamedField might cause problems for you when you try to alter values from script. Perhaps someone who is a litte more programming savvy can explain the difference between addField & addNamedField & if having it one way or the other matters...
#5
Edit: Found some information on the addField and addNamedField
02/19/2006 (9:22 am)
Hmm, strange. I just altered those lines to look like this:addNamedField(shakeCamera, TypeBool, ProjectileData, shakeCamera); addNamedField(camShakeFreq, TypePoint3F, ProjectileData, setcamShakeFreq); addNamedField(camShakeAmp, TypePoint3F, ProjectileData, camShakeAmp); addNamedField(camShakeDuration, TypeF32, ProjectileData, camShakeDuration); addNamedField(camShakeFalloff, TypeF32, ProjectileData, camShakeFalloff);And it compiled fine and the values change as they should, makes one heck of a nice effect. :)
Edit: Found some information on the addField and addNamedField
Quote:
In Torque, there is a function addField() and a MACRO addNamedField(). The MACRO calls addField() and exposes a class member as a console field, using the SAME name as the class member. For all intensive purposes, this is just a less flexible version of addField.
#6
If I'm reading that correctly it won't make any difference, however it does state that "For all intensive purposes, this is just a less flexible version of addField." so I'll be leaving mine the way it is.
02/19/2006 (9:28 am)
Ah, I see. Thanks for the info C2. If I'm reading that correctly it won't make any difference, however it does state that "For all intensive purposes, this is just a less flexible version of addField." so I'll be leaving mine the way it is.
#7
02/23/2006 (5:41 pm)
Does this resource work in Multiplayer yet?
#8
02/23/2006 (6:44 pm)
It works everwhere appart from the host computer, whenever the host fires every screen shakes. It works with AI and if you arent the host. I will fix it but its not a priority. The error seems to me occurs in getUpdatePriority().
#9
02/23/2006 (7:47 pm)
Somehow we must get this working in networked play. My knowledge in C++ is extremely limited, though.
#10
Just my two cents.
If nobody figures it out soon I will look into the code and post what I find.
02/23/2006 (11:39 pm)
Seems as though you should have this code solely on the client side. I haven't looked to deep into it, but from the pack and unpack it seems you are doing network packets to send shake data, if you where only doing this on the client side, you shouldn't have to send anything. That way you don't have to worry about wasting useless bandwidth on something that is a per client deal (along with cam shake when close to an explosion).Just my two cents.
If nobody figures it out soon I will look into the code and post what I find.
#11
02/24/2006 (9:54 am)
Grrrr
#12
02/24/2006 (11:21 pm)
P.S. I am sorry if I came off the wrong way, I wasn't trying to put down your work (it is good work), just trying to throw in my opinoin on how to get it working in multiplayer.
#13
I thought I had it working (across network) & posted my results but further testing proved otherwise. I deleted my original post and replaced it with Grrrr as a sign of my frustration.
I appreciate all input, if you'd be kind enough to take a look at this i'd be most appreciative. Sooner than later would be preferable as this has me stumped.
02/25/2006 (5:07 am)
Hey anthony, my grrrr in the above post wasn't directed towards you!!! I thought I had it working (across network) & posted my results but further testing proved otherwise. I deleted my original post and replaced it with Grrrr as a sign of my frustration.
I appreciate all input, if you'd be kind enough to take a look at this i'd be most appreciative. Sooner than later would be preferable as this has me stumped.
#14
02/25/2006 (7:21 am)
Same here, I'm stupid.
#15
I can't get it working on the server without it effecting the client. Just going to have to wait for some outside assistance.
02/25/2006 (9:58 am)
Not winning here guys.I can't get it working on the server without it effecting the client. Just going to have to wait for some outside assistance.
#16
03/28/2006 (2:25 pm)
@Anthony: Could use your help now :)
#17
04/11/2006 (11:02 am)
Hey hey. I'll be porting my recoil system to the engine and post a resource (with no limitations to usage) hopefully within the week.
#18
04/11/2006 (9:47 pm)
Got it working. Let me just compile it (again) - test it across a network - and I'll post a resource. The server (should) play no roll at all in this recoil system.
#19
Turns out the server does need to transfer the loaded image to the client - but that's about it.
04/11/2006 (10:13 pm)
w00t! Works great. I'll post a resourse in a few hours as I need some sleep. (took me about two hours to get this working)Turns out the server does need to transfer the loaded image to the client - but that's about it.
#20
When the resource is approved it can be found here:
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=10241
04/12/2006 (8:52 am)
Done. I did not use anything from this post (as you can see when you look at it). There are no changes to projectile.cc or projectile.h - I feal you were going about that the wrong way as it is the WEAPON that is recoiling, not the bullet. It doesn't use camera shake - but if you want that over my meathod it can be easily changed (just change 2 or so lines of code) as camera shake wouldn't create the effect I am looking for.When the resource is approved it can be found here:
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=10241

Torque Owner Chris Byars
Ion Productions