Jump to content
  • Advertisement
Sign in to follow this  
streamer

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.

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

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))
#define ToDegree( radian ) ((radian) * (180.0f / PI))

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


Link to post
Share on other sites
Advertisement
Quote:
Original post by streamer
To 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 this post


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


Link to post
Share on other sites
Quote:
Original post by haphazardlynamed
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


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


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


Link to post
Share on other sites
Quote:
Original post by erissian
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)


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


Link to post
Share on other sites
Quote:
Original post by streamer
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.


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


Link to post
Share on other sites
Quote:
Original post by erissian
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)


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


Link to post
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!