Game Development Community

T2D Rotations + velocity

by Chris · in Torque Game Builder · 03/09/2005 (4:12 pm) · 1 replies

I have begun work on a fishing game in which you aim with mouse (the hands point to the mouse cursor) and when you click it fires. For now I have the bobber fly straight to the target and plan to change that later on. So I have to use some velocity changing if I wish to have the projectile fly to the pointer.

First I made a makeshift function that takes the T2D degrees setup to something that I could use sin/cos on and it would work.

function getAngle(%ang) {
	%angle = 0;
	if(%ang >= 0 && %ang <= 90) {
		%angle = 90-%ang;
	}
	else if(%ang > 90 && %ang <=180) {
		%angle = mabs(%ang-90) * -1;
	}
	else if(%ang < 0 && %ang >= -90) {
		%angle = 180-(%ang+90);
	}
	else if(%ang > -180 && %ang < -90) {
		%angle = 180+mabs(%ang+90);
	}
	return %angle;
}

Next for setting the objects velocity (%angle in this is found by)
function sceneWindow2D::onMouseDown( %This, %Modifier, %WorldPosition, %MouseClicks )
{
    	if($player !$= "") {
   		%mxpos = getWord(%WorldPosition,0);
  		 %mypos = getWord(%WorldPosition,1);
   
   		//get player positions
   		%playerpos = $player.getPosition();
   		%px = getWord(%playerpos,0);
   		%py = getWord(%playerpos,1);
   
  		 %ang = getAng(%mxpos, %mypos, %px, %py);
		playerFire(%ang);
	}	
}
this is found in the playerFire function
%projectile.setLinearVelocityY($projspeed * msin(getAngle(%angle)));
	%projectile.setLinearVelocityX($projspeed * mcos(getAngle(%angle)));

My question is that even with this there are problems with the speed. I am confused as to what I have to change inorder to make it work. Anyone have any ideas on this or have an easier way to do this?

#1
03/10/2005 (1:47 am)
Christopher,

You didn't think I'd make you work that hard to do something so simple did you? I'm certainly not that evil. ;)

It's worth having a look through the reference manual for the "setXXXXPolar" functions. These do all the math for you and all you need to specify is the angle (in degrees) and other related parameters such as speed. These are available for tilemaps, sprites, particles etc. In this case, the "setLinearVelocityPolar()" will work for you. All you need to do is calculate the angle. You can calculate the angle quickly with the "mAtan()" function.

Also note the use of the"isObject()" function to determine if you've got a valid T2D object which is a much neater way to do it.

I extrapolated what you needed a little but the detail of what you need is in the mouse-callback function.

$speed = 15;

function sceneWindow2D::onMouseDown( %This, %Modifier, %WorldPosition, %MouseClicks ){
	// Do we have a player object?
	if( isObject($player) )
	{
		// Yes, so fetch mouse-components.
		%mxpos = getWord(%WorldPosition,0);
		%mypos = getWord(%WorldPosition,1);
		
		// Get players position and fetch components.
		%playerpos = $player.getPosition();
		%px = getWord(%playerpos,0);
		%py = getWord(%playerpos,1);
		
		// Calculate Angle from player to mouse (convert to degrees).
		%angle = mRadToDeg( mAtan( %mxpos-%px, %py-%mypos ) );
		
		// Fire Projectile.
		playerFire( %angle );
	}   
}

function playerFire( %angle )
{
   // Create some player fire...
   %projectile = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
   %projectile.setPosition( $player.getPosition() );
   %projectile.setSize( 2 );
   %projectile.setImageMap( particles1ImageMap );
   %projectile.setWorldLimit( kill, "-20 -20 20 20", true );

   // Set the projectile moving in the specified direction/speed.
   %projectile.setLinearVelocityPolar( %angle, $speed );
}

Hope this helps,

- Melv.