Weird mounting problem
by Vern Jensen · in Torque Game Builder · 08/23/2006 (1:11 am) · 5 replies
I'm trying to create a "number" sprite, where each digit is actually a separate sprite, using an image that has 10 frames, for 0-9. I mount each digit to the first digit I create, but I offset each mounted one by the width of that sprite times the index of that digit:
for (%n = 1; %n < %numDigits; %n++)
{
%newDigit = createDigit( %imageMapName, %layer ); // This simply creates a new T2DStaticSprite
%newDigit.mount( %newSprite, %newDigit.getWidth() * %n, 0, 0, 1, 1, 1, 0);
}
But this gives me strange results, where the digits are spaced apart. I've tried dividing by various values, and I can get the spacing to work right for some sizes, but if I change the digit's size (in advance of course), the spacing is all wrong again.
It's like the mount method is not using the "x offset" parameter as you'd expect it to. Is there something I'm missing here?
for (%n = 1; %n < %numDigits; %n++)
{
%newDigit = createDigit( %imageMapName, %layer ); // This simply creates a new T2DStaticSprite
%newDigit.mount( %newSprite, %newDigit.getWidth() * %n, 0, 0, 1, 1, 1, 0);
}
But this gives me strange results, where the digits are spaced apart. I've tried dividing by various values, and I can get the spacing to work right for some sizes, but if I change the digit's size (in advance of course), the spacing is all wrong again.
It's like the mount method is not using the "x offset" parameter as you'd expect it to. Is there something I'm missing here?
#2
The problem is that the mount-offsets are not in "world-space," they're in "object-space" meaning, for example, that "-1" is left/top and "+1" is bottom/right. Object-space is used so that when you resize your object, the same relative positions are used. Note that you can use values outside the -1/+1 range so you get multiples of the obejcts width/height.
So, if your main object is 10 world-units wide then object-space "-1" equals "-5" world-units and object-space "+1" equals "+5" world-units. Note that there are conversion functions provided by the "t2dSceneObject" to convert between spaces but you can also do this yourself easily if you wish as I show below.
You can find all this information in the "t2d reference.pdf" that also explains what these spaces are.
There's also a problem in your code where you are multiplying the width of the current digit by "%n" to find its position but this would only work if all your digits were of the same width anyway. Ignoring that, an easy way to do this is to create a conversion factor from the main objects object-space to world-space like so...
Another way (and much nicer) is to ignore the widths of the objects and just use the fact that "+1" in object-space for the "%offsetX" is the right-hand side of the object you're mounting to (assuming no rotation). You can then simply mount each "digit" onto the preceeding one like so...
Hope this helps,
- Melv.
08/23/2006 (7:35 am)
Vern,The problem is that the mount-offsets are not in "world-space," they're in "object-space" meaning, for example, that "-1" is left/top and "+1" is bottom/right. Object-space is used so that when you resize your object, the same relative positions are used. Note that you can use values outside the -1/+1 range so you get multiples of the obejcts width/height.
So, if your main object is 10 world-units wide then object-space "-1" equals "-5" world-units and object-space "+1" equals "+5" world-units. Note that there are conversion functions provided by the "t2dSceneObject" to convert between spaces but you can also do this yourself easily if you wish as I show below.
You can find all this information in the "t2d reference.pdf" that also explains what these spaces are.
There's also a problem in your code where you are multiplying the width of the current digit by "%n" to find its position but this would only work if all your digits were of the same width anyway. Ignoring that, an easy way to do this is to create a conversion factor from the main objects object-space to world-space like so...
%conv = 2.0 / %newSprite.getWidth();You can then multiple world-units by this value to get the respective objects' object-space coordinates.
Another way (and much nicer) is to ignore the widths of the objects and just use the fact that "+1" in object-space for the "%offsetX" is the right-hand side of the object you're mounting to (assuming no rotation). You can then simply mount each "digit" onto the preceeding one like so...
// Note the first sprite.
%lastObject = %newSprite;
// Add some digits...
for (%n = 1; %n < %numDigits; %n++)
{
// Create the first digit.
%newDigit = createDigit( %imageMapName, %layer );
// Mount the digit onto the previous one (the main object in the first case).
%newDigit.mount( %lastObject, 1, 0, 0, 1, 1, 1, 0);
// Make this digit the "last" one.
%lastObject = %newDigit;
}Hope this helps,
- Melv.
#3
08/23/2006 (2:13 pm)
Thanks Melv! I was completely unaware of Object-space vs. world-space. Thanks for clearing that up!
#4
08/23/2006 (2:21 pm)
Oh, one small correction to the code you posted. You must use "2" as the object-space offset factor to get the digits mounted properly, because if you use 1, the mounted sprite's *center* will be placed at the other sprite's right-hand side. By using 2, you can place the center where it should be.
#5
Eiher way, hope it helps,
- Melv.
08/24/2006 (12:54 pm)
Regarding "2"; Agreed! I must admit that I just typed the code here, I didn't try it but you are correct. Of course, this all does assume the digits are of the same width or at least the current digit is the same as the previous one.Eiher way, hope it helps,
- Melv.
Torque Owner Vern Jensen
Does this sound like something I should report as a bug? Why does mount()'s xOffset parameter not work the same as an x offset would work for setPosition()? Shouldn't it be the same?