# Calculating the angle of a point in relation to another

Posted by owen
 wilco2009 Re: Calculating the angle of a point in relation to another January 14, 2012 04:59PM Registered: 10 years agoPosts: 87
Aceleration and gravity is probabily easier than it can seems. A computer program doesn't works with continuous time. Due to it we have to work with discrete ecuations, theese means we will works with addtions instead products.
At the end, to simulate velocity we only have to increment position using a constant factor, and to simulate aceleration is enough to increment velocity using another constant factor.

I would like to see the result of your game, please let me check it when you will have a beta version.
 owen Re: Calculating the angle of a point in relation to another January 14, 2012 08:24PM Registered: 10 years agoPosts: 363
@wilco2009 Sure I will have a demo and make the game open-source because I am sure that I will not be able to do all the code by myself.

Since there are only 2 racing games on wiibrew; [www.wiibrew.org] then it must be a sign that they are hard to make :)
 owen Re: Calculating the angle of a point in relation to another January 18, 2012 03:45AM Registered: 10 years agoPosts: 363
Good day again.
I have a related problem. The code so far works fine. I usually use one of the grrlib functions such as GRRLIB_ObjectView to scale and rotate before I draw my model, those are doing thier own mtx stuff. Now I am trying to determine what angle to rotate the player_model so that it faces the player_direction. I would past the angle to GRRLIB_ObjectView(). Any suggestions on how I can calculate the angles?
 wilco2009 Re: Calculating the angle of a point in relation to another January 18, 2012 07:31PM Registered: 10 years agoPosts: 87
Hi Owen,
You are trying to find the angle to draw the player car?
 owen Re: Calculating the angle of a point in relation to another January 18, 2012 07:40PM Registered: 10 years agoPosts: 363
Quote
wilco2009
Hi Owen,
You are trying to find the angle to draw the player car?

Yes. Basically I want to rotate the model so that it faces the player_direction. It is easy to do with the camera because the camera has a look at parameter but I don't know how to do it with a model.
 wilco2009 Re: Calculating the angle of a point in relation to another January 19, 2012 08:49AM Registered: 10 years agoPosts: 87
If I understand, you try to draw the car of the player.
The angle needed is exactly the same that you used to setup the camera.
You have to apply this angle before to move the object to player_position.

For instance, if player car is a cube the code could be something like....:
```GRRLIB_ObjectViewBegin();
GRRLIB_ObjectViewRotate(0,angle,0);
GRRLIB_ObjectViewTrans(player_position.x,player_position.y,player_position.z);
GRRLIB_ObjectViewEnd();
GRRLIB_DrawCube(1.0, true, 0xFFFFFFFF);
```
 owen Re: Calculating the angle of a point in relation to another January 20, 2012 10:08PM Registered: 10 years agoPosts: 363
^ yes the method works but I have to continuously store the value of "angle" as I turn the player. It would be nicer if I could find the angle between the player_position and the player_direction by making a right angled triangle ( like this [www.mathsisfun.com] )
 wilco2009 Re: Calculating the angle of a point in relation to another January 20, 2012 11:10PM Registered: 10 years agoPosts: 87
Yes, this is a possible method.
If you know the player vector direction is easy to calculate it. Player direction is the tangent of the angle, then you only have to calculate the arctangent of the x component divided by z component:

angle = arctg(player_direction.x/player_direction.z)

Edited 2 time(s). Last edit at 01/20/2012 11:48PM by wilco2009.
 profetylen Re: Calculating the angle of a point in relation to another January 23, 2012 08:06PM Registered: 12 years agoPosts: 384
Quote
wilco2009
angle = arctg(player_direction.x/player_direction.z)

That could cause a division-by-zero exception. If he uses atan2 from math.h, he doesn't have to worry about that.

Example:
```#include <math.h>

angle=atan2(player_direction.z, player_direction.x);
```
 tueidj Re: Calculating the angle of a point in relation to another January 23, 2012 11:28PM ModeratorRegistered: 12 years agoPosts: 686
