Game Development Community

X/Y Positioning

by Psp Goto · in Torque 2D Beginner · 01/08/2014 (2:09 pm) · 3 replies

I'm creating a scene using sprites, and I need to make it so that the top-corner of the SceneWindow is 0, 0, and if you do sprite.setPosition(10, 10), it will move the sprite to start 10 to the right from the top corner and 10 down from the top corner. So that instead of starting from the center, and moving outwards it will start at the top corner and move down. Is there a way to do this easily?

Also, is there a way to make setSize(5, 5); make the object 5 pixels wide and 5 pixels long, vs 5% or whatever it does normally?

Sorry if the question is confusing
Thanks

#1
01/08/2014 (3:09 pm)
The coordinates system in T2D cannot be changed without modifying the C++ source code. It is done this way to work with the Physics system.

Placing the camera so that World position 0,0 is at the top left is fairly easy, but the Y axis will be negative, so 0, -1, -2, etc. as you go down on the screen.

One thing you can do though is to write a script function which translates world coordinates to your desired coordinates system.

Another way to do it would be with a CompositeSprite using a custom layout.
Wiki entry on Composite Sprites.

Pixel size

T2D uses meters as its basic size unit to make it work properly with the physics engine. As such, setSize(5,5) will make your object 5 meters by 5 meters.

The default camera size is 100x75, meaning your scenewindow displays 100 meters across and 75 meters high. At a resolution of 1024x768, one pixel would be 0.0976 meters by 0.0976. So setSize(0.488, 0.488) would give you an object which appears as 5x5 pixels.

Diffferent resolution, camera size, camera zoom and everything needs to be recalculated. If you absolutely need this pixel size, I suggest writing a script function like so

function Myobject::setPixelSize(%this, %x, %y)
{
//pseudo-code, not actual script
%finalsizeX = CameraSizeX / GameResolutionX;
%finalsizeY = CameraSizeY / GameResolutionY;

%this.setSize(%x * %finalsizeX, %y * %finalsizeY);
}

I don't have the actual variables for Camerasize and gameresolution at my disposition at the moment but you can search the scripts for them.
#2
01/09/2014 (6:48 pm)
Thanks, one more thing: setSize() doesn't scale from the top left moving out, having the same issue as the sceneWindow top-left starting at 0,0. Is there a way to move the scale-out point, or something similar for a sprite?

Thanks
#3
01/09/2014 (8:52 pm)
All scaling is done from the center, all positioning is done from the center of each object. The local coordinates of an object have 0,0 at the center, just like the Physics-based "World".

My suggestion is to adopt this way of thinking when working with T2D, else you will have to write custom functions for every single aspect of the engine.