Game Development Community

Using the RoundUp function..

by Ecliptic · in Torque Game Builder · 12/19/2008 (1:16 pm) · 2 replies

Hey guys, I am working on a specific mouse movement right now and I believe to get it working I would need to use whole numbers. I can't find an example of someone using the roundup function so I was wondering if you guys could help me with the syntax.

I am trying to get the position of %this and then round it up and store it into a global value. I am getting a syntax error because I am not sure what goes in the () for the roundUp.

function roundUp(%worldPos)
{
	%ValueX = %this.getword(%worldPos, 0);
	return mCeil(%ValueX);
	
	%ValueY = %this.getword(%worldPos, 1);
	return mCeil(%ValueY);
}
$ROUNDEDX = %ValueX;
$ROUNDEDY = %ValueY;

Thanks guys, once I get this code done I will post it for the community.
Dane

#1
12/19/2008 (3:34 pm)
Ok guys, well here is the mouse code I got so far.. There seems to be a bug with the Y direction that I cant seem to figure out. I think it could be better with the use of loop statements but I am not sure on how to get that working hehe.
Basically just create something with the name or class of image and have this code executed in the game.cs and you should be able to drag that item left/righ and up/down without diagonal movement.

*******Warning! This code is not written by an expert by any means so you will want to clean it up or look into modifying it to fit your needs. I am just trying to help out fellow beginners*********

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;

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


$oldX = mFloor(%newXPos);
$oldY = mFloor(%newYPos);

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

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


function Image::onMouseDragged(%this, %modifier, %worldPos)
{
//Get the new positions.
   %newXPos = getword(%worldPos,0);
   %newYPos = getword(%worldPos,1);	
   
//Round the new Position.
%newXPos = mFloor(%newXPos);
%newYPos = mFloor(%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 = mFloor(%posmod * %newYPos);
echo("The new Neutral Y=" SPC %NeutralY);
echo("The old Neutral Y=" SPC $OLDNEUTRALY);
   
   if (%newXPos < 0) 
   { %posmod=-1;} else { %posmod = 1; }
   %NeutralX = mFloor(%posmod * %newXPos);
echo("The new Neutral X=" SPC %NeutralX);
echo("The old Neutral X=" SPC $OLDNEUTRALX);
	
//Now I need to determine what axis I will be moving in.
   if ($OLDNEUTRALX != %NeutralX)
   { %this.Position = %newXPos SPC $oldY;}
   else
   { %this.Position = $oldX SPC %newYPos;}
   
}

Thanks to any who can add some input on making this x/y movement code better.
Dane
#2
12/26/2008 (10:38 pm)
A simple mouse drag only requires this:

function Image::onMouseDragged(%this, %modifier, %worldPos)
{
    %this.Position = %worldPos;
}

If you want to drag your mouse around the scene and have the object chase it you can do something like this (add something to the level that encapsulates the entire background and make its class name "Background"):

function Image::onLevelLoaded( %this, %scenegraph )
{
    $myImage = %this;
}
function Background::onMouseDragged(%this, %modifier, %worldPos)
{
    %vecX = %worldPos.x - $myImage.position.x;
    %vecY = %worldPos.y - $myImage.position.y;
    
    %divsor = 30;
    
    %newX = $myImage.position.x + %vecX/%divsor;
    %newY = $myImage.position.y + %vecY/%divsor;
    
    $myImage.setPosition( %newX, %newY );
}

Of course you can do something with the .moveTo command, but linear velocity doesn't look as fancy:
function Background::onMouseDown(%this, %modifier, %worldPos)
{
    $myImage.moveTo(%worldPos, 10, true, true, true, 0.01);
}


Hope this helps!

Jim G