Jump as Long as Jump Key is Pressed
by Pisal Setthawong · 07/03/2006 (11:37 am) · 2 comments
I was working on a console style 3D platform game in which required a different jump configuration that the default torque jump. I first started out by using the Charge Jump resource by James (BurNinG) Laker. However in the resource it will jump only when the jump key is released which is a different behavior from what I intended to be. Since I'm making a console like game, the default jump event is triggered by the jump key and should climb until the jump is released. If you have trouble visualizing, think about how RockMan(MegaMan) is played, the charged jump works much like the shoot event, and the jump works different.
This article explains how to modify Torque to allow that behavior assuming that you have the ChargedJump resource already implemented. If I dropped a few things, I have to apologize because my Torque core is rather heavily modified before this article is written.
To do create such an event the jump command must be triggered when the jump is pressed and then when the key is released, the falling state of the character must be immediately called so that it will stop jumping and proceed to the falling state.
What I first did was to modify the engine as the Charge Jump Resource. When done I made a few changes with it begining with the jump routine 'function jump(%val)' in the 'example\\client\scripts\default.bind.cs' to the following:
The modification of the jump from the earlier mod to this one is that the jump is triggered on the button press and and the release triggers another command that should cause the release of the jump and force the player to fall down fast.
On the server scripting side, modify the example\\server\scripts\commands.cs to include new instructions to the server scripting side which is theServerCmdJumpChargeRelease. I added my code after the original function ServerCmdJumpCharge(%client,%val) that could be modified if you like.
In this part of the code, it triggers that the player to force falling.
This is all that can be done in the scripting side, and now we need to modify the core Torque Engine in a number of places. We start by having to add a flag that informs whether we want the player to fall down immediately.
In the game/player.h, add the following data member to class Player in the protected part:
Now we could need to access the mForceFalling member with the set and get, in which we declare the method in game/player.h in class Player in the public part:
Now we need to define the method, and in game/player.c, add the following code:
Once done, we need to make sure we initialize the mForceFalling to false when we create the player. We can do that in the initializer (Player::Player()) at game/player.cc in which we will add the following code:
Once done, we now need to ask the engine in the jump routine to fall down once the mForceFalling flag is signaled to be true. This can be done by editing game/player.cc in the updateMove method. Find the end of the method and add the following code:
The above code will force the velocity of Z to fall when mForceFalling is true. This will cause the character to start dropping even before reaching the full jump height if the jump key is released. The change in velocity can create a huge variety of jumping effects, mattering to you liking.
Now there remains only third problem to fix. The first problem is to allow multiple variable height jumpings. In this case, we need to reset mForceFalling whenever we have reached the ground. I've found that the mFalling in the game/player.cc is the flag that tells if the player is falling or not. We can notice that to do so find all the mFalling=false; statements in the file. Add the following directly after it, mForceFalling=false;. This way after every jump, it resets the mForceFalling flag and that you could use it later.
The 2nd problem is that after every jump, due to the increasing velocity of the down jump, the player might injure themselves if that happens. To fix this problem you could do one of the following approaches:
1) Change the velocity change in the updateMove method above so that it increases slower so that is doesn't go above the threshold as in the original value of minImpactSpeed.
2) In the example\\server\scripts\player.cs modify the minImpactSpeed = value; Increase the value so that it requires a larger speed to injure the player.
2) You could clamp the falling velocity, though this could potentially cause players not to lose any health from falling off huge blocks if not careful.
The last problem is that after every jump, it is a default behavior that it causes the player to move in a certain direction depending on angle of the terrain the player impacts. In this case, there are a number of possible approaches. You could potentially ignore it and say it is a feature. Another thing you could do is the change how the player movement changes after the impact which you probably got to muck around the core engine again.
Additional things that you could add to this is to create console commands to access this forceFalling variables. It is similar to the charge jump example so you could probably be able to easily add code to allow the console command to work.
I hope this can help out.
This article explains how to modify Torque to allow that behavior assuming that you have the ChargedJump resource already implemented. If I dropped a few things, I have to apologize because my Torque core is rather heavily modified before this article is written.
To do create such an event the jump command must be triggered when the jump is pressed and then when the key is released, the falling state of the character must be immediately called so that it will stop jumping and proceed to the falling state.
What I first did was to modify the engine as the Charge Jump Resource. When done I made a few changes with it begining with the jump routine 'function jump(%val)' in the 'example\
function jump(%val)
{
//if %val = 0 the button is released (OnkeyUp); 1 if pressed (onKeydown)
if (%val==1)
{
echo("Jump Command");
CommandToServer('JumpCharge',2500);
// I know the following is weird, but it's a hack and i don't know any better.
// I works fine though!
$mvTriggerCount2++; //Adds the JumpTrigger....
$mvTriggerCount2++; //Removes Trigger again so Kork doesn't keep hopping crazy!
}
else
{
echo ("Release Jump");
CommandToServer('JumpChargeRelease');
}
}The modification of the jump from the earlier mod to this one is that the jump is triggered on the button press and and the release triggers another command that should cause the release of the jump and force the player to fall down fast.
On the server scripting side, modify the example\
function ServerCmdJumpChargeRelease(%client)
{
%client.player.setForceFalling();
}In this part of the code, it triggers that the player to force falling.
This is all that can be done in the scripting side, and now we need to modify the core Torque Engine in a number of places. We start by having to add a flag that informs whether we want the player to fall down immediately.
In the game/player.h, add the following data member to class Player in the protected part:
F32 mJumpCharge; //Original charged jump code [b] bool mForceFalling; //added code[/b]
Now we could need to access the mForceFalling member with the set and get, in which we declare the method in game/player.h in class Player in the public part:
bool getForceFalling(); //get bool mForceFalling for Jump Charge void setForceFalling(); //get bool mForceFalling for Jump Charge
Now we need to define the method, and in game/player.c, add the following code:
bool Player::getForceFalling()
{
return mForceFalling;
}
void Player::setForceFalling()
{
//Only if Client
if (!isGhost())
{
mForceFalling = true;
}
}Once done, we need to make sure we initialize the mForceFalling to false when we create the player. We can do that in the initializer (Player::Player()) at game/player.cc in which we will add the following code:
mForceFalling = false;
Once done, we now need to ask the engine in the jump routine to fall down once the mForceFalling flag is signaled to be true. This can be done by editing game/player.cc in the updateMove method. Find the end of the method and add the following code:
[b]if (mForceFalling==true)
{
mVelocity.z+= sFallingThreshold/3; //try playing around with this to get the drop speed you want
}[/b]The above code will force the velocity of Z to fall when mForceFalling is true. This will cause the character to start dropping even before reaching the full jump height if the jump key is released. The change in velocity can create a huge variety of jumping effects, mattering to you liking.
Now there remains only third problem to fix. The first problem is to allow multiple variable height jumpings. In this case, we need to reset mForceFalling whenever we have reached the ground. I've found that the mFalling in the game/player.cc is the flag that tells if the player is falling or not. We can notice that to do so find all the mFalling=false; statements in the file. Add the following directly after it, mForceFalling=false;. This way after every jump, it resets the mForceFalling flag and that you could use it later.
The 2nd problem is that after every jump, due to the increasing velocity of the down jump, the player might injure themselves if that happens. To fix this problem you could do one of the following approaches:
1) Change the velocity change in the updateMove method above so that it increases slower so that is doesn't go above the threshold as in the original value of minImpactSpeed.
2) In the example\
2) You could clamp the falling velocity, though this could potentially cause players not to lose any health from falling off huge blocks if not careful.
The last problem is that after every jump, it is a default behavior that it causes the player to move in a certain direction depending on angle of the terrain the player impacts. In this case, there are a number of possible approaches. You could potentially ignore it and say it is a feature. Another thing you could do is the change how the player movement changes after the impact which you probably got to muck around the core engine again.
Additional things that you could add to this is to create console commands to access this forceFalling variables. It is similar to the charge jump example so you could probably be able to easily add code to allow the console command to work.
I hope this can help out.
About the author
An Educator moonlighting as the Technical Lead at the indie game development studio called Flying Pig Game Studio
#2
03/13/2007 (10:13 am)
I must have forgotten that part when I written the resource. I have to apologize for that :P 
Torque Owner Jon C
So I hit the forums and was given just the right bit of info and came up with this fix to get this working properly.
Just add this in player.cc:
ConsoleMethod( Player, setForceFalling, void, 2, 4, "(bool mForceFalling)") { object->setForceFalling(); } ConsoleMethod( Player, getForceFalling, bool, 2, 2, "(bool mForceFalling)") { return object->getForceFalling(); }Voila! Have fun.