# direction between points

On 11/11/2013 at 03:53, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   r14
Platform:
Language(s) :     C++  ;

---------
hm, i am still having problems to detect the direction between two points. i thought this would work, but it seems not:

Vector angle = VectorToHPB(point_to-point_from);

how do i get the correct angle between two points?

On 11/11/2013 at 05:52, xxxxxxxx wrote:

Howdy,

Well, I think what you need is the Dot Product. The dot product of 2 normalized vectors gives you the cosine of the angle between them. :wink:

Adios,
Cactus Dan

On 11/11/2013 at 08:17, xxxxxxxx wrote:

There's also the  VectorAngle() function in the SDK that you can use. If you want to test the angle of two splines. Using their points rather than their axis values.

``````//This example gets the angle between two splines
//NOTE: This does NOT!! test the angle between the two spline objects axis values
//      You need to move the points of the splines to get a result from this code
//      It doesn't care how the two spline objects are oriented to eachother

BaseObject *sp1 = doc->SearchObject("s1");
SplineObject *s1 = ToPoint(sp1);
BaseObject *sp2 = doc->SearchObject("s2");
SplineObject *s2 = ToPoint(sp2);

Vector *s1pnts = s1->GetPointW();                //Get all the points...We only have two points in the spline
Vector *s2pnts = s2->GetPointW();                //Get all the points...We only have two points in the spline

Vector vec1 = s1pnts[1]-s1pnts[0];               //The first spline's vector
Vector vec2 = s2pnts[1]-s2pnts[0];               //The second spline's vector

Real angle_rad = VectorAngle(vec1, vec2);        //This gets the angle bewteen the two spline vectors in radians
Real angle_deg = angle_rad*180.0/pi;             //Converts radians to degrees
GePrint(RealToString(angle_deg));
``````

-ScottA

On 11/11/2013 at 08:35, xxxxxxxx wrote:

Howdy,

Ah! I've never noticed the VectorAngle() function in the SDK.
I've always just done it myself like this:

``````Real angle = ACos(!v1 * !v2);
``````

Adios,
Cactus Dan

On 11/11/2013 at 09:08, xxxxxxxx wrote:

thank you for your inputs. i came across the VectorAngle today, too. but it seems it doesn't return what i want. because if i rotate an object by the retrieved value, it behaves wrong when the second point passes the axis of the first point. generally i am trying to rotate an object placed at point x so it points the direction from point x-1 to point x+1. somehow i need to divide the result of VectorAngle by two, also.

btw, what would i need to do if i need 3-dimensional rotation?

On 11/11/2013 at 22:54, xxxxxxxx wrote:

here is a shot of what is happening.

edit: ok, got it now. in my previous approach i missed using the normalized vectors. now it seems to work:

VectorToHPB(from.GetNormalized() - to.GetNormalized())

On 12/11/2013 at 11:10, xxxxxxxx wrote:

Howdy,

Yes, vectors need to be normalized to get a proper angle calculation. :wink:

Adios,
Cactus Dan