Trying to get projectle to hit mouse pointer
by Ezell Burke · in Torque Game Builder · 07/04/2006 (12:47 am) · 6 replies
Hello As this is my first post I would like to give a shout out to the guys behind TGB, thanks I love the work. I am pretty rusty with code so if the answer to my question is to simplistic, please forgive.
I have been fooling around with the shooter tutorial and decided to try to get the missles to shoot out of a rotating cannon to wherever my mouse curser sort of like bubble bobble or like atari's missle command. So I used the moveTo function and placed the mouse cordinates inside like this:
%mxpos = getWord(%WorldPosition,0);
%mypos = getWord(%WorldPosition,1);
%this.moveTo(%mxpos, %mypos, 100, true, true, true, 0.001);
I get an error stating that I have the wrong number of parameters. wich makes me believe that the values that are stored within the my mouse x/y position variables or more than just the x and y coordinates. Can anyone help me get these missles to hit the target I specify?
I have been fooling around with the shooter tutorial and decided to try to get the missles to shoot out of a rotating cannon to wherever my mouse curser sort of like bubble bobble or like atari's missle command. So I used the moveTo function and placed the mouse cordinates inside like this:
%mxpos = getWord(%WorldPosition,0);
%mypos = getWord(%WorldPosition,1);
%this.moveTo(%mxpos, %mypos, 100, true, true, true, 0.001);
I get an error stating that I have the wrong number of parameters. wich makes me believe that the values that are stored within the my mouse x/y position variables or more than just the x and y coordinates. Can anyone help me get these missles to hit the target I specify?
#2
Here is the code for my projectile:
function DummyMissile::fire(%this)
{
%mxpos = getWord(%WorldPosition,0);
%mypos = getWord(%WorldPosition,1);
%this.setWorldLimit( kill, "-55.000 -420.000 600.000 40" );
//%this.setLinearVelocityPolar($pDummy.getRotation(), 10);
%this.moveTo(mxpos,mypos,100, 100, true, true, true, 0.001);
%this.setPosition(%this.player.getPosition());
%this.setImageMap(enemyMissileImageMap);
%this.setSize(11, 5);
%this.setCollisionActive( true, true );
%this.setCollisionPhysics(false, false);
%this.setCollisionCallback(true);
}
and the script for the cannon that is firing the projectile
function Dummy::onLevelLoaded(%this, %scenegraph)
{
$pDummy = %this;
moveMap.bindCmd(keyboard, "space", "$pDummy.createMissile();", "");
}
function SceneWindow2D::onMouseDragged(%this,%mod, %WorldPosition, %mouseClicks)
{
$mxpos = getWord(%WorldPosition,0);
$mypos = getWord(%WorldPosition,1);
pDummy.rotateTo($mxpos,500,true,true,0.01);
}
function Dummy::createMissile(%this)
{
%this.DummyMissile = new t2dStaticSprite()
{
scenegraph = %this.scenegraph;
class = DummyMissile;
player = %this;
};
%this.DummyMissile.fire();
}
As you can see I am rotating the cannon using the rotateTo function with the mouse this seems to work not as accurately as I like but it works. I figured that I could use the mouse coordinates with the moveTo function but the missiles shoot and go know where and I get the error.
07/05/2006 (2:25 pm)
Oh my bad,Here is the code for my projectile:
function DummyMissile::fire(%this)
{
%mxpos = getWord(%WorldPosition,0);
%mypos = getWord(%WorldPosition,1);
%this.setWorldLimit( kill, "-55.000 -420.000 600.000 40" );
//%this.setLinearVelocityPolar($pDummy.getRotation(), 10);
%this.moveTo(mxpos,mypos,100, 100, true, true, true, 0.001);
%this.setPosition(%this.player.getPosition());
%this.setImageMap(enemyMissileImageMap);
%this.setSize(11, 5);
%this.setCollisionActive( true, true );
%this.setCollisionPhysics(false, false);
%this.setCollisionCallback(true);
}
and the script for the cannon that is firing the projectile
function Dummy::onLevelLoaded(%this, %scenegraph)
{
$pDummy = %this;
moveMap.bindCmd(keyboard, "space", "$pDummy.createMissile();", "");
}
function SceneWindow2D::onMouseDragged(%this,%mod, %WorldPosition, %mouseClicks)
{
$mxpos = getWord(%WorldPosition,0);
$mypos = getWord(%WorldPosition,1);
pDummy.rotateTo($mxpos,500,true,true,0.01);
}
function Dummy::createMissile(%this)
{
%this.DummyMissile = new t2dStaticSprite()
{
scenegraph = %this.scenegraph;
class = DummyMissile;
player = %this;
};
%this.DummyMissile.fire();
}
As you can see I am rotating the cannon using the rotateTo function with the mouse this seems to work not as accurately as I like but it works. I figured that I could use the mouse coordinates with the moveTo function but the missiles shoot and go know where and I get the error.
#3
07/05/2006 (2:28 pm)
This seems like it's only doing it when you drag the mouse and not when you click. Maybe intercept onMouseDown? (is that the right one?)
#4
I planed on implementing mouse based firing later so the cannon rotates by dragging left and right and it fires when the space bar is hit. I also tried to using %this.setLinearVelocityPolar($pDummy.getRotation(), 10); wich is commented out. this function seems to make the projectile shoot in the general direction of the mouse but it is not perfect. (I guess it is because it is based off of the rotation of the cannon which is indirectly based of the rotateTo function wich I believe causes the cannon to rotate based on the mouse coordinates.)
07/05/2006 (3:32 pm)
True,I planed on implementing mouse based firing later so the cannon rotates by dragging left and right and it fires when the space bar is hit. I also tried to using %this.setLinearVelocityPolar($pDummy.getRotation(), 10); wich is commented out. this function seems to make the projectile shoot in the general direction of the mouse but it is not perfect. (I guess it is because it is based off of the rotation of the cannon which is indirectly based of the rotateTo function wich I believe causes the cannon to rotate based on the mouse coordinates.)
#5
What I'd probably do is have onMouseDragged() and/or onMouseMove() call a function to rotate your turret. Then within createMissile() you could call:
%mousePos = sceneWindow2D.getMousePosition ();
And use that position for your moveTo(). You don't actually have to split up the position into x and y components for the call to moveTo, just pass in %mousePos.
Also, check this thread out:
www.garagegames.com/mg/forums/result.thread.php?qt=31225
07/05/2006 (6:00 pm)
In DummyMissile::fire(%this), %WorldPosition isn't defined, so it's messing up %mxpos and %mypos. %WorldPosition is just local to onMouseDragged().What I'd probably do is have onMouseDragged() and/or onMouseMove() call a function to rotate your turret. Then within createMissile() you could call:
%mousePos = sceneWindow2D.getMousePosition ();
And use that position for your moveTo(). You don't actually have to split up the position into x and y components for the call to moveTo, just pass in %mousePos.
Also, check this thread out:
www.garagegames.com/mg/forums/result.thread.php?qt=31225
Torque 3D Owner Matthew Langley
Torque