Sign in to follow this  

problem with angles and direction to waypoints

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

If you intended to correct an error in the post then please contact us.

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());
		//	load new
	}
	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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this