Game Development Community

Calculations Based on Position

by Chase Webb · in Torque 2D Beginner · 02/28/2013 (4:15 am) · 5 replies

Hello! I'm trying to figure out how to make calculations based around a specific position on the map. It's similar to the function used in the Scroller Toy:

package ScrollerToyPackage
{

function SandboxWindow::onTouchDown(%this, %touchID, %worldPosition)
{   
    // Set the scrollers speed to be the distance from the farground scrollers origin.
    // Also use the sign to control the direction of scrolling.
    %scrollerSpeed = getWord(%worldPosition,0) - ScrollerToy.FarScroller .getPositionX();

    // Set the scroller speeds.
    ScrollerToy.FarScroller.ScrollX = %scrollerSpeed;
    ScrollerToy.NearScroller.ScrollX = %scrollerSpeed * 1.5;
}
    
};

Specifically this line:
%scrollerSpeed = getWord(%worldPosition,0) - ScrollerToy.FarScroller .getPositionX();

From what I can tell (I could be wrong), the calculations are based on the distance between the click on the position of the FarScroller, which is 0,0. Instead of the object, I'd rather just have it calculate based on specific coordinates, such as 0,0 instead of the sprite.

Or maybe I'm doing something wrong? I'm trying to combine the functions of the Scroller toy (clicking on the screen gives you a value which is used to determine speed) with the SceneLayer Toy. I've managed to hack in horizontal clicking determining the speed of the layer rotation, though I seem to have spliced in a miniature FarScroller in the center of the screen doing so. I also want to have vertical location clicks determine the number of layers that appear.

... it's just a little self-exercise to try to teach myself how everything works. It was going well until I discovered that the little scroller image was there hidden behind all the rectangles.

#1
02/28/2013 (5:17 am)
Your basic understanding of how the scroller toy works is correct. Perhaps it is something in the details that is tripping you up.

The scroller speed is set by a single numerical value. Coordinates are given in a string with 2 values, one for the X axis and one for the Y axis. The getWord(%worldPosition, 0) function is grabbing the first value in the coordinate string for the X axis. Switching the 0 for a 1 would give you the Y axis value. The position of the scroller object is at 0,0 like you mentioned and getPositionX is only giving us the X value. So if I clicked in the upper right hand corner of the screen my %worldPosition would be "50,35" and the equation for scroller speed is then 50 - 0 which means a speed of 50.

If you want to use specific coordinates, then just replace ScrollerToy.FarScroller.getPositionX() with your X or Y coordinate value.

As for why a scroller has appeared in your scene, you would have to post your script in order for us to find your error.
#2
02/28/2013 (6:28 pm)
I've never understood why examples have always used the getWord function. Unless they've changed something, you should be able to just use .X and .Y to get those parts of a coordinate. For instance, %worldPosition.X should be identical to getWord(%worldPosition, 0).
#3
02/28/2013 (9:58 pm)
I doubt it's common knowledge. It is a nice shortcut for when you're working with two/three dimensional points, though.

And just to clarify for anyone who might read this in the future:
// declare a three-dim point
$myPoint3F = "1.0 2.0 3.0";

// each 'word' in the point can be accessed either via getWord() or
// by the 'hidden' .x .y and .z fields
echo($mPoint3F.x); // this will print "1.0"
echo($myPoint3F.y); // this will print "2.0"
echo($myPoint3F.z); // this will print "3.0"

#4
03/01/2013 (1:09 am)
I think the simple answer is bad habit. The use of .x, .y & .z is far better. I'll look at getting examples of its usage into the existing toys and replace a bunch of the "getWord()" calls.

Certainly the "getPositionX()" and "gePositionY()" are redundant as those have been around since early TGB days well before these setter/getter shortcuts were around.
#5
03/01/2013 (5:33 am)
Everyone, thanks for the input! I was able to figure out my problem and successfully implement the vertical clicking determining the number of layers! Now I just need to polish up some math in the layer calculation (it tries to create layers beyond the possible 31) and tidy up the code and I can post the toy up on here for others to learn from :)