Game Development Community

MAKING A PROJECTILE: EXTREMELY DESPERATE!!!!!

by Tyler Slabinski · in Torque Game Builder · 04/11/2009 (11:14 am) · 25 replies

This is VERY annoying... I am trying to get an imagemap at these points:

X: -44.000
Y: -9.500

And with speeds of:

$arrowX
$arrowY

Look at the below script, it is a click-drag-shoot sort of projectile. And I am trying to make the sprite appear in the function shootArrow(); and it is being executed at onMouseUp.

//Get the position vector of the mouse when pressed and turn them into variables.

function t2dSceneWindow::onMouseDown(%this, %modifier, %worldPosition, %clicks)
{
     $startPosition = %worldPosition; //Vector for when mouse is clicked

     $dX = getWord(%worldPosition, 0); //X position when mouse is pressed down.
     $dY = getWord(%worldPosition, 1); //Y position when mouse is pressed down.
}

//Get the position vector of the mouse when released and turn them into variables.

function t2dSceneWindow::onMouseUp(%this, %modifier, %worldPosition, %clicks)
{
     $endPosition = %worldPosition;

     $rX = getWord(%worldPosition, 0); //X position when mouse is released.
     $rY = getWord(%worldPosition, 1); //Y position when mouse is released.

     //Execute the functions for getting the angle and power. Then creating the velocity for the arrow.
     convertAngle();
     convertPower();
     convertVelocity();

     //Launch the arrow.
     shootArrow();
}

function convertAngle()
{
     %xSlope = ($dX - $rX);
     %ySlope = ($dY - $rY);

     $angle = mRadToDeg(mAtan(%xSlope, %ySlope)); //Get the arctan of the slopes. Then convert it to degrees.
}

function convertPower()
{
     $distance = t2dVectorDistance($startPosition, $endPosition); //Get the distance between the click and the release.

     //Keep the power from going over 30.
     if($distance > 30)
     {
          $power = 30;
     }
     else
     {
          $power = $distance;
     }
}

function convertVelocity()
{
     //Get the power for how far the arrow will go.
     if ($angle > 90)
     {
          $arrowX = (90 - ($angle - 90)) * ($power / 10);
     }
     else
     {
          $arrowX = $angle;
     }

     //Get the height for the arrow.
     if ($angle != 90)
     {
          $arrowY = ($angle - 90) * ($power / 10);
     }
     else
     {
          $arrowY = 0;
     }
}

function shootArrow() {
     %arrow = new t2dStaticSprite()
     {
          scenegraph = SceneWindow2D.getSceneGraph();
          position = "-44.0 -9.5";
          imageFile = "silverBolt.png";
          class = "silverBolt";
    	  CollisionActiveSend = "1";
          CollisionActiveReceive = "1";
          CollisionPolyList = "-0.250 -0.250 0.250 -0.250 0.250 0.250 -0.250 0.250";
          ConstantForce = "0.000 20.000";    
          ConstantForceGravitic = "1";    
          mountID = "20";
	};
	%arrow.setLinearVelocityX($arrowX);
	%arrow.setLinearVelocityY($arrowY);
}
Page«First 1 2 Next»
#21
04/13/2009 (7:46 pm)
Ahaha!!! Well it worked... But is goes reverse for the Y speed...

Basically, when I aim up, it shoots down.

It fixes it if I reverse it though. Here is my new code before I start the rotation:

//Get the position vector of the mouse when pressed and turn them into variables.

function t2dSceneWindow::onMouseDown(%this, %modifier, %worldPosition, %clicks)
{
     $startPosition = %worldPosition; //Vector for when mouse is clicked

     $dX = getWord(%worldPosition, 0); //X position when mouse is pressed down.
     $dY = getWord(%worldPosition, 1); //Y position when mouse is pressed down.
}

//Get the position vector of the mouse when released and turn them into variables.

function t2dSceneWindow::onMouseUp(%this, %modifier, %worldPosition, %clicks)
{
     $endPosition = %worldPosition;

     $rX = getWord(%worldPosition, 0); //X position when mouse is released.
     $rY = getWord(%worldPosition, 1); //Y position when mouse is released.

     //Execute the functions for getting the angle and power. Then creating the velocity for the arrow.
     convertAngle();
     convertPower();
     convertVelocity();

     //Launch the arrow.
     shootArrow();
}

function convertAngle()
{
     %xSlope = ($dX - $rX);
     %ySlope = ($dY - $rY);

     $angle = mRadToDeg(mAtan(%xSlope, %ySlope)); //Get the arctan of the slopes. Then convert it to degrees.
}

function convertPower()
{
     $distance = t2dVectorDistance($startPosition, $endPosition); //Get the distance between the click and the release.

     //Keep the power from going over 30.
     if($distance > 30)
     {
          $power = 1;
     }
     else
     {
          $power = $distance / 30;
     }
}

function convertVelocity()
{
     //Get the power for how far the arrow will go.
     if ($angle > 90)
     {
          $arrowX = (90 - ($angle - 90)) * $power;
     }
     else
     {
          $arrowX = $angle * $power;
     }

     //Get the height for the arrow.
     $arrowY = ($angle - 90) * $power * -1;
}

function shootArrow() {
     %arrow = new t2dStaticSprite()
     {
          scenegraph = SceneWindow2D.getSceneGraph();
          position = "-44.0 -9.5";
          imageMap = "silverBoltImageMap";
          class = "silverBolt";
    	  CollisionActiveSend = "1";
          CollisionActiveReceive = "1";
          CollisionPolyList = "-0.250 -0.250 0.250 -0.250 0.250 0.250 -0.250 0.250";
          ConstantForce = "0.000 35.000";    
          ConstantForceGravitic = "1";    
          mountID = "20";
	};
	%arrow.setLinearVelocityX($arrowX);
	%arrow.setLinearVelocityY($arrowY);
}
#22
04/15/2009 (6:59 pm)
I cannot for the life of me figure out how to make this math work:

function silverBolt::onTimer(%this)
{
     %X = $arrow.getLinearVelocityX();
     %Y = $arrow.getLinearVelocityY();

     %temp = (%Y / %X);

     $arrow.setRotation(%temp);
}

I tried the following for the math:

%Y / %X
%X / %Y
(%X + %Y) / 2

I even tried getting the tangent with this code:

%temp = mTan(%X, %Y);

Does anyone know how this works? I am good at math, I just never took triginometry yet.
#23
04/16/2009 (12:52 am)
dY/dX gives you the gradient of a slope, not an angle. IIRC, mTan returns its result in radians, so you'll have to convert to degrees.

That said, what exactly is the problem you're having?
#24
04/16/2009 (11:46 am)
I am trying to get an object to face the direction it is going.

This is what it does now:

img2.imageshack.us/img2/1619/epicfailedarrow.png
This is what I want it to do.

img13.imageshack.us/img13/1559/epicwinarrow.png
#25
04/16/2009 (2:42 pm)
Wow, already down 20 threads from the top? Wierd.

BUMP
Page«First 1 2 Next»