Game Development Community

Better Mouse MovetoMouse movement...

by Ecliptic · in Torque Game Builder · 12/16/2008 (11:12 pm) · 6 replies

I was wondering how I could go about putting something like this into my moveToMouse function. I know the syntax is incorrect. I am still trying to learn the exact syntax but I was wondering if this would work logic wise. I am trying to have my movements to go in either only X or Y. The movement is a grid system and should never have angles. The grid spaces are really large so X or Y Should always be easily less than one another if moving in the Y or X.

if { %this.PosX < %this.NewPosY;
    %this.worldPos = "0 NewPosY";
Else
    %this.worldPos = "NewPosX 0";
}

This is what I currently have and it works because of the collision barriers I have set up but I would like to make it coded as the game play is intended. Plus I am still trying to learn.

function Image::moveToMouse(%this, %worldPos)
{
   %this.position = t2dVectorAdd(%worldPos, %this.offset);
}

Also I find that is I move the mouse really quick the object loses contact with the position. Is this normal? IE, If I am too fast I leave the object I am dragging behind and it stops responding until I go back to it.

Thanks guys
Dane

#1
12/17/2008 (6:35 pm)
Ok, reworking this a little bit.. I got it to a point where I think the logic should work but on the last function I get a syntax error on the "%this.position = (0, %newYPos);" section. Is this the right syntax? I thought I had all this mouse movement scripting under control. Its starting to get away from me hehe...


function Image::onMouseDown(%this, %modifier, %worldPos, %mouseClicks )
{
   // Toggle the drag status.
   %this.dragging = !%this.dragging;
   
   // We always stop dragging on mouse up 
   %this.cancelOnMouseUp = !%this.toggleDragState;

   if (!%this.centerOnMouse)
      %this.offset = t2dVectorSub(%this.position, %worldPos);
}

function Image::onMouseUp(%this, %modifier, %worldPos)
{
   if (%this.cancelOnMouseUp)
   {
      %this.dragging = false;
   }
}

function Image::onMouseDragged(%this, %modifier, %worldPos)
{
   if (%this.dragging)
   {
      %this.moveToMouse(%worldPos);
      
      // Once we have dragged, then dragging will always stop on mouse up.
      %this.cancelOnMouseUp = true;
   }
}

function Image::onMouseMove(%this, %modifier, %worldPos)
{
   if (%this.dragging)
      %this.moveToMouse(%worldPos);
}

function Image::moveToMouse(%this, %worldPos, %movement)
{
   
%newXPos = getword(%worldPosition,0);
%newYPos = getword(%worldPosition,1);

if (%newXPos < %newYPos){
		if (%newYPos < 0) { %posmod=-1; } else { %posmod = 1; }
		%newYPos = (%posmod * %newYPos);
		%this.position = (0, %newYPos);
}
else
{
	if (%newXPos < 0) { %posmod=-1; } else { %posmod = 1; }
		%newXPos = (%posmod * %newXPos);
		%this.position =(%newXPos,0);
}

		sceneWindow2D.setMousePosition("0 0");
}

BTW, if you want to test what I am doing here, just drag an image into TGB and make its name image and make it selectable. This movement code will work as intended if you change the "movetoMouse(%this, %worldPos) to
function Image::moveToMouse(%this, %worldPos)
{
   %this.position = t2dVectorAdd(%worldPos, %this.offset);
}

But I am trying to make it decide which axis the user is trying to move in. I only want movement in the X only or the Y only. Any help/suggestions would be greatly appreciated. I am getting close to finishing up my scripting for my first project. I just need to find out how to mount objects through code next and I think I am done!

Thanks guys
Dane
#2
12/17/2008 (8:08 pm)
Well I modified its moveToMouse function some more. Still can't figure out what is wrong with the syntax... But I noticed a problem with the logic.. Since I remove the negative to determine which direction I should be moving in, it will always move either up or to the right because the new positions will always be positive.. So I made it store the neutral value in another variable just to use to determine which direction I would be moving in.
function Image::moveToMouse(%this, %worldPos, %movement)
{
//Get the position of the new X and new Y
	%newXPos = getword(%worldPosition,0);
	%newYPos = getword(%worldPosition,1);

//Removing the negative from the position so I know which direction I need to be moving in.
	if (%newYPos < 0) { %posmod=-1; } else { %posmod = 1; }
		%NeutralY = (%posmod * %newYPos);
		
	if (%newXPos < 0) { %posmod=-1; } else { %posmod = 1; }
		%NeutralX = (%posmod * %newXPos);
	
//Now I need to determine what axis I will be moving in.
	if (%NeutralX < %NeutralY)
		{%this.Position = %worldPos(0, %newYPos);}
else
		{%this.Position = %worldPos(%newXPos,0);}
}

