Game Development Community

Does t2danglebetween work?

by Jacob Wagner · in Torque Game Builder · 04/25/2007 (8:36 pm) · 13 replies

The following bit of code:

%angle = t2dAngleBetween("10 0", "0 0");
echo(%angle);
%angle = t2dAngleBetween("10 10", "0 0");
echo(%angle);
%angle = t2dAngleBetween("0 10", "0 0");
echo(%angle);
%angle = t2dAngleBetween("-10 10", "0 0");
echo(%angle);
%angle = t2dAngleBetween("-10 0", "0 0");
echo(%angle);


Produces the following output:
90
90
90
90
90

Also using the torsion debugger watch values with the t2danglebetween function gives me the same results
Am I doing something wrong ?

#1
04/25/2007 (9:00 pm)
What do you expect the results to be? I think t2dAngleBetween is calculating the difference in angles between vector A and vector B, not the angle of the vector between point A and point B.
#2
04/25/2007 (9:52 pm)
I'm not that great at math.

Is there a function that gives the result of the angle of the vector between point a and point b?

by 'vector between' you mean a line drawn between the 2 points, right?

So results that I am looking for is for example if one point is below the other point I'd want whatever the angle for straight up is, 0 or whatever, and if one point was straight right of the other point I'd want it to be 90 , etc.
#3
04/25/2007 (10:15 pm)
There is such a function:
%dir = t2dVectorSub(%b, %a);
%angle = mRadToDeg(mAtan(getWord(%dir, 0), getWord(%dir, 1)));
Enjoy!
#4
04/25/2007 (10:22 pm)
Where can I go to learn about all this vector math that torque 2d uses? Is there a book you can recommend?

And thanks for the function! :P
#5
04/25/2007 (11:00 pm)
The math concepts used are, for the most part a combination of algebra and calculus (to my understanding, math's not my strong point either) ... specifically for games, there are a few books that highlight all the usual concepts .... however, most of these books tend to focus on the 3D aspect of the math ... which for TGB, is fairly useless (well, its helpful to know the stuff ... just not usable in most senses with TGB's 2D) ...

I'd say the best thing to do would be to look at geometry books, tutorials and other such things ... I've got a book called "Calculus Demystified" which I personally believe is rather good ... it shows quite a lot of examples and practical uses of calculus for an assortment of things ... digging through the book (literally just looking at the pictures to find something that looks right ... haha) usually helps me find the answer I'm looking for ...
#6
04/25/2007 (11:03 pm)
@Igor -- thats not so much a function, but more of an algorithm ... a function, in the sense that jacob's looking for would be more like:

function angleOfDirection(%vector1, %vector2)
{
  %dir = t2dVectorSub(%vector2, %vector1);
  %angle = mRadToDeg(mAtan(getWord(%dir, 0), getWord(%dir, 1)));
  return %angle;
}

which ... could be tossed into your common dir or a reusable math script ... and called in place of a standard function ...

NOTE: this is obviously the same thing that Igor posted, just wrapped inside of a function for easier copy/paste and usability ...


#7
04/26/2007 (12:23 pm)
Just to clarify, mAtan() expects y first and then x. So the right usage is: mAtan(getWord(%dir, 1), getWord(%dir, 0).
#8
04/26/2007 (10:17 pm)
@David - i thought he can do that (to put this chunk in a function body).

@Nicolas Olhaberry - this is correct, but not significant in that case. I mean it just change an angle by 90 CW.
This example assumes that 0 degree is a looking down vector.
#9
04/26/2007 (10:51 pm)
@Igor.

I see your point. I shouldn't have said "right usage" since it depends on the problem you want to solve. I just wanted to point out, considering the coordinate system TGB uses and the way rotated sprites are rendered, that is probably more helpful as atan(y, x).
#10
04/27/2007 (4:51 am)
You are right. I'm using atan(x, -y) usually. The point is - usability.
#11
04/27/2007 (8:16 am)
Since we are in the subject, what's the reason for TGB defining polar coordinates with zero pointing up?
#12
04/27/2007 (8:24 am)
Nicolas, cause thats the default for 'up' in 3D geometry ... TGB is based on a 3D world, with a set camera view ... X,Y,Z ... Z is locked (forward/backward) ... Y and X are not (up/down, left/right) .... and ... in general ... most sprites when they are placed would assumably ... depending on the game ... be placed facing 'upward' (ie; no rotation on the object at all)


Which direction to default things to is kind of hard, as each game genre usually has them going in different directions ... you could probably change the default direction fairly quickly in the C++ with little to no work involved ... I would assume it's in the t2dSceneObject code, just change the default value of the 'Rotation' field on the object during construction and that'd probably face it another way ... granted ... 'up' would still be 0 ... but your default placement would be say ... 90 or 180 or whatever ...
#13
04/27/2007 (10:10 pm)
For a platformer game. for example, I'd prefer zero pointing up. You can easily detect moving direction since negative angles are left-side, positive - right-side.
The coordinate system in TGB is OpenGL default.