Game Development Community

dev|Pro Game Development Curriculum

Jetpack - 1.4 / 1.5 Compliant

by Tim Heldna · 11/13/2006 (4:44 pm) · 56 comments

This resource will allow you to add a jetting functionality to your player. This enables the player to press a mouse button, or keyboard input and begin to fly.

It also adds the ability of movement whilst jumping or jetting when in the air.

It supports the use of energy. When energy is drained you will not be able to jet until it has regenerated. This of course is optional. If desired, you may permit players to jet indefinitely. This is controlled through script.

Although this has a similar feel to the Tribes jetting functionality, it is not the same. It does not support skiing.

I do not take credit for the code used in this resource. I merely used the Jet Pack Love resource, combined with the Air Control resource and updated it to work with 1.4. & 1.5. I also cleaned some code and did some reformatting.

Files affected:

Source Code
player.cc
player.h

Script
player.cs
default.bind.cs

Install Instructions:

All code changes have been encased with:
// Added for player jetting start
   // Added for player jetting end

player.cc

Find
maxJumpSpeed = 2 * minJumpSpeed;

Underneath that add
// Added for player jetting start
   jetJumpForce = 75 * 5;
   jetJumpEnergyDrain = 0;
   jetMinJumpEnergy = 0;
   jetJumpSurfaceAngle = 78;
   jetMinJumpSpeed = 20;
   jetMaxJumpSpeed = 100;
   // Added for player jetting end

Find
groundImpactShakeFalloff = 10.0;

Underneath that add
// Added for player jetting start
   airControl=0.0f;
   // Added for player jetting end

Find
if (minJumpEnergy < jumpEnergyDrain)
      minJumpEnergy = jumpEnergyDrain;

Underneath that add
// Added for player jetting start
   if (jetMinJumpEnergy < jetJumpEnergyDrain)
      jetMinJumpEnergy = jetJumpEnergyDrain;
   // Added for player jetting end

Find
addField("jumpDelay", TypeS32, Offset(jumpDelay, PlayerData));

Underneath that add
// Added for player jetting start
   addField("jetJumpForce", TypeF32, Offset(jetJumpForce, PlayerData));
   addField("jetJumpEnergyDrain", TypeF32, Offset(jetJumpEnergyDrain, PlayerData));
   addField("jetMinJumpEnergy", TypeF32, Offset(jetMinJumpEnergy, PlayerData));
   addField("jetMinJumpSpeed", TypeF32, Offset(jetMinJumpSpeed, PlayerData));
   addField("jetMaxJumpSpeed", TypeF32, Offset(jetMaxJumpSpeed, PlayerData));
   addField("jetJumpSurfaceAngle", TypeF32, Offset(jetJumpSurfaceAngle, PlayerData));
   // Added for player jetting end

Find
addField("groundImpactShakeFalloff",   TypeF32,       Offset(groundImpactShakeFalloff,    PlayerData));

Underneath that add
// Added for player jetting start
   addField("airControl", TypeF32, Offset(airControl,PlayerData));
   // Added for player jetting end

Find
stream->writeInt(jumpDelay,JumpDelayBits);

Underneath that add
// Added for player jetting start
   stream->write(jetJumpForce);
   stream->write(jetJumpEnergyDrain);
   stream->write(jetMinJumpEnergy);
   stream->write(jetMinJumpSpeed);
   stream->write(jetMaxJumpSpeed);
   stream->write(jetJumpSurfaceAngle);
   // Added for player jetting end

Find
stream->write(groundImpactShakeFalloff);

Underneath that add
// Added for player jetting start
   stream->write(airControl);
   // Added for player jetting end

Find
jumpDelay = stream->readInt(JumpDelayBits);

Underneath that add
// Added for player jetting start
   stream->read(&jetJumpForce);
   stream->read(&jetJumpEnergyDrain);
   stream->read(&jetMinJumpEnergy);
   stream->read(&jetMinJumpSpeed);
   stream->read(&jetMaxJumpSpeed);
   stream->read(&jetJumpSurfaceAngle);
   // Added for player jetting end

