Game Development Community

Mouse shooting from ship

by Kayrel Parcen Vandal · in Torque Game Builder · 01/26/2009 (11:50 am) · 1 replies

Hello, I am making a small 2D shooter (left to right) and I have the ship moving and everything else.

I was wondering how I could make the ship shoot in the direction the mouse is. If I have a target cursor on the top then the ship will shoot in that direction.

I hope that was clear. Anyways, thanks for anybody in advance if they can help me.

#1
01/27/2009 (6:59 pm)
I have done this a few times... you need to get the vector from your ship to your mouse.. I'll see if I can dig up some of my code to help:


Here is the basics.. you need to use sceneWindow2d.getMousePosition() then fidn the angle between it.. the code below should save you a bunch of time. I used it to draw an arrow between, which is trickier then what you need to do unless you are drawing a lazer or something.. then once you have the angle you can take a bullet or whatever and spawn it using the rotation I showed you and setimpulseforce.. enjoy.
function updateChargeArrow(){

//$chargeArrow.setVisible($arrowVis);
//$chargeArrowGauge.setVisible($arrowVis);

%srcPos = $pBall.getPosition();
%dstPos = sceneWindow2D.getMousePosition();
%angleVector=calculateAngle(%srcPos,%dstPos);
%angleAdd=t2dVectorDistance(%srcPos,%dstPos);
%angleAdd*=2; //double the lengh since I'm using a half image

$powerMeter1.setRotation(%angleVector-90);
$powerMeter2.setRotation(%angleVector-90);
$powerMeter3.setRotation(%angleVector-90);
$powerMeter4.setRotation(%angleVector-90);
$chargeArrow.setRotation(%angleVector-90);
$chargeArrow.setWidth(%angleAdd);


$chargeArrowGauge.setRotation(%angleVector-90);
%gaugeWidth=  %angleAdd*($pBall.jumpPower/$pBall.maxJumpPower);
$chargeArrowGauge.setWidth(%gaugeWidth);   




faceOrb(%angleVector);

}