Game Development Community

Direction of travel

by Jon Jorajuria · in Torque Game Builder · 07/20/2006 (12:05 pm) · 5 replies

Is there an easy way to get an objects direction of travel? Here is what I am trying to do:

get the direction of travel for y (+ or -)

if y = +

$player.setLinearVelocityY(200);

if y = -

$player.setLinearVelocityY(-200);

Obviously this is not the real code, just curious if there is a way to get DOT.

#1
07/20/2006 (12:26 pm)
You could have two variables such as $oldYPosition and $currentYPosition. You store the current Y position in $oldYPosition. The next time the object is updated (after one loop), check the current Y Position and store it in $currentYPosition. If $currentYPosition > $oldYPosition then the object is moving upwards. (ie positive). I'm pretty sure that torque has the 0,0 position at the bottom left of the screen so that would be the case. If $currentYPosition < $oldYPosition then it's moving downwards.
#2
07/20/2006 (2:52 pm)
The 0,0 point can move depending on how your camera is defined. By default, it's actually in the centre of the screen.

Surely the easiet way is:
switch ($player.getLinearVelocityY())
{
case > 0:
$player.setLinearVelocityY(200);
case < 0:
$player.setLinearVelocityY(-200);
}
I haven't tested it, but something like that should work.
#3
07/20/2006 (6:20 pm)
Ah, that's a much better method Philip. Looks like it should work.
#4
07/21/2006 (1:52 am)
Just to clarify: positive Y is actually down in TGB. Philip's code still applies, but I thought it was worth mentioning that "if $currentYPosition > $oldYPosition then the object is moving [downwards]". Just as Philip said, "0 0" world coordinates are not technically related to the camera, but the camera's default position (which, just like any other object in TGB, is based on it's center) is "0 0".
#5
07/21/2006 (3:27 pm)
This is the way I went, thank you for pointing me in the right direction:

if ( $player.getLinearVelocityY() > 0 )
   {
      $player.setLinearVelocityY(200);
   }
   else
   {
      $player.setLinearVelocityY(-200);
   }