# problem with angles and direction to waypoints

This topic is 3997 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi folks, I had this system where I used two normalized vectors and calculated the angle with acos, but then I did not get the + or - so I did not know which way the angle was so I checked when the angle is bigger then the last time change turning direction. now this worked but it did not look nice. So i decided to create a different approach in which I try to calculate the exact angle and then alter the direction according to that. My plane however does not go where it supposed to be and frankly I am a little lost why this happens. here is my code(very ugly for all the changes, comments and stuff):
void Enemy::AI(TrackPart* thisPart) {
if (thisPart == NULL) return;

//return;

//if (waypointPath.at(0) == NULL) return;

angleOld = angle;
// first calculate direction
ScePspFVector3 destination, dir;
//if (waypointPath.size() == 0) return;

//destination = Vector3D::Substract(*(waypointPath.at(0)), position);
destination = Vector3D::Substract(position, *(waypointPath.at(0)));
// calculate difference :)
float tmpdistance = destination.x * destination.x + destination.z * destination.z;

if(distance < tmpdistance && tmpdistance < 100.0f) {
// erase old
waypointPath.erase(waypointPath.begin());
}
distance = tmpdistance;
destinationOld = destination;
Vector3D::Normalize(destination);

dir.x = direction.x;
dir.y = direction.y;
dir.z = direction.z;
Vector3D::Normalize(dir);

//angle = acos(Vector3D::DotProduct(dir, destination));
angle = atan2(destination.x,destination.z) - atan2(dir.x,dir.z);

if (angle > 0.0f) {
directionAngle -= 0.025f; }
else {
directionAngle += 0.025f; }
// aanvulling wanneer 0 dan is het goed :D dus hoek veranderd niet :D

Vector3D::Rotate(direction, directionAngle);

// then calculate speed

speed =20.0f;

// save old position
positionOld.x = position.x;
positionOld.y = position.y;
positionOld.z = position.z;

// update new location
position.x += (direction.x * ((float)speed / 100.0f));
position.y += (direction.y * ((float)speed / 100.0f));
position.z += (direction.z * ((float)speed / 100.0f));

};

Well the plane takes the wrong angle :s i guess in any case it does not go the right way, can anybody see what I am doing wrong exactly? substract= first minus the last rotate= rotate vector along the y axis with the second value as angle to add normalize= divide by length please note that it is a 2D problem in a 3D world, the planes are always on the same y height so moving is done on the xz plane. Hope you guys can help me :S greets ghoti

##### Share on other sites
I didn't look at the code, but I can tell you how to compute the signed angle between two vectors in 2-d:
angle = atan2(perp_dot(a,b),dot(a,b));
Does that help at all?

##### Share on other sites
Hi,

I don't know what you mean by the perp_dot is that the perpendicular dot product? and what is that exactly?

To calculate the angle I use:
angle = atan2(destination.x,destination.z) - atan2(dir.x,dir.z);

##### Share on other sites
Quote:
 I don't know what you mean by the perp_dot is that the perpendicular dot product? and what is that exactly?
perp(x,y) = (-y,x)And:perp_dot(a,b) = dot(perp(a),b)

##### Share on other sites
hi well if I do that then my plane only goes straigth ahead and does not turn:
maybe i did not od it correct:

// my old one
//angle = atan2(destination.x,destination.z) - atan2(dir.x,dir.z);

// new one
angle = atan2((-destination.y * dir.x + destination.x * dir.y), (destination.x * dir.x + destination.y * dir.y));

##### Share on other sites
It works, thank you very much jyk my plane now cruises over the track :D coolie

[Edited by - ghotirein on August 6, 2007 1:46:34 PM]

1. 1
2. 2
Rutin
19
3. 3
JoeJ
16
4. 4
5. 5

• 30
• 22
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001800
×