The wii doesn't throw division by zero exceptions unless you go to the effort of enabling them.
 owen Re: Calculating the angle of a point in relation to another January 26, 2012 05:53PM Registered: 10 years agoPosts: 363
Got distracted a bit but I am back now. I'm confused, how do you use this arctan function? since I need 3 angles to rotate the object matrix using; GRRLIB_ObjectViewRotate( angle1,angle2, angle3); If I need the object at player_position to "look at" the player_direction.
For example if player_position = { 5, 5, 5 } and player_direction={ -10,50,100}.
```angle1=atan2(player_direction.x, player_position.x);
angle2=atan2(player_direction.y, player_position.y);
angle3=atan2(player_direction.z, player_position.z);
```
 wilco2009 Re: Calculating the angle of a point in relation to another January 26, 2012 08:35PM Registered: 10 years agoPosts: 87
For the calculation of the angle for each rotation axis, you have to take the other two coordinates.

- To calculate rotation around x axis you have to take y and z coordinates ------> angle = atan2(y,z)
- To calculate rotation around y axis take x and z coordinates -----> angle = atan2(x,z)
- To calculate rotations around z axis take x and y coordinates ------> angle = atan2(x,y)

I'm not sure about the order of each coordinate but you can try. If is in opposite order the result is a negative angle.
 Titmouse Re: Calculating the angle of a point in relation to another January 28, 2012 12:10PM Registered: 10 years agoPosts: 99
Owen Q: for you - are you at this state yet - i.e. rotate,scale & translating objects independent of the camera.
I use this but it suffers from gimbal lock (I'm looking into using quaternions, but I'm finding it hard going)
The rotation part is giving me big problems (gimbal lock)
Are you turning the car on just one axis - so don't suffer from gimbal lock?
``` // suffers from gimbal lock - but works if you keep within the limits of matrix rotation)
guMtxConcat(mat3,Model,Model);
guMtxConcat(mat2,Model,Model);
guMtxScaleApply(Model,Model,0.18f,0.18f,0.18f);   // Scale it down
guMtxTrans(mat, 35,12, -450);  // translate = move it
guMtxConcat(mat,Model,Model);  // concat, i.e. Matrix multiplication - create new matrix from the pair
guMtxConcat(m_pWii->GetCamera()->GetcameraMatrix(),Model,Model);
// ready for render, i.e setup thing like GX_LoadPosMtxImm with the Model and render
```
 owen Re: Calculating the angle of a point in relation to another January 28, 2012 01:54PM Registered: 10 years agoPosts: 363
^ I am currently rotating the car in relation to itself by a small amount (40 degrees) on the y axis. No pitch or roll yet but I will probably try to apply those 1 at a time.
 owen Re: Calculating the angle of a point in relation to another January 29, 2012 05:00PM Registered: 10 years agoPosts: 363
@wilco2009

well here I am again with not much success with atan2. I am getting values but they are very small.
```rotation.x = atan2(player_direction.y,player_position.z);
rotation.y = atan2(player_direction.x,player_position.z);
rotation.z = atan2(player_direction.x,player_position.y);```

I tried multiplying the rotation values by 180 but that still did not result in constant rotation. Do the value have to be normalized by atan2 to work?
 wilco2009 Re: Calculating the angle of a point in relation to another January 29, 2012 11:01PM Registered: 10 years agoPosts: 87
atan give you the values in radians. You have to convert to degrees (degrees = radians* 180 / PI)
 owen Re: Calculating the angle of a point in relation to another January 31, 2012 10:25PM Registered: 10 years agoPosts: 363
Hello
hope you guys had a good weekend. Still no luck in getting the correct orientation of the model following a path in 3d.
```	rotation.x = atan2( player_position.y, player_direction.z) * 180 / M_PI;
rotation.y = atan2( player_position.x, player_direction.z) * 180 / M_PI;
rotation.z = atan2( player_position.x, player_direction.y) * 180 / M_PI;```

