Game Development Community

Mouse movement

by Bruno · in Torque Game Builder · 04/11/2006 (5:08 am) · 7 replies

Hi.,

I'm tryng to bind the mouse to my ship movement, but it's looking very jaggy.
This is what i'm doing :

playerMap.bind(mouse , "xaxis", mousemovex);
playerMap.bind(mouse , "yaxis", mousemovey);





function mousemovex( %val)
{
  if(%val >= 1 ) playerRight();
  else
  if(%val <= 0 ) PlayerLeft();
  else
  {
    playerLeftStop();	
    playerRightStop();
  }
}


function mousemovey( %val)
{
  if(%val =< 0) playerUp();
  else
  if(%val >= 1) PlayerDown();
  else
  {
    playerUpStop();	
    playerDownStop();
  }
}


I don't know if this is the right way to do it, so any ideias would be apreaciated :)
thanks,

Bruno

#1
04/11/2006 (8:42 am)
Shouldn't it be (%val >= 1) and (%val <= -1) ?
#2
04/11/2006 (10:10 am)
Hmm, don't thikn so .., with those values i can't get a steady movement, the ship is allways either moving up or down.. left or right
#3
04/11/2006 (10:45 am)
Perhaps I should step back. What's your goal? Please define how you'd like the input to work, then we can discuss implementation.


There are several possible models that come to mind:

1) the mouse is a guide for how and where to move: Depending on which edge of the screen the mouse is near, when you click the button determines the ship starts to move in the direction of the mouse.

2) the mouse directly controls the direction of the ship. In this case, you are going to want to add some "dampening", because 1:1 control will result in very jerky movements.

3) the mouse influences the ship's movements based on gesture. Clicking and dragging in a direction causes the ship to change velocity and direction.

Are you trying to do one of these 3 or something different?
#4
04/11/2006 (10:48 am)
Ahh, i see what you mean..,
My goal is 2 ) , the mouse directly controls the direction of the ship., how exactly would i add a "dampening" ?
Instead of -1,1 something like -1.2, 1-2 ??
#5
04/11/2006 (2:59 pm)
Ok, i just been tryng with bigger and smaller values in %val , but somehow it doens't look this well ever get smooth like this...
#6
04/11/2006 (8:47 pm)
"dampening" means that you soften out the inputs to give a smoother move. The easiest way to accomplish what you are looking for is to separate your input handling from your movement functionality and have the velocity measured as a moving average of the mouse inputs.

$mouseVelocityXCount = 0;

actionMap.Bind("mouse0", "x-axis", "mouseX");
actionMap.Bind("mouse0", "y-axis", "mouseY");

function mouseX(%val)
{
   $mouseVelocityXSamples[$mouseVelocityXCount] = %val;
   $mouseVelocityXCount = ($mouseVelocityXCount + 1) % 10;

   for (%i = 0; %i < 10; %i++)
     %sumX = $mouseVelocityXSamples[%i];

   $mouseVelocityX = %sumx / 10;
}

function t2dSceneGraph::onUpdateScene(%this)
{
   $player.setLinearVelocity($mouseVelocityX SPC $mouseVelocityY);
}

Something like that... I haven't built this out completely and tested it, but this is the right direction. The goal is to smooth out the inputs by averaging the last N (in this case N = 10). You may find that you want N larger or smaller, and you may need to handle the case of what happens if the mouse stops moving.

Make sense?
#7
04/12/2006 (2:53 am)
Thanks Jason..,

I got the movement smoothing, kind of, using most of your example, but what is making things harder, is the situation when the mouse stops moving.,
This is what i'm doing :

function mousemovex( %val)
{
   $mouseVelocityXSamples[$mouseVelocityXCount] = %val;
   $mouseVelocityXCount = ($mouseVelocityXCount + 1) % 4;

   for (%i = 0; %i < 4; %i++)
     %sumX = $mouseVelocityXSamples[%i];

   %tempX = %sumx / 4;

   $mouseVelocityX += %tempX;

   if($mouseVelocityX > 40) $mouseVelocityX = 40;
   if($mouseVelocityX < -35) $mouseVelocityX = -35;
   if( (%val >= -1) && (%val < 1) ) $mouseVelocityX = 0;
}

function t2dSceneGraph::onUpdateScene(%this)
{
   $player.setLinearVelocityX($mouseVelocityX);
}

The deal is that, if i make a too fast movement on the mouse, he gains speed, and if i dont toutch the
mouse anymore, the ship keeps going forward, as if the %val is with a high value.
This gives a big lack of precision in the movement, because if we move the mouse too fast, we need to compensate in the oposite direction so that the ship stops.