Game Development Community

Player Movement in Torque 2D

by tylar steger · in Game Design and Creative Issues · 09/16/2012 (5:53 pm) · 7 replies

Hello there!, I am a brand spankin new developer trying to build a awesome 2.5d game. Currently I have been working on character movement. I have the basics down IE:

{
$AshePlayer = %this;


moveMap.bindCmd(keyboard, "w", "AshePlayerUp();", "AshePlayerUpStop();");
moveMap.bindCmd(keyboard, "s", "AshePlayerDown();", "AshePlayerDownStop();");
moveMap.bindCmd(keyboard, "a", "AshePlayerLeft();", "AshePlayerLeftStop();");
moveMap.bindCmd(keyboard, "d", "AshePlayerRight();", "AshePlayerRightStop();");
%force = 20;
sceneWindow2D.mount($AshePlayer, "0 0", %force, true);
}

But what I really want to do is, make so upon the release of "w" for instance, movement doesn't just immediately stop. I would like to move at a set rate for a set amount of time and then stop. All for the slight feeling of momentum, kinda like a skid. This is what I have been able to get to work so far:

function AshePlayerUp()
{
$AshePlayer.setLinearVelocityY( -240);
}

function AshePlayerUpStop()
{
if ( $AshePlayer.getLinearVelocityY() < -35 )

$AshePlayer.setLinearVelocityY( -30 );

}

I have been trying to input a timer into this if statement, sadly all my attempts so far have failed, I was wondering if anyone out there could point me in the right direction. Maybe lead me to some educating resources?

About the author

An indie developer with a dream, to make an RPG for the ages!


#1
09/18/2012 (7:17 am)
@tylar - Each object has a physics field for dampening. If you increase that value, your player will start sliding instead of immediately stopping.
#2
09/18/2012 (5:12 pm)
@ Michael,

Thank you for your response, after messing around with the damping options I am getting to the look and feel for what I want. My only current problem is that I can't seem to get it to apply to the release of a button.
IE: "w" for my upward movement.

To get the damping to apply the way I would like, I need to write it in the Player.cs right? Would it look at all similar to this?:

function AshePlayerUpStop()
{
if ( $AshePlayer.getLinearVelocityY() < -35 )

$AshePlayer.setdamping( 5.000 );

}

Update:

I have recently got it to apply to the release of a button, but once its has been done one time, the damping applies itself to all movement, (this is a problem XD) So the problem now is getting the damping effect to turn back off.
#3
09/18/2012 (6:58 pm)
I have something similar, I want to like "buffer" buttons, if a player jumps but they aren't on the ground yet I still want it to count.

Here's what I'm doing, it works well.
You want to look into the schedule API. You can schedule a call to your custom function that will stop movement.

http://docs.garagegames.com/tgb/official/content/documentation/Component%20Tutorials/Scripting/Scheduling%20Events.html


function Player::moveButton_a(%this, %val)
{
if (%val == 1) //need to check if the player is actually standing on something
{
%grounded = gameStateHandlerObject.checkGrounded(%this);

if (%grounded && %this.disableJump != true)
{
cancel(%this.jumpBufferSchedule); //avoid double hob
%this.jump = %val;
%this.updateMovement(%this);
%this.disableJump();
}
else if (%grounded == false && %this.disableJump != true) //jumping is really annoying when it doesn't work, I don't care if it's timed perfectly, let's buffer a few hundred ms
{
cancel(%this.jumpBufferSchedule); //avoid double hop
%this.jumpBufferSchedule = %this.schedule(100, "jumpBuffer");
}
}
else
{
%this.jump = %val;
%this.updateMovement(%this);
}
}

function Player::disableJump(%this)
{
%this.disableJump = true; //dont double hop
%this.schedule(200,"enableJump");
}
function Player::enableJump(%this)
{
%this.disableJump = false;
}
function Player::jumpBuffer(%this)
{
%grounded = gameStateHandlerObject.checkGrounded(%this);
if (%grounded)
{
%this.disableJump();
%this.jump = 1;
%this.updateMovement(%this);
}
}
#4
09/18/2012 (7:52 pm)
Do you want it to actually slide and slow down, like a lack of friction?
Or keep going at the regular move rate and halt suddenly?

For the latter, you'd want a timer. For the former, you can use just the physics engine, no timers required.

I _think_ you need to track the direction the player is moving, and call setLinearVelocity in an update function

Ie, yourobject.enableUpdateCallback();

function yourobject::onUpdate(%this)
{
if (rightpressed)
{
setLinearVelocity blah blah
}
}


I'm using setImpulseVelocity for this because of how I wanted it to ramp up from a walk to run but slow down faster
http://www.youtube.com/watch?v=kLbB-C_Weko&feature=plcp

#5
09/18/2012 (8:37 pm)
@ Dylan

I would like a slow down to a halt after release, I have been messing with the physics engine for objects, and I have tried to script in a scheduled event like your example. Though it keeps seeming to fail. I can get the object in question to dampin, upon release. Thus the slow down to a halt.

My problem is figuring out how to turn the dampening back off after the it has come to a complete stop.

These are different variations of scripting I have tried:
AshePlayer.enableUpdateCallback();

function AshePlayer::onUpdate(%this)
{
$AshePlayer.setdamping(0.000)
}

function AshePlayerUpStop()
{
if ( $AshePlayer.getLinearVelocityY() < -35 )

schedule(5000, $AshePlayer.setdamping( 5.000 ));
}

//////////////////////////////

function AshePlayerUpStop()
{
if ( $AshePlayer.getLinearVelocityY() < -35 )

$AshePlayer.setdamping( 5.000 );

else if ($AshePlayer.getLinearVelocityY() < 0 )

Cancel ($AshePlayer.setdamping)
}

////////////////////////////


function AshePlayerUpStop()
{
if ( $AshePlayer.getLinearVelocityY() < -35 )

$AshePlayer.setdamping( 5.000 );

else if ($AshePlayer.getLinearVelocityY() < 0 )

$AshePlayer.setdamping( 0.000);
}


Also on a side note thank you for your input!

#6
09/18/2012 (10:46 pm)
Also I have tried this, I am not getting any errors, though it doesn't seem to be doing what i am telling it to

function AshePlayerUpStop()
{

$AshePlayer.setLinearVelocityY( -35 );

if ( $AshePlayer.getLinearVelocityY() == -35 )

schedule(500, "AshePlayerUpStopp", 0);


}

function AshePlayerUpStopp()
{
$AshePlayer.setLinearVelocityY( 0 );
}

#7
11/26/2012 (8:01 am)
500 is half a second. Jack that up to 2000 ms and it should def. be noticeable if it is working.

The setting to -35 and then immediately checking if it is -35 is a little weird. Might as well not have the if statement.

Make use of code tags, it makes reading code on the forums much better.