Find
stream->read(&groundImpactShakeFalloff);

Underneath that add
// Added for player jetting start
   stream->read(&airControl);
   // Added for player jetting end

This next bit is a little tricky. It's a trap for young players, people always seem to get confused as to where the code starts and ends. Make sure all your opening and closing brackets line up! Please take some extra care with the next few step. Read slowly and carefully. I'll try to make it as easy as possible for you.

Find
// Adjust the players's requested dir. to be parallel
      // to the contact surface.
      F32 pvl = pv.len();

Directly underneath, replace the next if statement with this
// Removed for player jetting start
/*
      if (pvl) {
         VectorF nn;
         mCross(pv,VectorF(0,0,1),&nn);
         nn *= 1 / pvl;
         VectorF cv = contactNormal;
         cv -= nn * mDot(nn,cv);
         pv -= cv * mDot(pv,cv);
         pvl = pv.len();
      }
*/
// Removed for player jetting end

I tend to comment code out rather than deleting it, makes life easier when debugging.

Directly underneath the section of code you just commented out, and above this
// Convert to acceleration
      if (pvl)
         pv *= moveSpeed / pvl;
      VectorF runAcc = pv - (mVelocity + acc);
      F32 runSpeed = runAcc.len();

Add this
// Added for player jetting start
      if(mJetting)
      {
         pvl = moveVec.len();
         if (pvl)
         {
            VectorF nn;
            mCross(pv,VectorF(0,0,0),&nn);
            nn *= 1 / pvl;
            VectorF cv;
            cv.set(0,0,0);
            cv -= nn * mDot(nn,cv);
            pv -= cv * mDot(pv,cv);
            pvl = pv.len();
         }
      }
      else
      {
         if (pvl)
         {
            VectorF nn;
            mCross(pv,VectorF(0,0,1),&nn);
            nn *= 1 / pvl;
            VectorF cv = contactNormal;
            cv -= nn * mDot(nn,cv);
            pv -= cv * mDot(pv,cv);
            pvl = pv.len();
         }
      }
// Added for player jetting end

Now just a few lines below find this
else
      mContactTimer++;

And replace those two lines of code with
// Removed for player jetting start
//   else
//      mContactTimer++;
// Removed for player jetting end

// Added for player jetting start
   else 
   {
      mContactTimer++;

      if (!inLiquid && mDataBlock->airControl > 0.0f) 
      {
         VectorF pv;
         pv = moveVec;

         F32 pvl = pv.len();

         if (pvl)
            pv *= moveSpeed / pvl;
            VectorF runAcc = pv - acc;
            runAcc.z = 0;
            runAcc.x = runAcc.x * mDataBlock->airControl;
            runAcc.y = runAcc.y * mDataBlock->airControl;
            F32 runSpeed = runAcc.len();

            F32 maxAcc = (mDataBlock->runForce / mMass) * TickSec * mDataBlock->airControl;

            if (runSpeed > maxAcc)
               runAcc *= maxAcc / runSpeed;
               acc += runAcc;
      }
   }
// Added for player jetting end

If you've followed my instructions correctly, you should find this directly underneath the section of code you just pasted in.
// Acceleration from Jumping
   if (move->trigger[2] && !isMounted() && canJump())

Directly after the closing brace { of above mentioned if statement, E.g.
setActionThread((mVelocity.len() < 0.5)?
            PlayerData::StandJumpAnim: PlayerData::JumpAnim, true, false, true);
         mJumpSurfaceLastContact = JumpSkipContactsMax;
      }
   } [b]<-- this is the last closing bracket[/b]


