Incremental Running...
by Lateral Punk · in Torque Game Engine · 01/11/2009 (7:03 am) · 10 replies
Hi,
I'm trying to figure out the best way to do incremental running in torque. What I mean is this: the faster the player presses a button e.g. the 'S' key, the faster they run, and vice-versa. If the key is not being pressed, they are stopped. I figure this would involve changes in defaultbind.cs, MoveManager, and Player, but I don't quite know the way to go about it. I'm also thinking that there is probably a similar resource out there on GG where someone has implemented something like this e.g. http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=2697. If someone can point me in the right direction, that would be great!
thanks in advance
I'm trying to figure out the best way to do incremental running in torque. What I mean is this: the faster the player presses a button e.g. the 'S' key, the faster they run, and vice-versa. If the key is not being pressed, they are stopped. I figure this would involve changes in defaultbind.cs, MoveManager, and Player, but I don't quite know the way to go about it. I'm also thinking that there is probably a similar resource out there on GG where someone has implemented something like this e.g. http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=2697. If someone can point me in the right direction, that would be great!
thanks in advance
#2
01/11/2009 (8:22 am)
I looked further into that resource, and while it does make the player spring, it's not quite what i'm looking for e.g. it is a toggle, and i'm looking for something for time based. It's just that that search on GG really sucks, so it's hard to find anything relevant, but that's another beef for another day :)
#3
the easiest (and script-only) solution will doing something like that:
You decide how many "presses" you need to have so it will run full-speed. I take for example 5
Than we start the schedule for checking and altering speed with every tick.
I think the code is self-explanatory so it will be easy to adopt it for your own use.
Feel free to play with different values for delay and amount of ticks.
The only thing you will need to is to update your Player class to use different animations depending on the speed. The sprint resource you have mentioned is a good choice (as Ted mentioned) to start going deep into the code.
01/11/2009 (8:22 am)
@Lateral Punk:the easiest (and script-only) solution will doing something like that:
You decide how many "presses" you need to have so it will run full-speed. I take for example 5
Than we start the schedule for checking and altering speed with every tick.
I think the code is self-explanatory so it will be easy to adopt it for your own use.
$ticker::maxKeyPressCount = 5; // 5 ticks to get to 100%, every tick = 20% of speed
$ticker::timeBetweenTicks = 500; // every 500 ms we slowdown by 20% (see above), so 2.5 seconds to full stop if no key-presses
function moveforward(%val)
{
if(!%val) return; // We don't "count-in" when user release key button
// Increase amount of key-presses
if ($tmp::pressCnt<$ticker::maxKeyPressCount)
$tmp::pressCnt++;
// Update the move (inform server on change)
$mvForwardAction = ($tmp::pressCnt/$ticker::maxKeyPressCount) * $movementSpeed;
cancel($tmp::sch::moveForwardTicker);
$tmp::sch::moveForwardTicker = schedule($ticker::timeBetweenTicks, 0, tickMoveForward);
}
function tickMoveForward()
{
cancel($tmp::sch::moveForwardTicker);
// We decrease the press count on every tick (decide by yourself how fast it will slowdown
if($tmp::pressCnt>0)
$tmp::pressCnt--;
$mvForwardAction = ($tmp::pressCnt/$ticker::maxKeyPressCount) * $movementSpeed;
if ($tmp::pressCnt>0) $tmp::sch::moveForwardTicker = schedule($ticker::timeBetweenTicks, 0, tickMoveForward); // re-schedule if we are still moving (no schedules for idling player
}This code is not tested but *should* work.Feel free to play with different values for delay and amount of ticks.
The only thing you will need to is to update your Player class to use different animations depending on the speed. The sprint resource you have mentioned is a good choice (as Ted mentioned) to start going deep into the code.
#4
thanks' that's definitely a start. It looks like it may just work as you coded. Let me give it a shot, and i'll let you know!
thanks
01/11/2009 (8:28 am)
Hey bank!thanks' that's definitely a start. It looks like it may just work as you coded. Let me give it a shot, and i'll let you know!
thanks
#5
thanks
01/11/2009 (8:46 am)
Yup, that definitely did the trick Bank! I'm just fiddling with the constants to get the right speed that I'm looking for. Question: If i wanted to simulate two keys "R" for right foot and "L" for left foot being pressed, than I guess I would bind both those keys to moveForward right? I'm going to look into the Player class for animation, and report back.thanks
#6
be sure to bind moveforwardLeft and moveforwardRight and remove/update config.cs :)
P.S. Again, typed everything right here in forum so double check if there are any typoos or errors ;)
good luck!
01/11/2009 (8:58 am)
If you want to separate left/right ticks, you will need to "count" them separately (different bindings):$ticker::maxKeyPressCount = 5;
$ticker::timeBetweenTicks = 500;
$ticker::slowDownIfWrong = 1; // Try 2 to slowdown fast
function moveforwardLeft(%val)
{
if(!%val) return;
// First we need to check if the previous was "none" or "right" tick:
if($tmp::tickType==0 || $tmp::tickType==1)
{
// Increase if we match or was standing still
if ($tmp::pressCnt<$ticker::maxKeyPressCount)
$tmp::pressCnt++;
}
else
{
// Decrease if no match (slowdown, can be)
$tmp::pressCnt--; // you can slowdown more like $tmp::pressCnt -=2;
if($tmp::pressCnt<0) $tmp::pressCnt=0;
}
$tmp::tickType = 2; // save last state: 0-nothing pressed, any key works to start running
// Update the move (inform server on change)
$mvForwardAction = ($tmp::pressCnt/$ticker::maxKeyPressCount) * $movementSpeed;
cancel($tmp::sch::moveForwardTicker);
$tmp::sch::moveForwardTicker = schedule($ticker::timeBetweenTicks, 0, tickMoveForward);
}
function moveforwardRight(%val)
{
if(!%val) return;
// First we need to check if the previous was "none" or "left" tick:
if($tmp::tickType==0 || $tmp::tickType==2)
{
// Increase if we match or was standing still
if ($tmp::pressCnt<$ticker::maxKeyPressCount)
$tmp::pressCnt++;
}
else
{
// Decrease if no match (slowdown, can be)
$tmp::pressCnt -= $ticker::slowDownIfWrong;
if($tmp::pressCnt<0) $tmp::pressCnt=0;
}
$tmp::tickType = 1; // save last state: 0-nothing pressed, any key works to start running
// Update the move (inform server on change)
$mvForwardAction = ($tmp::pressCnt/$ticker::maxKeyPressCount) * $movementSpeed;
cancel($tmp::sch::moveForwardTicker);
$tmp::sch::moveForwardTicker = schedule($ticker::timeBetweenTicks, 0, tickMoveForward);
}
function tickMoveForward()
{
cancel($tmp::sch::moveForwardTicker);
// We decrease the press count on every tick (decide by yourself how fast it will slowdown
if($tmp::pressCnt>0)
$tmp::pressCnt--;
if($tmp::pressCnt==0) $tmp::tickType = 0;
$mvForwardAction = ($tmp::pressCnt/$ticker::maxKeyPressCount) * $movementSpeed;
if ($tmp::pressCnt>0) $tmp::sch::moveForwardTicker = schedule($ticker::timeBetweenTicks, 0, tickMoveForward);
}Something like that should work..be sure to bind moveforwardLeft and moveforwardRight and remove/update config.cs :)
P.S. Again, typed everything right here in forum so double check if there are any typoos or errors ;)
good luck!
#7
thanks for this, I'll have to try it out. One last question, how best would I bind the above so that it matches the player's animation? e.g. if L is pressed, the left leg moves forward? I'm guessing I need to control the playback of the animation sequences, but I don't know how best to approach it. any ideas there?
thanx
01/12/2009 (7:34 am)
Hey bank,thanks for this, I'll have to try it out. One last question, how best would I bind the above so that it matches the player's animation? e.g. if L is pressed, the left leg moves forward? I'm guessing I need to control the playback of the animation sequences, but I don't know how best to approach it. any ideas there?
thanx
#8
"Homebrew Wrestling" by David Horn is the thing to look at -- how the custom animation handling could be done. Check his .blog posts series to see the progress. May be getting through his posts on forums can help you too.
Good luck with research!
01/12/2009 (1:22 pm)
Hm. thats a bit harder question and with no easy solution I can think of right now.."Homebrew Wrestling" by David Horn is the thing to look at -- how the custom animation handling could be done. Check his .blog posts series to see the progress. May be getting through his posts on forums can help you too.
Good luck with research!
#9
01/12/2009 (1:27 pm)
Thanks Bank!
#10
I can't get my head around how to make the player animate according to it's speed. do you got any other pointers left for me?
02/16/2009 (7:21 pm)
Hey bank,I can't get my head around how to make the player animate according to it's speed. do you got any other pointers left for me?
Torque 3D Owner Ted Southard