Jumping movement
by Bullitt Sesariza · in Torque Game Engine · 10/01/2007 (1:40 am) · 13 replies
Hi, I'm trying to make a 3D frogger-like game. I've just meddling with the FPS starter kits and added a static camera from the Advanced Camera tutorial. Currently I am trying to make my own custom movement and implement it to the wsad keys. I need the frog to hop instead of walking and halfway successful of making the hopping movement. The scripts for them are:
function jumpForward(%val)
{
//If pressed
if ( %val )
{
switch ($frogOrientation)
{
case 1: //If facing south
$mvYaw += 3.141593;
break;
case 2: //If facing west
$mvYaw += 1.570796;
break;
case 3: //If facing east
$mvYaw -= 1.570796;
break;
}
$mvForwardAction = 200;
$mvTriggerCount2 += $mvTriggerCount2 & 1 == %val ? 2 : 1;
//Set the orientation flag to north
$frogOrientation = 0;
}
//If released
else
{
$mvForwardAction = 0;
$mvTriggerCount2 += $mvTriggerCount2 & 1 == %val ? 2 : 1;
}
}
function jumpBackward(%val)
{
//If pressed
if ( %val )
{
switch ($frogOrientation)
{
case 0: //If facing north
$mvYaw += 3.141593;
break;
case 2: //If facing west
$mvYaw -= 1.570796;
break;
case 3: //If facing east
$mvYaw += 1.570796;
break;
}
$mvForwardAction = 200;
$mvTriggerCount2 += $mvTriggerCount2 & 1 == %val ? 2 : 1;
//Set the orientation flag to south
$frogOrientation = 1;
}
//If released
else
{
$mvForwardAction = 0;
$mvTriggerCount2 += $mvTriggerCount2 & 1 == %val ? 2 : 1;
}
}
function jumpLeft(%val)
{
//If pressed
if ( %val )
{
switch ($frogOrientation)
{
case 0: //If facing north
$mvYaw -= 1.570796;
break;
case 1: //If facing south
$mvYaw += 1.570796;
break;
case 3: //If facing east
$mvYaw += 3.141593;
break;
}
$mvForwardAction = 200;
$mvTriggerCount2 += $mvTriggerCount2 & 1 == %val ? 2 : 1;
//Set the orientation flag to west
$frogOrientation = 2;
}
//If released
else
{
$mvForwardAction = 0;
$mvTriggerCount2 += $mvTriggerCount2 & 1 == %val ? 2 : 1;
}
}
function jumpRight(%val)
{
//If pressed
if ( %val )
{
switch ($frogOrientation)
{
case 0: //If facing north
$mvYaw += 1.570796;
break;
case 1: //If facing south
$mvYaw -= 1.570796;
break;
case 2: //If facing west
$mvYaw += 3.141593;
break;
}
$mvForwardAction = 200;
$mvTriggerCount2 += $mvTriggerCount2 & 1 == %val ? 2 : 1;
//Set the orientation flag to east
$frogOrientation = 3;
}
//If released
else
{
$mvForwardAction = 0;
$mvTriggerCount2 += $mvTriggerCount2 & 1 == %val ? 2 : 1;
}
}
#2
in player.cc updateMove() method
Now, I still need to solve the other problems. Can anybody help me please? Thanks in advance
10/02/2007 (7:54 pm)
Still no answer I see. Please, can anybody help me here? Ok, I've figured out one of my problems. I have managed to make the main player's jumping height. This is the code that I changed:in player.cc updateMove() method
// We want to scale the jump size by the player size, somewhat
// in reduced ratio so a smaller player can jump higher in
// proportion to his size, than a larger player.
//F32 scaleZ = (getScale().z * 0.25) + 0.75;
//--- Mine: test for jump height not based on size
F32 scaleZ = 5.0;Now, I still need to solve the other problems. Can anybody help me please? Thanks in advance
#3
PS: Just occurred to my mind that engines trivialize complicated matters and complicate trivial matters.
10/02/2007 (8:05 pm)
BTW, I've just figured why it can't jump very far without moving first. Maybe it's because there's an acceleration due to the gravity so the velocity became near 0. So how do I turn off the acceleration? Or maybe it would be better if I can turn off the whole physics. So how can I do that? Thanks in advance.PS: Just occurred to my mind that engines trivialize complicated matters and complicate trivial matters.
#4
Air Control
Double/Air Jump
10/02/2007 (8:08 pm)
This probably won't help you much but I may as well mention it... Here are two resources that might contain some useful code:Air Control
Double/Air Jump
#5
Thanks, I've looked at the air control code. I think it's for adding x & y acceleration in mid air, right? Not exactly what I need. What I really need is the jumping distance will be the same whether the player's starting position is idle (no starting velocity) or running (with starting velocity). Also it will be the same whether I press the movement button only for a short while or hold the movement button while jumping. So maybe what I really need is how to make the horizontal velocity (x and/or y) from 0 to max in an instant without having to accelerate. AFAIK, the vertical / z acceleration is still needed to calculate the descend / falling of the player. Thanks though.
10/03/2007 (9:31 pm)
@ Tim HeldnaThanks, I've looked at the air control code. I think it's for adding x & y acceleration in mid air, right? Not exactly what I need. What I really need is the jumping distance will be the same whether the player's starting position is idle (no starting velocity) or running (with starting velocity). Also it will be the same whether I press the movement button only for a short while or hold the movement button while jumping. So maybe what I really need is how to make the horizontal velocity (x and/or y) from 0 to max in an instant without having to accelerate. AFAIK, the vertical / z acceleration is still needed to calculate the descend / falling of the player. Thanks though.
#6
10/03/2007 (10:33 pm)
Hey...
#7
This variable is for a flag for whether the player is jumping or not. Will be toggled true in the first instance of jumping (if move->trigger[2]) and false if it meets the runSurface.
Next, in the beginning of the Player::updateMove() after the
I added these:
With this, if we're still jumping, the movement to the front (y) will always be 1 so the air control will always be active.
And inside the:
I added:
So, if the player is on the ground / meet with the run surface, the jumpFlag will be toggled false.
Lastly, in the:
I added:
So every time there's a trigger to jump, it will set the jumpFlag to true.
Now I just have to set the movement speed, jump force, mass, air control, etc, to give the right combination for the jump. But still there's some issues regarding momentum and impulse. Now after the player reaches the ground, it will walk for a few step before standing still. How do I make it stand still right after it reaches the ground regardless of the starting velocity?
Thanks again.
10/24/2007 (10:57 am)
Well, after some tinkering, I've managed to add a quick hack from the "air control" resource to solve my problem. I've added a static global variable to the player.cc:static bool jumpFlag=false;
This variable is for a flag for whether the player is jumping or not. Will be toggled true in the first instance of jumping (if move->trigger[2]) and false if it meets the runSurface.
Next, in the beginning of the Player::updateMove() after the
delta.move = *move;
I added these:
if (jumpFlag)
{
//The quick and dirty hack. I like it!!!!!!
const_cast<Move *> (move)->y=1;
}With this, if we're still jumping, the movement to the front (y) will always be 1 so the air control will always be active.
And inside the:
if (runSurface) {
...I added:
jumpFlag=false;
So, if the player is on the ground / meet with the run surface, the jumpFlag will be toggled false.
Lastly, in the:
if (move->trigger[2] && !isMounted() && canJump())
{
...I added:
jumpFlag=true;
So every time there's a trigger to jump, it will set the jumpFlag to true.
Now I just have to set the movement speed, jump force, mass, air control, etc, to give the right combination for the jump. But still there's some issues regarding momentum and impulse. Now after the player reaches the ground, it will walk for a few step before standing still. How do I make it stand still right after it reaches the ground regardless of the starting velocity?
Thanks again.
#8
Set the mass / drag higher in the datablock.
10/25/2007 (4:30 am)
>>>How do I make it stand still right after it reaches the ground regardless of the starting velocitySet the mass / drag higher in the datablock.
#9
1. I need the control to be responsive as in the jumping movement should be as fast as it could. The current jumping movement is too slow because the delta time between take off and landing is still to big. I'm afraid that it will break the player's timing in the game so eventually they will be annoyed with this. In the classic frogger, the movement is instant so the player's timing is precise. I've tried increasing the value of jumpForce and runForce (I believe the angle of the jump is the resultant of these 2 force, right? CMIIW). It did make the jump faster, but the jump range is also increase (which was not my intention). How do I make the jumping movement faster but without making it's range longer?
2. In the classic frogger, the game's map was a tiled square one and also the movement is only 4 direction. And also, the movement's length is constant. But in my game, because of the realistic physics, the movement's length isn't always constant. It is still affected by momentum although I've increased the drag. Eg, when I jumped forward 5 times with the jumpForward() function above and immediately jumped backward 5 times also with the jumpBackward() function, it ends up not in the starting position. In the 5th jump right before the 1st jumpBackward() was called, there is, IMHO, a starting velocity vector that was in the opposite direction of the jumpBackward() direction (or still moving forward) due to the momentum. So the 1st jumpBackward() function's range was shorter than the 1st jumpForward()'s range. Also, the 1st jumpForward()'s range is shorter than the 2nd because the 2nd already has the starting velocity from the 1st's momentum. So, how do I solve that?
3. The frogger game is only 4 directional one so I just make it yaw 90, -90, or 180 degree and then move (jump) it forward. But because the mYaw is in radian, IMHO the yaw is not truly 90, -90, or 180. I've experimented it myself and found out that the yaw-ed orientation is slightly off. So, how do I make the yaw angle to be exactly either 90 / -90 / 180 degree? Or better yet, how do I make the player / control object's orientation to be parallel to either x / -x / y / -y axis of the world coordinate?
4. I need to restrict the player inside the static camera's viewport so it cannot out of the camera's sight. How do I do that?
Hope they are not too much to ask. Thanks a lot in advance. :D
10/30/2007 (10:27 pm)
Ah thanks. It's fixed now. However, there's still lots of physics issues that need to be solved. The issues I believe is because my game is basically a glorified 3D frogger. So I don't need the physics to be realistic because it will break the gameplay itself. The issues are:1. I need the control to be responsive as in the jumping movement should be as fast as it could. The current jumping movement is too slow because the delta time between take off and landing is still to big. I'm afraid that it will break the player's timing in the game so eventually they will be annoyed with this. In the classic frogger, the movement is instant so the player's timing is precise. I've tried increasing the value of jumpForce and runForce (I believe the angle of the jump is the resultant of these 2 force, right? CMIIW). It did make the jump faster, but the jump range is also increase (which was not my intention). How do I make the jumping movement faster but without making it's range longer?
2. In the classic frogger, the game's map was a tiled square one and also the movement is only 4 direction. And also, the movement's length is constant. But in my game, because of the realistic physics, the movement's length isn't always constant. It is still affected by momentum although I've increased the drag. Eg, when I jumped forward 5 times with the jumpForward() function above and immediately jumped backward 5 times also with the jumpBackward() function, it ends up not in the starting position. In the 5th jump right before the 1st jumpBackward() was called, there is, IMHO, a starting velocity vector that was in the opposite direction of the jumpBackward() direction (or still moving forward) due to the momentum. So the 1st jumpBackward() function's range was shorter than the 1st jumpForward()'s range. Also, the 1st jumpForward()'s range is shorter than the 2nd because the 2nd already has the starting velocity from the 1st's momentum. So, how do I solve that?
3. The frogger game is only 4 directional one so I just make it yaw 90, -90, or 180 degree and then move (jump) it forward. But because the mYaw is in radian, IMHO the yaw is not truly 90, -90, or 180. I've experimented it myself and found out that the yaw-ed orientation is slightly off. So, how do I make the yaw angle to be exactly either 90 / -90 / 180 degree? Or better yet, how do I make the player / control object's orientation to be parallel to either x / -x / y / -y axis of the world coordinate?
4. I need to restrict the player inside the static camera's viewport so it cannot out of the camera's sight. How do I do that?
Hope they are not too much to ask. Thanks a lot in advance. :D
#10
Now that those problems are solved, there's another problem with the camera. But I guess I'll make a new thread for this. Consider this thread closed and solved. Thanks guys.
11/13/2007 (8:24 am)
Hi all. After doing some more nasty hacks, I've solved all those problems above. Heck, I don't even use the drag at all. I've finally killed the physics. Well, the physics for the frog only because all the other objects still use the normal realistic physics. Now that those problems are solved, there's another problem with the camera. But I guess I'll make a new thread for this. Consider this thread closed and solved. Thanks guys.
#11
11/13/2007 (8:29 am)
Bullitt, what was your strategy for increasing responsiveness?
#12
11/13/2007 (7:28 pm)
Well, I just play around with jumpForce and runForce. Increasing one and decreasing the other. Also I 'killed' the momentum by setting the mMovement.x and mMovement.y to zero if it hits the ground. A nifty hack that doesn't require the drag at all. Also, with the momentum out of the picture, I can set the jumpDelay with any value I want.
#13
The schedule method:
11/15/2007 (6:59 pm)
BTW, to make my frog can only change direction when it's landing but can get inputs when it's still jumping, I used scheduling like this:function jumpForward(%val)
{
//If pressed
if ( %val )
{
echo("Forward=1");
$mvForwardAction = 200;
//$mvTriggerCount2 += $mvTriggerCount2 & 1 == %val ? 2 : 1;
//Set the orientation flag to north
$currentKeyPress = 0;
if ($currentKeyPress==$frogOrientation)
{
echo("mvTriggerCount2 before=",$mvTriggerCount2);
$mvTriggerCount2++;
echo("mvTriggerCount2 after=",$mvTriggerCount2);
}
else
{
cancel($frogMoveSchedule);
$frogMoveSchedule = schedule(100,0,moveSched);
}
}
//If released
else
{
$mvForwardAction = 0;
//$mvTriggerCount2 += $mvTriggerCount2 & 1 == %val ? 2 : 1;
//$mvTriggerCount2 = 0;
cancel($frogMoveSchedule);
if ($mvTriggerCount2%2 == 1)
{
echo("mvTriggerCount2 before=",$mvTriggerCount2);
$mvTriggerCount2++;
echo("mvTriggerCount2 after=",$mvTriggerCount2);
}
$currentKeyPress = 4;
echo("Forward=0");
}
}
function jumpBackward(%val)
{
//If pressed
if ( %val )
{
echo("Backward=1");
$mvForwardAction = 200;
//$mvTriggerCount2 += $mvTriggerCount2 & 1 == %val ? 2 : 1;
//Set the orientation flag to north
$currentKeyPress = 1;
if ($currentKeyPress==$frogOrientation)
{
echo("mvTriggerCount2 before=",$mvTriggerCount2);
$mvTriggerCount2++;
echo("mvTriggerCount2 after=",$mvTriggerCount2);
}
else
{
cancel($frogMoveSchedule);
$frogMoveSchedule = schedule(100,0,moveSched);
}
}
//If released
else
{
$mvForwardAction = 0;
//$mvTriggerCount2 += $mvTriggerCount2 & 1 == %val ? 2 : 1;
//$mvTriggerCount2 = 0;
cancel($frogMoveSchedule);
if ($mvTriggerCount2%2 == 1)
{
echo("mvTriggerCount2 before=",$mvTriggerCount2);
$mvTriggerCount2++;
echo("mvTriggerCount2 after=",$mvTriggerCount2);
}
$currentKeyPress = 4;
echo("Backward=0");
}
}
function jumpLeft(%val)
{
//If pressed
if ( %val )
{
echo("Left=1");
$mvForwardAction = 200;
//$mvTriggerCount2 += $mvTriggerCount2 & 1 == %val ? 2 : 1;
//Set the orientation flag to north
$currentKeyPress = 2;
if ($currentKeyPress==$frogOrientation)
{
echo("mvTriggerCount2 before=",$mvTriggerCount2);
$mvTriggerCount2++;
echo("mvTriggerCount2 after=",$mvTriggerCount2);
}
else
{
cancel($frogMoveSchedule);
$frogMoveSchedule = schedule(100,0,moveSched);
}
}
//If released
else
{
$mvForwardAction = 0;
//$mvTriggerCount2 += $mvTriggerCount2 & 1 == %val ? 2 : 1;
//$mvTriggerCount2 = 0;
cancel($frogMoveSchedule);
if ($mvTriggerCount2%2 == 1)
{
echo("mvTriggerCount2 before=",$mvTriggerCount2);
$mvTriggerCount2++;
echo("mvTriggerCount2 after=",$mvTriggerCount2);
}
$currentKeyPress = 4;
echo("Left=0");
}
}
function jumpRight(%val)
{
//If pressed
if ( %val )
{
echo("Right=1");
$mvForwardAction = 200;
//$mvTriggerCount2 += $mvTriggerCount2 & 1 == %val ? 2 : 1;
//Set the orientation flag to north
$currentKeyPress = 3;
if ($currentKeyPress==$frogOrientation)
{
echo("mvTriggerCount2 before=",$mvTriggerCount2);
$mvTriggerCount2++;
echo("mvTriggerCount2 after=",$mvTriggerCount2);
}
else
{
cancel($frogMoveSchedule);
$frogMoveSchedule = schedule(100,0,moveSched);
}
}
//If released
else
{
$mvForwardAction = 0;
//$mvTriggerCount2 += $mvTriggerCount2 & 1 == %val ? 2 : 1;
//$mvTriggerCount2 = 0;
cancel($frogMoveSchedule);
if ($mvTriggerCount2%2 == 1)
{
echo("mvTriggerCount2 before=",$mvTriggerCount2);
$mvTriggerCount2++;
echo("mvTriggerCount2 after=",$mvTriggerCount2);
}
$currentKeyPress = 4;
echo("Right=0");
}
}The schedule method:
//Current movement key pressed
//0 = north, 1 = south, 2 = west, 3 = east, 4=no direction
$currentKeyPress = 4;
function moveSched()
{
echo("Schedule !!");
//If key pressed != orientation
if ( ($frogOrientation != $currentKeyPress)&& !$isJumpFlag)
{
//If
switch ($frogOrientation)
{
case 0: //If facing north
if ($currentKeyPress==1)
$mvYaw += 3.14159265359;
else if ($currentKeyPress==2)
$mvYaw -= 1.57079632679;
else if ($currentKeyPress==3)
$mvYaw += 1.57079632679;
break;
case 1: //If facing south
if ($currentKeyPress==0)
$mvYaw += 3.14159265359;
else if ($currentKeyPress==2)
$mvYaw += 1.57079632679;
else if ($currentKeyPress==3)
$mvYaw -= 1.57079632679;
break;
case 2: //If facing west
if ($currentKeyPress==0)
$mvYaw += 1.57079632679;
else if ($currentKeyPress==1)
$mvYaw -= 1.57079632679;
else if ($currentKeyPress==3)
$mvYaw += 3.14159265359;
break;
case 3: //If facing east
if ($currentKeyPress==0)
$mvYaw -= 1.57079632679;
else if ($currentKeyPress==1)
$mvYaw += 1.57079632679;
else if ($currentKeyPress==2)
$mvYaw += 3.14159265359;
break;
}
if ($currentKeyPress!=4)
$frogOrientation = $currentKeyPress;
if ($currentKeyPress==0)
{
jumpForward(1);
}
else if ($currentKeyPress==1)
{
jumpBackward(1);
}
else if ($currentKeyPress==2)
{
jumpLeft(1);
}
else if ($currentKeyPress==3)
{
jumpRight(1);
}
}
//If released
else if ($isJumpFlag)
{
$frogMoveSchedule = schedule(100,0,moveSched);
}
}
Torque Owner Bullitt Sesariza
1. I see that the physics in the FPS starter kit is for a humanoid so the jump isn't high enough (for a frog) and can only be far if there's an initial speed from the player. What I need is a jump that is high and far like a frog's jump. How can I do that?
2. When I pressed and hold one of my customized movement button (wsad), it moves like this "jump->run->jump->run->...". Because my main character is a frog, I needed the movement to be "jump->jump->jump->...". How do I do that?
3. Like in the classic frogger, it can only move to 4 direction only. North, south, west, and east. So I needed to turn off the multiple key press (ie. can only press either w / s / a / d, not ,for example, w&a / w&d / etc ). How do I do that?
Thanks a lot in advance.