Game Development Community

SetAngularVelocity, but now i want it to move foward but can't

by Hugo Queiriga · in Torque Game Builder · 11/08/2005 (10:35 am) · 15 replies

I've set it so that my player sprite rotates to left or right, but now i can't get it to move foward to where the sprite is facing.
tried various things, but i'm stunped on this.
Any help would be appreciated.

#1
11/08/2005 (10:59 am)
To make this kind of action easier, I've added some new calls to the SDK, these are...

%obj.setForwardMovementOnly(%status) - The object will only move to it's zero-degree position. If you rotate the object, it'll move in that "forward" direction. In other words, your sprite will always face the direction it's moving. If the sprite turns, it moves in that new direction. You can use this for vehicles, missiles etc.

%obj.setForwardSpeed(%speed) - Obviously sets the speed of movement. You can still apply velocity/impulse changes but in 'forward mode' they are converted into forward movement only.

Of course, this is in the v1.1.0 release. For the v1.0.2 release, you'll need to use "%obj.setLinearVelocityPolar()" to specify a direction to move.

- Melv.
#2
11/08/2005 (11:47 am)
Thanks for the post melv,
But i've tried that with this code

function playerFoward()
	{
	// Set the player moving Foward.
	$player.setLinearVelocityPolar(10)
	}
	function playerFowardStop()
	{
	// If we're moving Foward then nullify any Foward movement.
	if ( $player.getLinearVelocityPolar() < 0 )
	$player.setLinearVelocityPolar( 0 );
but it won't move in the direction of the rotation
#3
11/08/2005 (12:03 pm)
I think you need an angle and a force for the *Polar functions

$player.setLinearVelocityPolar(%angle, 10);
#4
11/08/2005 (12:10 pm)
Yeah just done this
function playerFoward()
	{
	// Set the player moving Foward.
	$player.setLinearVelocityPolar(($player.getRotation()),10);
	}

and it moves foward to where it rotated but it doesn't stop it keeps moving so i added this which i think is totally incorrect, it doesn't stop.

function playerFowardStop()
	{
	// If we're moving Foward then nullify any foward movement.
	if ( $player.getLinearVelocityPolar(($player.getRotation())< 0 ));
	$player.setLinearVelocityPolar(($player.getRotation()), 0 );
	}

any ideas would be good, thanks fo rthe replies
#5
11/08/2005 (12:29 pm)
This is what I am using for CW and CCW rotations:
function playerCW()
{
   $player.setAngularVelocity( $rotateSpeed );
   $turnCW=true;
   playerCWSched();
}


function playerCWSched()
{
   %playerRot = $player.getRotation();
   // Game specific - stop rotation at 85deg
   if (%playerRot >= 85) {
       $player.setRotation(85);
       playerCWStop();
   }
   
   if($turnCW)
   {
      //$player.setLinearVelocityPolar(%playerRot,10);
      //Every 250 milliseconds apply thrust
      schedule( 250, 0, "playerCWSched");
   }
}


function playerCWStop()
{
   if ( $player.getAngularVelocity() > 0 )
      $player.setAngularVelocity( 0 );
   $turnCW=false;
}



function playerCCW()
{
   $player.setAngularVelocity( -$rotateSpeed );
   $turnCCW=true;
   playerCCWSched();
}


function playerCCWSched()
{
   %playerRot = $player.getRotation();
   // Game specific - stop rotation at 85deg
   if (%playerRot <= -85) {
       $player.setRotation(-85);
       playerCCWStop();
   }
   
   if($turnCCW)
   {
      //Every 250 milliseconds apply thrust
      schedule(250, 0, "playerCCWSched");
   }
}


function playerCCWStop()
{
   if ( $player.getAngularVelocity() < 0 )
      $player.setAngularVelocity( 0 );
   $turnCCW=false;
}
#6
11/08/2005 (12:33 pm)
I really can't wait for this upcomming T2D release. :)
#7
11/09/2005 (8:48 am)
Jerry,

Does your player move until you press the opposite action then it stop, this is what i have for my rotation
function playerLeft()
	{
	// Set the player rotating left.
	$player.setAngularVelocity( -100 );
	}
	function playerLeftStop()
	{
	// If we're rotating left then nullify any leftward movement.
	if ( $player.getAngularVelocity() < 0 )
	$player.setAngularVelocity( 0 );
	}
	function playerRight()
	{
	// Set the player rotating right.
	//$player.setLinearVelocityX( 10 );
	$player.setAngularVelocity(100);
	}
	function playerRightStop()
	{
	// If we're rotating right then nullify any rightward movement.
	//if ( $player.getLinearVelocityX() > 0 )
	//$player.setLinearVelocityX( 0 );
	if ( $player.getAngularVelocity() > 0 )
	$player.setAngularVelocity( 0 );
	}

The only issue i have is with the movement foward.
#8
11/09/2005 (9:42 am)
My player rotates when the key is pressed and stops when released. Here's my player's action map

playerMap.bindCmd(keyboard, "left",  "playerCCW();", "playerCCWStop();");
    playerMap.bindCmd(keyboard, "right", "playerCW();", "playerCWStop();");
#9
11/09/2005 (9:45 am)
How do you get it to move foward?

I was thinking of adding some sort of lag in the movement like you need to counteract the rotation movememt to be able to stop. But for now all i want it to do is go foward on the rotation.
#10
11/09/2005 (9:55 am)
Hugo,

To make your object move in a specific direction, do...

%obj.setLinearVelocityPolar( %angle, %speed );

If you want to move in the direction it's "facing" e.g. zero-degrees, do...

%obj.setLinearVelocityPolar( %obj.getRotation(), %speed );

- Melv.
#11
11/09/2005 (10:10 am)
Hi Melv,

Thanks again for your post, this is what ive got and it works this was from your previous post, my only issue now is that i can't get it to stop.

function playerFoward()
	{   // Set the player moving Foward.   
	$player.setLinearVelocityPolar($player.getRotation,100);   
	}
#12
11/09/2005 (11:08 am)
Get this error when i go on the console.

T2D/client/client.cs Line: 117 - Syntax error.
>>> Advanced script error report. Line 233.
>>> Some error context, with ## on sides of error halt:
^// ************************************************************************

^

function ##p##layerFoward()

^{ // Set the player moving Foward.

^$player.setLinearVelocityPolar($player.getRotation(),100);
#13
11/09/2005 (11:12 am)
@Hugo
I think that the problem is in the way you are checking the linear velocity. Try this code, and let me know if it works:


function playerFowardStop()   
{
    // If we're moving Foward then nullify any foward movement.   
    if ( getword($player.getLinearVelocityPolar(),1) > 0 )   
         $player.setLinearVelocityPolar(($player.getRotation()), 0 );
}

@Melv
Just a suggestion for the "setForwardSpeed" method: it would be really nice to give it an angle offset (relative to current heading) in addition to speed. This will let us simulate something like "strafing" while moving forward.
BTW, from what I could understand reading all the posts about the new T2D release, it will rock!

Bye,
Jacopo

Edit: Typos
#14
11/09/2005 (12:10 pm)
Thanks Jacopo, it worked a treat now, trying to do what you were saying strafe, since i'm doing a space game!
#15
11/10/2005 (2:36 pm)
Glad to help, Hugo. :-)

Bye,
Jacopo