What is wrong with the if else statement... Something with the syntax is off because I keep getting an error on it.. I can't find any examples that show me something blantingly obvious that I am missing.. Please. Anyone?

Dane
#3
12/17/2008 (8:20 pm)
When you are setting the value of a variable, don't wrap it in parenthesis...

//Get the position of the new X and new Y
	%newXPos = getword(%worldPosition,0);
	%newYPos = getword(%worldPosition,1);

//Removing the negative from the position so I know which direction I need to be moving in.
	if (%newYPos < 0) 
       { 
          %posmod=-1; 
       } 
       else 
       { 
          %posmod = 1; 
       }

       %NeutralY = %posmod * %newYPos;

       if (%newXPos < 0)
       { 
          %posmod=-1; 
       } 
       else 
       { 
          %posmod = 1; 
       }

       %NeutralX = %posmod * %newXPos;
	
       //Now I need to determine what axis I will be moving in.
       if (%NeutralX < %NeutralY)
       {
          %this.Position = "0" SPC %newYPos;
       }
       else
       {
          %this.Position = %newXPos SPC "0";
       }

I think the above will do what you want, at least it should have correct syntax
#4
12/18/2008 (9:00 am)
Thanks! That did remove the syntax error but I guess something is wrong with the logic still since I don't get any syntax errors. I added a debug statment to see what my X and Y Pos are but when it echos it is blank. So apparently it isn't grabbing the selected objects position and updating it as it moves. So I tried adding it to %this but that didn't help.

function Image::moveToMouse(%this, %worldPos, %movement)
{
//Get the position of the new X and new Y
	%this.newXPos = getword(%worldPosition,0);
	%this.newYPos = getword(%worldPosition,1);
	
echo("The objects X =" SPC %newXPos SPC "The objects Y =" SPC %newYPos);

//Removing the negative from the position so I know which direction I need to be moving in.
	if (%this.newYPos < 0) { %posmod=-1; } else { %posmod = 1; }
		%NeutralY = (%posmod * %this.newYPos);
		
	if (%this.newXPos < 0) { %posmod=-1; } else { %posmod = 1; }
		%NeutralX = (%posmod * %this.newXPos);
	
//Now I need to determine what axis I will be moving in.
	if (%NeutralX < %NeutralY)
		{%this.Position = "0" SPC %this.newYPos;}
else
		{%this.Position = %this.newXPos SPC "0";}
}

Thanks for the help Eric!
Dane
#5
12/18/2008 (9:08 am)
Try this:

function Image::moveToMouse(%this, %worldPos, %movement)
{
   //Get the position of the new X and new Y
   %newXPos = getword(%worldPos,0);
   %newYPos = getword(%worldPos,1);
	
   echo("The objects X =" SPC %newXPos SPC "The objects Y =" SPC %newYPos);

   //Removing the negative from the position so I know which direction I need to be moving in.
   if (%newYPos < 0) 
   { 
      %posmod=-1; 
   } 
   else 
   { 
      %posmod = 1; 
   }

   %NeutralY = %posmod * %newYPos;
		
   if (%newXPos < 0) 
   { 
      %posmod=-1; 
   } 
   else 
   { 
      %posmod = 1; 
   }

   %NeutralX = %posmod * %newXPos;
	
   //Now I need to determine what axis I will be moving in.
   if (%NeutralX < %NeutralY)
   {
      %this.Position = "0" SPC %newYPos;
   }
   else
   {
      %this.Position = %newXPos SPC "0";
   }
}
#6
12/18/2008 (9:43 am)
Thanks Eric! That did it. I guess it was because of the %worldPosition in the getword function. Now if I would have declared %worldPosition in the function rather than %worldPos would it have worked properly? Or is %worldPosition replaced with %worldPos. I have seen a lot of examples that used both so I thought it was just a preference.

Also, is there anything used to just update one vector? I would like this to update just the X or just the Y. If I set X or Y to 0 as I am doing here it restricts me to moving along the X=0 axis and the Y = 0 axis. This is currently what I am looking for but since we are discussing movement and I am sure I will be looking for this later. :)

I played with it a little and thought this would work but didn't have the desired affect.

%newXPos = getword(%worldPos,0);
%newYPos = getword(%worldPos,1);

//Creating a new variable to store the original positions in.
%oldXPos = %newXPos;
%oldYPos = %newYPos;

Then I added the %oldYPos and %oldXPos to the X or Y Position later instead of 0. Thinking it would then retain its original value. Any thoughts on this?

Thanks again!
Dane