Game Development Community

Variable Movement

by Chris Cain · in Torque Game Engine · 03/13/2005 (8:24 am) · 4 replies

Hello. I've had the torque engine for a long time, but I haven't used it much. The only times I have used it was to mess around with the scripting engine. What I want to do right now, though, is to change something in the C++ code to make variable movement.

I've seen one way to do it in a resource, but because it was so old (2002 I think), it was difficult to find the places to put everything.

What I want to do is make it so that when the player object has one of the datablock variables attached to it, then it would be used over the datablock. For example, if the datablock said runForce = 4000, but the %player.runForce = 40000, then the %player.runForce would be used to calculate movement rather than what the datablock says.

In the C++ code, I deducted that updateMove is the thing that I want to change, but I'm not sure what in there I should change to make it do what I want, plus I've read and figured out that I might have to change more code to let the clients know that the movement was changed. I say might, because I don't think any of the other resources I have looked at use a script sided variable movement.

I'm not experienced with C++, however I have read a couple of books. If you think that this would be easy enough for me to do myself, do you think you could point me to resources to look at to figure out how to do this by myself and fill in the holes for me? If you think its too hard, could you tell me what I would need to do to impliment my idea?

Thanks in advance! ^_^

#1
03/13/2005 (1:41 pm)
Actually, I think that I might have figured out how to do it myself... but I'm having problems (its buggy) ... Could someone help me out with this?

In player.h I added my modification variable for runForce:
class Player: public ShapeBase
{
...
   // Current pos, vel etc.
   Point3F mHead;                   ///< Head rotation, uses only x & z
   Point3F mRot;                    ///< Body rotation, uses only z
   VectorF mVelocity;               ///< Velocity
   Point3F mAnchorPoint;            ///< Pos compression anchor
   static F32 mGravity;             ///< Gravity
   S32 mImpactSound;

   S32 mMountPending;               ///< mMountPending suppresses tickDelay countdown so players will sit until
                                    ///< their mount, or another animation, comes through (or 13 seconds elapses).
[b]   // Variable Movement! -- Zshazz
   F32 mRunForceMod; // Modifies the runForce [/b]

   /// Main player state
   enum ActionState {
      NullState,
      MoveState,
      RecoverState,
      NumStateBits = 3
   };
...
   // Object control
   void setControlObject(ShapeBase *obj);
   ShapeBase* getControlObject();

[b]   // Variable Movement -- Zshazz
   static void initPersistFields(); // Has to be declared so that the engine knows to use my persist fields [/b]

   //
   void updateWorkingCollisionSet();
   void processTick(const Move *move);
...
};

In player.cc I did this:
Player::Player()
{
...
   mMountPending = 0;

[b]   // Variable Movement -- Zshazz
   mRunForceMod = 0.0; // I'm implementing it so that if it is 0, then it does nothing [/b]
}

then

Player::~Player()
{
}


//----------------------------------------------------------------------------

[b]// Variable Movement -- Zshazz
// Had to make the initPersistFields entirely because player doesn't normally have it.
void Player::initPersistFields()
{
   Parent::initPersistFields();
   
   // Add the Variable Movement things you want.
   addField("runForce", TypeF32, Offset(mRunForceMod,   Player));
}[/b]

bool Player::onAdd()

then

void Player::updateMove(const Move* move)
{
   delta.move = *move;
   
[b]   // Variable Movement -- Zshazz
   if (mRunForceMod == 0) // If its 0, then use the standard runForce
	   mRunForceMod = mDataBlock->runForce;[/b]

   // Trigger images
   if (mDamageState == Enabled) {
      setImageTriggerState(0,move->trigger[0]);
      setImageTriggerState(1,move->trigger[1]);
   }
...
      // Clamp acceleratin, player also accelerates faster when
      // in his hard landing recover state.
[b]	  // Variable Movement -- Zshazz
      F32 maxAcc = (mRunForceMod / mMass) * TickSec; // Replaced mDataBlock->runForce with mRunForceMod[/b]
      if (mState == RecoverState)
         maxAcc *= mDataBlock->recoverRunForceScale;
      if (runSpeed > maxAcc)
         runAcc *= maxAcc / runSpeed;
      acc += runAcc;

      // If we are running on the ground, then we're not jumping
      if (mDataBlock->isJumpAction(mActionAnimation.action))
         mActionAnimation.action = PlayerData::NullAnimation;
   }
   else
...
}

... and then ...

U32 Player::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
{
...
[b]   // Variable Movement -- Zshazz
   stream->write(mRunForceMod);[/b]
   return retMask;
}

... and then ...

void Player::unpackUpdate(NetConnection *con, BitStream *stream)
{
...
   F32 energy = stream->readFloat(EnergyLevelBits) * mDataBlock->maxEnergy;
   setEnergyLevel(energy);

[b]   // Variable Movement -- Zshazz
   stream->read(&mRunForceMod);[/b]
}

Sorry about so much code...

Is there anything I forgot? I don't know what I could have missed...

Edit:

Oops... I forgot to say what my problem was... The code works, but the player becomes jerky (I guess its a sync problem, but I don't know how to fix it...)
#2
03/13/2005 (4:06 pm)
Actually, now I think I fixed it.

The updates were the problem, For some reason it didn't work at the bottom of the file, so I put it nearer to the top. Works like a dream now.

Sorry to waste everyone's time. I'm going to add this to the Code Snipits and hopefully this will help other people. It will be called "Variable Movement" if anyone wants to search for it.
#3
03/28/2005 (11:08 am)
I tried to post the resource, but its been sitting as "Unapproved" since I posted it... I'm not sure whats wrong.

Is there a limit to the length of the resource?