# path angle problem

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

## Recommended Posts

Hi all. I have an annoying problem. I'm trying to calculate path of an object between points. To explain better here is example code:

float angle;
int x, y; // these are coordinates
float fspeed = 5.0f;// moving speed

void Move(int tox, int toy)
{
#define PI    ((FLOAT)  3.141592654f)
#define ToRadian( degree ) ((degree) * (PI / 180.0f))

float target_angle = atan2 ((float)(toy - y),(float) (tox - x));

// *** REMARK

x += (int)(cos(angle) * fspeed);
y += (int)(sin(angle) * fspeed);

if(x > tox)		distx = x-tox;
else if(x < tox)	distx = tox-x;
else				distx = 0;

if(y > toy)		disty = y-toy;
else if(y < toy)	disty = toy-y;
else				disty = 0;

if (sqrt((float)(distx*distx+disty*disty)) < 5.0f)
{
moving = REACHED;
}
}


//*** REMARK if I put there a line: angle = target_angle; then object is moving perfectly on linear line, but I need rotated movement with spline. So I tried to add or sub from angle +-0.1f and there I run into a problem I can't solve: target_angle is variable with value between -pi to +pi, and x,y coordinate of target point is moving, so sometimes if target_angle goes beyond -pi (-180 degree) it jumps to for example at 179 degree(-181). And if I add this code: if(angle > target_angle) angle-=0.1f; else angle+=0.1f; it simply wont work, because of angle limit, and my enemy nicely goes out of screen on some crazy sinusoid. How can I solve this crazy thing? Thanks in advance

##### Share on other sites
Quote:
 Original post by streamerTo explain better here is example code:

Actually, in this case I would say that posting the code first actually makes the explanation harder to understand...
I'm not entirely sure just what you are building...thus its kinda hard to know what sections of code are helping or hindering that goal... whatever it might be

At any rate... regarding the Sign of the angle changing and causing things to mess up...
I'm going to guess that you are using Absolute angles for both 'angle' and 'target_angle' and as you can see, the > comparison fails because of the signs of the angles...
You probably need to use Relative measurement between the enemy and target...

Ugh... I hate using angles for this kinda thing... I use vectors myself.
Try:
use 0-360 ranges for your angles

subtract angle from target_angle
if the result is positive and less than 180 then you need to increase angle
if it was positive and greater than 180 then it is better to turn around the other direction and you need to decrease angle
if it was negative and greater than -180 then you need to decrease angle
if it was negative and less than -180 then you need to increase angle

get the idea? you find the difference in degrees, and do a check against the 180 mark to see if it has 'wrapped around' or not(to avoid turning a full 359 degress when the target was just 1 degree in the opposite direction)...

##### Share on other sites
Well what I want to achieve is something like spline movement across several x,y coordinates. My example try to move object between x,y and xto,yto coordinate, but not on linear line. I calculate rotation angle of object, then destination angle, and I tried to angle of object decrease or increase in steps, moving toward destination angle. That gives me nice angled movement.

##### Share on other sites
Quote:
 Original post by haphazardlynamedsubtract angle from target_angle if the result is positive and less than 180 then you need to increase angleif it was positive and greater than 180 then it is better to turn around the other direction and you need to decrease angleif it was negative and greater than -180 then you need to decrease angleif it was negative and less than -180 then you need to increase angle

Yeah I tried this. But if angle is -170 and destination angle is 160, then the difference is only 30, so I need to decrese. The point is that angle is not greater than 180 and not less than -180.

If there is any other solution to this problem, I'm listening[smile].
But I'm not so good in math, so I'll better understand by example.
Thanx

##### Share on other sites
Well, keeping in the spirit of your method, you could use:
difference_angle = (target_angle - current_angle) % 360;
if (difference_angle > 180) difference_angle -= 180;

difference_angle will the be in the range of (-180,180)

##### Share on other sites
Quote:
 Original post by erissianWell, keeping in the spirit of your method, you could use:difference_angle = (target_angle - current_angle) % 360;if (difference_angle > 180) difference_angle -= 180;difference_angle will the be in the range of (-180,180)

I tried this also. It wont work.
Actualy I found something on the net, different approach on what I want to achieve.

Example
This is it.
How can I implement this? I can't find any c++ code on net. I totaly abandoned previous approach. This is exactly what I need.

##### Share on other sites
Quote:
 Original post by streamerExample This is it.How can I implement this? I can't find any c++ code on net. I totaly abandoned previous approach. This is exactly what I need.

He is using a Vector approach.
You take the vector from your current location to the target point.
Then you compare it to your current vecocity vector (via subtraction) to get a 'seek steering' vector as he calls it.
Each update he adds a little bit of the steering vector onto the character's current velocity vector (as limited by max acceleration and other factors like turning speed).

Eventaully the steering contributions end up pulling the current velocity vector to finally hit the target...

##### Share on other sites
Thank you all. I'll have another one sleeples night [smile]

##### Share on other sites
Quote:
 Original post by erissianWell, keeping in the spirit of your method, you could use:difference_angle = (target_angle - current_angle) % 360;if (difference_angle > 180) difference_angle -= 180;difference_angle will the be in the range of (-180,180)

Whoops, that has two problems:
1. In C, -100%360 = -100. I was still in the matlab mindframe, where -100%360 =260.
2. Even if that was right, there shouldn't be an if statement. It should have just done the subtraction.

Corrected:

difference_angle = (target_angle - current_angle);
// Assuming your input angles are in the range [0,360), the answer should be in (-360,360)
if (difference_angle < -180) difference_angle += 360;
if (difference_angle > 180) difference_angle -= 360;

##### Share on other sites
Actually I noticed this. I changed my whole code to work with vectors and not with angles. Now it works perfectly.
Anyway I didn't saw anywhere any explanation that moduo operand can produce negative numbers. I always thought that moduo is giving positive number in range 0-valrange( myval%valrange).

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632925
• Total Posts
3009235
• ### Who's Online (See full list)

There are no registered users currently online

×