Game Development Community

Need help trying to figure out when I'm at my new location

by Craig Griffiths · in Torque Game Builder · 09/09/2009 (6:54 pm) · 2 replies

//Get a random speed
   %this.speed = getRandom(%this.minSpeed, %this.maxSpeed);

	//Pick a new location to move too
   %this.newX = getRandom(%this.minX, %this.maxX);
   %this.newY = getRandom(%this.minY, %this.maxY);

	//2d math function. Get old and new x and y position, atan calculates the radians
	//between them then radtodeg gives you the degree
	 %this.dir = mRadToDeg(mAtan( %this.newX - %this.getPositionX(), %this.getPositionY() - %this.newY ) );  
	//%this.dir = %this.dir % %this.dir;
   //Where to move to, the speed, true or false if we want it to auto stop, true for a callback
   %this.moveTo(%this.newX, %this.newY, %this.speed, false, true);
   	//The degree for which the player is to rotate by, dir is the angle, 90 is the speed for which the player is to rotate by
   %this.rotateTo(%this.dir, 45);

   //Call this function every 5 seconds
   //if(%this.Location)
	 // {
	  //	%this.Location = 0;
	  	//echo(%this.Location);
	  	%this.schedule(5000, "updateMovement");
	  //}
}
I'm trying to figure out from the current Position I am at to the new location I'm moving to; when have I reached my new location. Once I get to my new location I need to add some code to change the players speed and how it rotates; also, if 5 seconds is up and I'm still not at my new location I don't want to call updateMovement until I've reached it. A friend and I have been talking about this for a bit and all the solution we come up with involve decimal points for the current position but the new positions are whole numbers. We've tried the distance formula and if checks comparing the positions. One of the main problems is this function only gets called every five seconds, I figure I could call it in the main game loop but I'm not sure to how to go about this. The last problem is whenever we're checking against the new location whenever the function gets called again I'm going to have a new x and a new y. This thing is just becoming a jumble mess. Any outside assistance would be greatly appreciated. Thanks.
Craig

#1
09/09/2009 (7:55 pm)
I believe there's a callback made for this exact situation onPositionTarget() will be called when your object finishes the .moveTo command and you can change the rotation and speed there. You've even enabled the callback in moveTo so you might already be aware of this.

If for some reason you can't use this method, another (if ugly way) might be to create a timer for this object and check at every tick if it's close enough to the target position. I did something similar when I was first making my game and I wasn't aware of the onPositionTarget callback.
#2
09/09/2009 (8:25 pm)
Woot, thank you so much. A lot easier then I was making it out to be.