Game Development Community

Player Movement Twitching

by Joshua M Miller · in Torque Game Builder · 03/06/2008 (12:56 pm) · 9 replies

I'm currently working on a top down game and have come into troubles with my players movement. After I tried to add rotation to my player image he began to twitch quite rapidly as you press the movement keys in game. I was wondering if anyone would know how to fix this so I can have my character face the direction he is moving without all the twitching.

Here is the code for my characters movement:

//Binds keys for player movement
new actionmap(keys);

keys.bindcmd("keyboard","left","moveleft();","stopmoveleft();");
keys.bindcmd("keyboard","right","moveright();","stopmoveright();");
keys.bindcmd("keyboard","up","moveup();","stopmoveup();");
keys.bindcmd("keyboard","down","movedown();","stopmovedown();");

keys.push();


function moveleft()
{
player.moveleft = true;
}

function stopmoveleft()
{
player.moveleft = false;
}

function moveup()
{
player.moveup = true;
}

function stopmoveup()
{
player.moveup = false;
}

function moveright()
{
player.moveright = true;
}

function stopmoveright()
{
player.moveright = false;
}

function movedown()
{
player.movedown = true;
}

function stopmovedown()
{
player.movedown = false;
}

function player::updateHorizontal(%this)
{
if(%this.moveleft)
{
%this.setLinearVelocityX(-40);
%this.setrotation(-90);
}

if(%this.moveRight)
{
%this.setLinearVelocityX(40);
%this.setrotation(90);
}

if(!%this.moveleft && !%this.moveright)
{
%this.setLinearVelocityX(0);
}
}

function player::updateVertical(%this)
{
if(%this.moveup)
{
%this.setLinearVelocityY(-40);
%this.setrotation(0);
}

if(%this.movedown)
{
%this.setLinearVelocityY(40);
%this.setrotation(180);
}

if(!%this.moveup && !%this.movedown)
{
%this.setLinearVelocityY(0);
}
}

function SceneGraph::onUpdateScene()
{
if (isObject(player))
{
player.updateMovement();
}
}

function player::updateMovement(%this)
{
%this.updateHorizontal();
%this.updateVertical();
}

//makes camera follow player
function player::onLevelLoaded()
{
%force = 20;
%keyforce = 0;
scenewindow2d.mount(player, "0 0", %force, 1);
player.lives = 3;
}



This is the code I tried to add to stop the twitching because I thought it might be caused by the constant updating of the players rotation and I don't understand why it doesn't work.



function player::updateHorizontal(%this)
{
if(%this.moveleft)
{
%this.setLinearVelocityX(-40);
if(%this.getrotation != -90)
{
%this.setrotation(-90);
}
}

If someone could help me out that would be awesome.

Thanks,
Josh

About the author

Recent Threads


#1
03/12/2008 (12:34 pm)
If i understand right you are tying to make player move up/down/left/right with W-S-A-D commands (or arrow keys) wich makes him able to move in 8 different dirrections.

And when he moves on some direction you want him to face that direction, right?

If that is right, ill try to make one code for you. Player's name is "player", he doesn't have a class or something?
#2
03/13/2008 (5:03 pm)
Yes, I'm using movement with the arrow keys, the player's name is "player" and he doesn't have a class or super class. I just need him to face the direction he is moving without all the twitching of the character. It's kind of acting like if you were to try to create a gui by making an object move the same speed as you on the screen, there is a weird twitching of the object when it tries to move with the screen.
#3
03/13/2008 (6:46 pm)
You forgot the parentheses after getRotation()

function player::updateHorizontal(%this)
{
if(%this.moveleft)
{
%this.setLinearVelocityX(-40);
if(%this.getRotation() != -90)    //or  if( %this.rotation != -90 )
{
%this.setrotation(-90);
}
}

Also, your code is a little more segmented than it needs to be. Updating player movement on the scenegraph onUpdate callback is overkill. Set up a 25 millisecond timer on the player instead. Lastly, you can use a combination of "else if's" and "else" statements instead of just using "if's".

ie:

function player::updateVertical(%this)
{
if(%this.moveup && !%this.movedown)
{
%this.setLinearVelocityY(-40);
%this.setrotation(0);
}

else if(!%this.moveup && %this.movedown)
{
%this.setLinearVelocityY(40);
%this.setrotation(180);
}

else
{
%this.setLinearVelocityY(0);
}
}
#4
03/13/2008 (6:56 pm)
Quote:
Also, your code is a little more segmented than it needs to be. Updating player movement on the scenegraph onUpdate callback is overkill. Set up a 25 millisecond timer on the player instead.

Actually, that's overkill as well. There already exists a 32 millisecond callback called ::onUpdate() that can be used for the same purpose.
#5
03/14/2008 (7:57 am)
I made a new thread about W-S-A-D movement to be available to everyone.

You can see the code here:
www.garagegames.com/mg/forums/result.thread.php?qt=73073#521424
#6
03/14/2008 (1:13 pm)
Thanks for the correction, Stephen. My only concern: is the onUpdate function consistent? Like, if you are getting 10 FPS versus 100 FPS, is the callback still called correctly? Inversely, is onUpdate more consistent than the onTimer?
#7
03/14/2008 (1:17 pm)
This is of interest to me because I am doing a fairly involved platformer, and I'm using a 25 millisecond timer for the movement handler. Is there a better way, such as onUpdate? My preference for timers goes all the way back to when Thomas Buscaglia made a tank movement demo that used a 25 millisecond timer. I learned a lot from that demo!
#8
03/15/2008 (3:43 pm)
I dont know if you saw my code, Josh, but i even implemented the rotation of player that you wanted. And it wasnt easy i can tell you. ;)
#9
03/15/2008 (6:07 pm)
Thanks for the help everyone.