I have a track which is currently a square but its really made up of points;
point( -100,100,-1000); //starts at this point, turns to face next point then moves to point
point( 100,100,-1000); //repeat above
point( 100,-100,-1000);
point( -100,-100,-1000); //stops

The moving from point to point works fine but rotating the model using the above code does not give any good results. I want something similar to what "guLookAt" does for the camera: [libogc.devkitpro.org]
 bmeier Re: Calculating the angle of a point in relation to another February 01, 2012 03:57PM Registered: 10 years agoPosts: 5
Hello owen,
your vectors wont necessarily form a right angled triangle, so atan does not work.

instead have a look at the dot product of two vectors.

lets assume you have two vectors
`a = (ax, ay, az)T and b = (bx, by, bz)T`

the dot product of vector a and b is defined as
`dot(a,b)=|a| |b| cos(theta)`

where theta is the angle between vector a and vector b.
|a|, |b| is the length of vector a resp vector b
`|a| = sqrt( ax² + ay² + az²)`

this yields to
```=>dot(a,b)=|a| |b| cos(theta)
<=> cos(theta) = dot(a,b) / (|a| * |b|)
=> theta = arccos[dot(a,b) / (|a| * |b|)]```

there is a function in libogc (ogc/gu.h) to calculate the dot product of two vectors:
`f32 guVecDotProduct (guVector *a, guVector *b)`

that leaves the question wich vectors a,b to put into the equation.

tbh im a little confused by your previous posts.

what is player_direction? is it a vector from (0,0,0) to the next point in your track or is it a vector from the current player_position to the next point?

see the pictures for the difference
1. 2. Also what about the current direction the player is facing? You didnt mention this in your posts.

anyway, you basically want to put in the vectors current_direction and player_directions as in picture 2 into the equation.

deal with each rotation angle (x, y, z axis) seperatly by setting the corresponding components of the two vectors to 0.
(e.g. for angle around y-axis set current_directiony=current_directiony=0)
 owen Re: Calculating the angle of a point in relation to another February 01, 2012 07:25PM Registered: 10 years agoPosts: 363
@bmeier sorry for the confusion. There are actually 2 problems in this thread, the first problem was rotating the player_direction vector around the player_position (on the y-axis) was solved using matrix rotation by wilco2009. The second problem is rotating the model to face the player_direction vector similar to what you have in picture number 2.

For example;

player_position = ( 9, 50, 200 )
player_direction = ( 300, 80, -100 )

so if I put those into guVecDotProduct I am not sure what to do with them to get the 3 angles that I need. When I set the corresponding x,y,z components to zero what do I use to calculate the angles? (sorry my algebra is not very strong )

NB. By default the player model is always facing/looking down -z.

Edited 2 time(s). Last edit at 02/01/2012 07:33PM by owen.
 bmeier Re: Calculating the angle of a point in relation to another February 01, 2012 08:14PM Registered: 10 years agoPosts: 5
Quote
owen
By default the player model is always facing/looking down -z.

so vec a is (0, 0, -1) (negative z axis), vec b = (300, 80, 100) (player_direction)

to get the angle you need to rotate around y axis you set

a=(0, 0,-1)
b=(300, 0, 100), [b_y was 80]
and do the calculations

dot(a,b) = 0*300 + 0*0 + (-1)*100 = -100 // you can use guVecDotProduct for that
|a| = sqrt(0²+0²+(-1)²) = 1, |b| = sqrt(300² + 0² + 100²) = 316.23.. //i dont know of any function in libogc that calcs the length of a vector, you'll prob have to do it on you on

finally do:
theta = acos(-100/(1*316.23)) = 1.89 .. rad = 108.4 deg

do the same for rotation around x

also note that if one of the vector lengths is 0 (i.e. a=[0,0,0], |a| = sqrt(0²+0²+0²) = 0 or b=[0,0,0]) there is no angle between the 2 vectors, hence no rotation
in your setup this is always true for rotation around z (a=(0,0,-1) becomes a=(0,0,0) )

what you are basically doing is to split up a rotation in 3d space to multiple rotations in 2d space
Sorry, only registered users may post in this forum.