something wrong with objects positions sums
by Isaac Barbosa · in Torque Game Builder · 03/01/2009 (9:21 am) · 3 replies
Hello, I was dealing almost for a whole day hunting for a strange bug was not supossed to exist in my code. Indeed, there is not a bug due to my code, but maybe due to something with TGB or maybe with something I don´t understand.
I have a code that allows me to click on an object and set its coordinates as global variables so I can destroy the object and keep their position. So when I enter an object is near to the first object I can now if this new object is up or down or left up, left down, or rigth up or right down.
Everything works fine with this code, except when the second object position X is -0. The wird thing is that is a echo the Y positions of both objects I get always -2.5 or 2.5 depends on the case and 5 or -5, what means everything should be fine and works, but as I´ve said already something is wrong when the second object is below 0 no matter is everything seems ok.
So the question is: what could be wrong here????
Thanks
I have a code that allows me to click on an object and set its coordinates as global variables so I can destroy the object and keep their position. So when I enter an object is near to the first object I can now if this new object is up or down or left up, left down, or rigth up or right down.
Everything works fine with this code, except when the second object position X is -0. The wird thing is that is a echo the Y positions of both objects I get always -2.5 or 2.5 depends on the case and 5 or -5, what means everything should be fine and works, but as I´ve said already something is wrong when the second object is below 0 no matter is everything seems ok.
So the question is: what could be wrong here????
if(%this.getPositionX() $= $object1.getPositionX() - 5 && %this.getPositionY() $= $object1.getPositionY() -2.5)
{
echo("left up");//echoed ok only if %this position X is not below 0
}
else if(%this.getPositionX() $= $object1.getPositionX() - 5 && %this.getPositionY() $= $object1.getPositionY() +2.5)
{
echo("left down");//echoed ok only if %this position X is not below 0
}
else if(%this.getPositionX() $= $object1.getPositionX() + 5 && %this.getPositionY() $= $object1.getPositionY() -2.5)
{
echo("right up");//echoed ok only if %this position X is not below 0
}
else if(%this.getPositionX() $= $object1.getPositionX() + 5 && %this.getPositionY() $= $object1.getPositionY() +2.5)
{
echo("right down");//echoed ok only if %this position X is not below 0
}
else if(%this.getPositionX() $= $object1.getPositionX() && %this.getPositionY() $= $object1.getPositionY() - 5)
{
echo("up"); //echoed ok ALWAYS
}
else if(%this.getPositionX() $= $object1.getPositionX() && %this.getPositionY() $= $object1.getPositionY() + 5)
{
echo("down");//echoed ok ALWAYS
}Thanks
#2
You may also want to try and add a space to change the "-2.5" into "- 2.5" as well as the "+ 2.5". Torquescript is weird sometimes and wants things just right or it will not parse it correctly.
03/01/2009 (12:37 pm)
I have had issues with positions and math returning a "-0" which really screws up comparisons. I have had to check for cases where this is the case and change it to "0" before comparing. Also, your code may be easier to read and maintain if you would pull those getpositions into a %local variable before doing all the if statements. You may also want to try and add a space to change the "-2.5" into "- 2.5" as well as the "+ 2.5". Torquescript is weird sometimes and wants things just right or it will not parse it correctly.
#3
I don´t know why but if I use == instead of $=, everything got screwed up and the game doesn´t works as should do.
@Dustin,
Thanks for the light.
I did this and now it works just as I want (previous code works fine with entire numbers -like a 5 instead of 2.5-). Check condition one and three when the object position is below -0; the string comparison should be <= instead of $=
03/02/2009 (8:11 am)
@Phillip,I don´t know why but if I use == instead of $=, everything got screwed up and the game doesn´t works as should do.
@Dustin,
Thanks for the light.
I did this and now it works just as I want (previous code works fine with entire numbers -like a 5 instead of 2.5-). Check condition one and three when the object position is below -0; the string comparison should be <= instead of $=
if(%this.getPositionY() >= 0)
{
if(%this.getPositionX() $= $Object2.getPositionX() - 5 && %this.getPositionY() $= $Object2.getPositionY() - 2.5 || %this.getPositionX() $= $Object2.getPositionX() - 5 && %this.getPositionY() $= $Object2.getPositionY() + 2.5 || %this.getPositionX() $= $Object2.getPositionX() + 5 && %this.getPositionY() $= $Object2.getPositionY() - 2.5 || %this.getPositionX() $= $Object2.getPositionX() + 5 && %this.getPositionY() $= $Object2.getPositionY() + 2.5 || %this.getPositionX() $= $Object2.getPositionX() && %this.getPositionY() $= $Object2.getPositionY() - 5 || %this.getPositionX() $= $Object2.getPositionX() && %this.getPositionY() $= $Object2.getPositionY() + 5)
{
%this.createObject3();
}
}
else if(%this.getPositionY() <= -0)
{
if(%this.getPositionX() $= $Object2.getPositionX() - 5 && %this.getPositionY() <= $Object2.getPositionY() - 2.5 || %this.getPositionX() $= $Object2.getPositionX() - 5 && %this.getPositionY() $= $Object2.getPositionY() + 2.5 || %this.getPositionX() $= $Object2.getPositionX() + 5 && %this.getPositionY() <= $Object2.getPositionY() - 2.5 || %this.getPositionX() $= $Object2.getPositionX() + 5 && %this.getPositionY() $= $Object2.getPositionY() + 2.5 || %this.getPositionX() $= $Object2.getPositionX() && %this.getPositionY() $= $Object2.getPositionY() - 5 || %this.getPositionX() $= $Object2.getPositionX() && %this.getPositionY() $= $Object2.getPositionY() + 5)
{
%this.createObject3();
}
}
Associate Phillip O'Shea
Violent Tulip