Coordinate Systems
by W. Wood Harter · in Torque Game Builder · 11/05/2007 (2:37 pm) · 3 replies
I'm not sure this is a question, just figured out how to do what I needed with World Limits and never found what I needed when searching. Thought I would post this for comment and others that have similar issues.
I was struggling to programatically set a world limit on a sprite to the size of the window, plus the width of the sprite. When ever the sprite moved off the screen it would be removed from the screen and the callback would be called.
The first thing is to understand the size of the display. The sceneWindow is the global the has the coordinate system in pixels. The upper left is at 0,0. The lower right can be found using sceneWindow2D.Extent
These can be converted to world coordinates. (UL = upper left, LR = lower right)
This sprite will be killed and the callback called immediatly when it moves off the screen as the world limit is the screen size with a border the width of the sprite.
In another attempt, I actually created a default object using TGB and graphically created the world limit. I kept this object out of the view area and just copied that world limit when creating similar sprites programatically. A real hack, but it worked.
One question this brings up is where the world coordinate system is defined. On printing out the world limit I receive using the above code, I get:
World limit = KILL -60.000000 -47.500000 60.000000 47.500000 true
The camera is setup with 100,75 width and height. I know object space is -1-1,1,1, but I have no idea where the -60,-47,60,47 comes from. It doesn't really matter because I'll just deal with the values I'm given programatically, but it would be interesting to know where these values come from.
I was struggling to programatically set a world limit on a sprite to the size of the window, plus the width of the sprite. When ever the sprite moved off the screen it would be removed from the screen and the callback would be called.
The first thing is to understand the size of the display. The sceneWindow is the global the has the coordinate system in pixels. The upper left is at 0,0. The lower right can be found using sceneWindow2D.Extent
%tmpRight = getWord(sceneWindow2D.Extent,0); %tmpBottom = getWord(sceneWindow2D.Extent,1);
These can be converted to world coordinates. (UL = upper left, LR = lower right)
%worldPtUL = sceneWindow2D.getWorldPoint(0,0); %worldPtLR = sceneWindow2D.getWorldPoint(%tmpRight,%tmpBottom);Since I was setting this up in a call directly to the object, %this is the sprite. The getHeight and getWidth methods return the height and width in world coordinates. These can be added and subtracted from the UL and LR points just calculated.
%worldPtUL = (getWord(%worldPtUL,0)-%this.getWidth()) SPC (getWord(%worldPtUL,1)-%this.getHeight()); %worldPtLR = (getWord(%worldPtLR,0)+%this.getHeight()) SPC (getWord(%worldPtLR,1)+%this.getHeight()); %this.setWorldLimit(kill,%worldPtUL SPC %worldPtLR,true);
This sprite will be killed and the callback called immediatly when it moves off the screen as the world limit is the screen size with a border the width of the sprite.
In another attempt, I actually created a default object using TGB and graphically created the world limit. I kept this object out of the view area and just copied that world limit when creating similar sprites programatically. A real hack, but it worked.
%this.setWorldLimit(kill,getWords(MyDefaultSprite.getWorldLimit(),1,4),true);getWorldLimit() also returns the limit mode and callback flag, so you have to use getWords to find extract out the UL and LR values. Hope that helps.
One question this brings up is where the world coordinate system is defined. On printing out the world limit I receive using the above code, I get:
World limit = KILL -60.000000 -47.500000 60.000000 47.500000 true
The camera is setup with 100,75 width and height. I know object space is -1-1,1,1, but I have no idea where the -60,-47,60,47 comes from. It doesn't really matter because I'll just deal with the values I'm given programatically, but it would be interesting to know where these values come from.
#2
Pondering the choice of putting the coordinate system origin at the center of the camera makes sense if most of the games that are written have a scrolling component to it as I'm betting they do. Why offset your origin from the center if it is just going to move around anyway? If you offset it to the upper left you just limit your movement (minutely) in the right/down direction. It takes a little getting used to, but it does make the most sense for the most flexibility.
11/06/2007 (9:58 am)
Oh, I should have seen that. I knew the center of the screen was 0,0 and now that you say that it makes sense that I just have to take half of the camera size 120 =60 and 37.5 = 75. It was all right there in front of me. Thanks for the kick.Pondering the choice of putting the coordinate system origin at the center of the camera makes sense if most of the games that are written have a scrolling component to it as I'm betting they do. Why offset your origin from the center if it is just going to move around anyway? If you offset it to the upper left you just limit your movement (minutely) in the right/down direction. It takes a little getting used to, but it does make the most sense for the most flexibility.
#3
11/06/2007 (12:12 pm)
Exactly, you got it!
Associate James Ford
Sickhead Games
If you want to see your camera position, width and height go to the edit tab when you have no objects selected. Mine by default is at (50, 37.5) with a size of 100 x 75, which means if your sprite is 20 x 15 in size those world limits would be correct (for what you are trying to do).