Add
// Added for player jetting start
   if (move->trigger[1] && !isMounted() && canJetJump())
   {
      mJetting = true;

      // Scale the jump impulse base on maxJumpSpeed
      F32 zSpeedScale = mVelocity.z;

         if (zSpeedScale <= mDataBlock->jetMaxJumpSpeed)
         {
            zSpeedScale = (zSpeedScale <= mDataBlock->jetMinJumpSpeed)? 1:
            1 - (zSpeedScale - mDataBlock->jetMinJumpSpeed) / (mDataBlock->jetMaxJumpSpeed - mDataBlock->jetMinJumpSpeed);

            // Desired jump direction
            VectorF pv = moveVec;
            F32 len = pv.len();

            if (len > 0)
               pv *= 1 / len;

            // If we are facing into the surface jump up, otherwise
            // jump away from surface.
            F32 dot = mDot(pv,mJumpSurfaceNormal);
            F32 impulse = mDataBlock->jetJumpForce / mMass;

            if (dot <= 0)
               acc.z += mJumpSurfaceNormal.z * impulse * zSpeedScale;

            else
            {
               acc.x += pv.x * impulse * dot;
               acc.y += pv.y * impulse * dot;
               acc.z += mJumpSurfaceNormal.z * impulse * zSpeedScale;
            }
            mEnergy -= mDataBlock->jetJumpEnergyDrain;
	    setActionThread((mVelocity.len() < 0.5)?            
	    // PlayerData::StandJumpAnim: PlayerData::JumpAnim, true, false, true); // Removed as it was causing people problems with the jump animation whilst jetting  
         }
   }

   else
   {
      mJetting = false;
   }

   if (mJetting)
   {
      F32 newEnergy = mEnergy - mDataBlock->minJumpEnergy;
      if (newEnergy < 0)
      {
         newEnergy = 0;
         mJetting = false;
      }
   mEnergy = newEnergy;
   }
// Added for player jetting end

Tricky part is over.

Find this if statement
// Container buoyancy & drag
   if (mBuoyancy != 0)
   {     // Applying buoyancy when standing still causing some jitters-
      if (mBuoyancy > 1.0 || !mVelocity.isZero() || !runSurface)
         mVelocity.z -= mBuoyancy * mGravity * mGravityMod * TickSec;
   }

And change it to look like this
// Container buoyancy & drag
   if (mBuoyancy != 0)
   {     // Applying buoyancy when standing still causing some jitters-

      // Removed for player jetting start
      //if (mBuoyancy > 1.0 || !mVelocity.isZero() || !runSurface)
      // Removed for player jetting end
      
      // Added for player jetting start
      if (mBuoyancy > 1.0 || !runSurface)
      // Added for player jetting end
         mVelocity.z -= mBuoyancy * mGravity * mGravityMod * TickSec;
   }

Find this function
bool Player::canJump()
{
   return mState == MoveState && mDamageState == Enabled && !isMounted() && !mJumpDelay && mEnergy >= mDataBlock->minJumpEnergy && mJumpSurfaceLastContact < JumpSkipContactsMax;
}

Directly underneath add this new function
// Added for player jetting start
bool Player::canJetJump()
{
   return mState == MoveState && mDamageState == Enabled && !isMounted() && mEnergy >= mDataBlock->jetMinJumpEnergy;
}
// Added for player jetting end

That's all for player.cc

player.h

Find
S32 jumpDelay;

Underneath that add
// Added for player jetting start
   F32 jetJumpForce;
   F32 jetJumpEnergyDrain; // Energy per jump
   F32 jetMinJumpEnergy;
   F32 jetMinJumpSpeed;
   F32 jetMaxJumpSpeed;
   F32 jetJumpSurfaceAngle; // Angle vertical degrees
   // Added for player jetting end

Find
F32 footSplashHeight;

Underneath that add
// Added for player jetting start
   F32 airControl;
   // Added for player jetting end

Find
void updateState();

Underneath that add
// Added for player jetting start
   bool mJetting;
   // Added for player jetting end

Find
bool  canJump();

Underneath that add
// Added for player jetting start
   bool canJetJump();
   // Added for player jetting end

That takes care of all head code changes. Recompile now.


Now we only have some minor scripting additions to make.

player.cs

Anywhere in the PlayerData datablock add

// Added for player jetting start
   airControl         = 0.3;
   jetJumpForce       = (8.3 * 10);
   jetJumpEnergyDrain = 0.8;
   jetMinJumpEnergy   = 0;
   // Added for player jetting end

default.bind.cs

Anywhere you like add

// Added for player jetting start
function mouseJet(%val)
{
   $mvTriggerCount1++;
}

moveMap.bind(mouse, button1, mouseJet);
// Added for player jetting end

That will bind jetting to the right mouse button. Feel free to change the binding to anything you like.

Make sure you delete your client\config.cs file for the new keybinding to take affect.

Season the datablock values inside the PlayerData datablock to your liking.

This concludes today's code snippet, enjoy!
#21
12/27/2006 (8:42 pm)
I restored the resource to its original state, there was nothing wrong with it.

I don't like calling animations from script (script is too slow) and besides, if you follow the resource instructions correctly and have a properely set up player model you won't have any problems.
#22
12/31/2006 (6:22 am)
I tried it with the default ork and it still loops the jump anim. Not sure whats causing it. Could be another resource I have added.
#23
01/23/2007 (2:54 pm)
Hi there. Excellent resource compilation! Many thanks!

mb: I incorporated this resource into an existing game with a non-Kork player and it did the jump animation thing, so I tried it in a vanilla 1.5 SDK install with Kork and it still did it.

Still, I am happy that I now have a flying dude, even if he has to jump in the air to maintain flight. (I guess if Michael Jordan kicks his legs to stay in the air longer, my dude can too!)
#24
01/23/2007 (9:19 pm)
Works great on TGE 1.5.
Thanks much!
#25
02/09/2007 (3:33 am)
Does this work with AIPlayers as well? Or is there another resource. There are quite a few jet pack resource button I am unsure which one to use to get bots to fly.
#26
02/16/2007 (11:38 am)
Thanks Tim for this resource, It works flawlessly with TGEA!!!
All you have to do is make changes in player.cpp vs. player.cc

Thanks much

John
#27
02/26/2007 (1:28 pm)
I would love to implement this into my 1.5.2

Anyone had any luck with it ?
#28
05/13/2007 (12:04 am)
Finally a resource that works for me... now to make it a pickup and more fuel and a fuel bar... Thanks!!!
#29
05/15/2007 (10:47 am)
Hey is there anywayt o do this through script only?
#30
06/14/2007 (5:07 am)
Works great with torque 1.5.2

Only flaw was the player keep jumping in air but i followed this by MB and it is fixed;

"I hacked it by commenting out:

//setActionThread((mVelocity.len() < 0.5)?
//PlayerData::StandJumpAnim: PlayerData::JumpAnim, true, false, true);

Theres two places in player.cc like that. Comment out the second one only or your regular jump anim wont play."
#31
06/22/2007 (9:21 am)
Nice resource. I noticed that the fall animation was quite nice to have while jetting about, but I didn't want it looping loads, so I just implemented the change above and then changed this line:
if (mFalling)
in this section
bool forward = true;
   U32 action = PlayerData::RootAnim;
   if (mFalling)
   {
      // Not in contact with any surface and falling
      action = PlayerData::FallAnim;
   }
to this:
if (mFalling || mJetting)
and it just does the fall animation when you're jetting.
#32
08/01/2007 (12:40 pm)
Compiles perfectly, I get no errors when I start the game, either... But absolutely nothing happens when I press the right mouse button.

Here are the four files: http://www.savefile.com/files/935866
#33
08/28/2007 (12:19 pm)
@Maddermadcat

add the following to your client/config.cs
// Added for player jetting start
moveMap.bind(mouse0, "button1", mouseJet);
// Added for player jetting end

Gabriel
#34
09/21/2007 (5:20 pm)
Added. Still, no jetting.
#35
09/27/2007 (8:38 am)
Easily added to 1.5.2 with no errors. Implemented suggestions by MB & Patrick Clifford and Nick Matthews to prevent the cycling jump animation.
#36
09/29/2007 (5:22 am)
I updated the resource with MB's fix (jump animation playing whilst jetting). I left the original line in there commented out as a reference for people want to add in their own jetting animation.
#37
10/22/2007 (4:14 pm)
In the updated resource you forgot to also edit out : setActionThread((mVelocity.len() < 0.5)?
before you comment out : PlayerData::StandJumpAnim: PlayerData::JumpAnim, true, false, true);
Otherwise you get an error like " error C2143: syntax error : missing ')' before '} '"
#38
12/02/2007 (2:31 pm)
Damn It, I Think I pretty much followed everything, but i think i have something wrong in my player.cs or im not sure, but here i will upload my files plz take a look at them, i really need to make this working xD thx a lot
here the link http://205.200.226.224/jetpack.rar (dont worry not a virus my home ip ^^)
#39
04/24/2008 (6:46 am)
I was wondering if ne1 here could help me i have applied all above correctly (as far as i know) on a clean TGE build that i have compiled without errors previously however when this resource is applied i get 1 error on compile. here is the error:

c:\torque\project chiron\engine\game\player.cc(1765) : error C2059: syntax error : ')'

and here is the set of code where this error takes place:

// Added for player jetting start
   if (move->trigger[1] && !isMounted() && canJetJump())
   {
      mJetting = true;

      // Scale the jump impulse base on maxJumpSpeed
      F32 zSpeedScale = mVelocity.z;

         if (zSpeedScale <= mDataBlock->jetMaxJumpSpeed)
         {
            zSpeedScale = (zSpeedScale <= mDataBlock->jetMinJumpSpeed)? 1:
            1 - (zSpeedScale - mDataBlock->jetMinJumpSpeed) / (mDataBlock->jetMaxJumpSpeed - mDataBlock->jetMinJumpSpeed);

            // Desired jump direction
            VectorF pv = moveVec;
            F32 len = pv.len();

            if (len > 0)
               pv *= 1 / len;

            // If we are facing into the surface jump up, otherwise
            // jump away from surface.
            F32 dot = mDot(pv,mJumpSurfaceNormal);
            F32 impulse = mDataBlock->jetJumpForce / mMass;

            if (dot <= 0)
               acc.z += mJumpSurfaceNormal.z * impulse * zSpeedScale;

            else
            {
               acc.x += pv.x * impulse * dot;
               acc.y += pv.y * impulse * dot;
               acc.z += mJumpSurfaceNormal.z * impulse * zSpeedScale;
            }
            mEnergy -= mDataBlock->jetJumpEnergyDrain;
	    setActionThread((mVelocity.len() < 0.5)?            
	    // PlayerData::StandJumpAnim: PlayerData::JumpAnim, true, false, true); // Removed as it was causing people problems with the jump animation whilst jetting  
         )}
   }

   else
   {
      mJetting = false;
   }

   if (mJetting)
   {
      F32 newEnergy = mEnergy - mDataBlock->minJumpEnergy;
      if (newEnergy < 0)
      {
         newEnergy = 0;
         mJetting = false;
      }
   mEnergy = newEnergy;
   }
// Added for player jetting end

more particularly this specific portion of this block:

setActionThread((mVelocity.len() < 0.5)?          
	    // PlayerData::StandJumpAnim: PlayerData::JumpAnim, true, false, true); // Removed as it was causing people problems with the jump animation whilst jetting  
         )}
   }
#40
08/20/2008 (8:58 pm)
Colling College (#0323) I am getting the exact same error as you with the ')' in the exact same spot. It would be greatly appreciated if someone could give any help on this.