Correct formula to detect if something is facing my position
Correct formula to detect if something is facing my position
What's the correct formula to detect if something is facing my position.
I have a std::map that holds every object in the area but I'd like to check wheater other objects/players are facing my direction. I'm pretty sure all I need is my position the other object position and it's current angle but I'm not sure how to calculate it.
I have a std::map that holds every object in the area but I'd like to check wheater other objects/players are facing my direction. I'm pretty sure all I need is my position the other object position and it's current angle but I'm not sure how to calculate it.
- Administrator
- Site Admin
- Posts: 5313
- Joined: Sat Jan 05, 2008 4:21 pm
Re: Correct formula to detect if something is facing my posi
math.atan2(y2-y1, x2-x1) will give you the angle between two points. First, get the angle that the object is looking, then compare it to the angle that you calculate with atan2(). If it is within some threshold, it is looking at you.
Remember that atan2() will return radians between 0 and 2PI. 6.27 and 0.0 would, really, be very close to each other. You'll need a special case to handle roll-overs in this gap.
Remember that atan2() will return radians between 0 and 2PI. 6.27 and 0.0 would, really, be very close to each other. You'll need a special case to handle roll-overs in this gap.
Re: Correct formula to detect if something is facing my posi
My angle varaible is 0-360 I believe. I'm gonna look into this a bit and see what I can do. Thanks.
Re: Correct formula to detect if something is facing my posi
Ok, so I've figured out how this math works now I'm onto another smal math problem. After I watched the angle variable I get from the object it a byte between 0 and 254. How could I convert my 0-359 degrees to 0-254?
I figured this out after some brain storming... "headache" degree = (angle * (255/2) / PI)
EDIT: That was a dumb question but now I've got another...probably easy too. The angle also rotates counter clockwise how could I reverse the way it rotates?
I figured this out after some brain storming... "headache" degree = (angle * (255/2) / PI)
EDIT: That was a dumb question but now I've got another...probably easy too. The angle also rotates counter clockwise how could I reverse the way it rotates?
Code: Select all
const float PI = 3.1415;
int main()
{
int x1 = 1111;
int y1 = 1111;
int x2 = 1200;
int y2 = 1111;
float angle = atan2(y2-y1, x2-x1);
float degree = (angle * 180 / PI);
printf("atan2 Angle: %f Degree: %f\n", angle, degree);
system("pause");
return 0;
}
- Administrator
- Site Admin
- Posts: 5313
- Joined: Sat Jan 05, 2008 4:21 pm
Re: Correct formula to detect if something is facing my posi
Why would you change that? That's the natural way math dealing with circles works. You really shouldn't bother converting to/from degrees or worrying about the angle being clockwise or counter-clockwise. Adding unnecessary complications like that will only make it more difficult to understand. Still, if you're really set on doing that, you could always just take the full circle (2PI) and subtract the angle you get.
Additionally, you should use math.pi instead of your own constant.
Additionally, you should use math.pi instead of your own constant.
Re: Correct formula to detect if something is facing my posi
Well what's happening is...
I'm reading from a packet where all the players, NPC, enemies...everything is. In the packet it's send me a rotation byte 0-255. What I'm trying to do it check wheater the unit is facing me based on the rotation it has (0-255) This variable moves from 0 = right, 65 = up, 131 = left, 189 = down... Them are not exact but thre fairly close and it covers every rotation in between. The variable is going counter clockwise so I can't just match up my degrees with this variable. I need to convert what I have to degrees that go counter clockwise I guess? I don't know how else to do it.
Edit: Should I try to convert the byte 0-255 to rads?
I'm reading from a packet where all the players, NPC, enemies...everything is. In the packet it's send me a rotation byte 0-255. What I'm trying to do it check wheater the unit is facing me based on the rotation it has (0-255) This variable moves from 0 = right, 65 = up, 131 = left, 189 = down... Them are not exact but thre fairly close and it covers every rotation in between. The variable is going counter clockwise so I can't just match up my degrees with this variable. I need to convert what I have to degrees that go counter clockwise I guess? I don't know how else to do it.
Edit: Should I try to convert the byte 0-255 to rads?
- Administrator
- Site Admin
- Posts: 5313
- Joined: Sat Jan 05, 2008 4:21 pm
Re: Correct formula to detect if something is facing my posi
As for clockwise vs counter-clockwise, I think you are just using the wrong origin. The angle from player to target would be the inverse of target to player. I think that could be your issue.
1-byte angles, variously named but I'm most familiar with 'Allegro degrees', are easy to convert to and from. To convert your radians, just do this:
1-byte angles, variously named but I'm most familiar with 'Allegro degrees', are easy to convert to and from. To convert your radians, just do this:
Code: Select all
adeg = radians*255 / (math.pi*2);
Re: Correct formula to detect if something is facing my posi
x2/y2 = target positions
x1/y1 = player positions
What I was doing is
Is this the correct way to turn a 1byte angle into a radian?
EDIT:
NVM! It was a stupid error on my part.. I forgot to add the second param to printf so it was printing some random number for radians instead of the actualy radians! Woks perfect. Thanks for the help .. If i keep messing with this stuff I may actually learn some math.
EDIT: Ok now assuming my the player object y is 10 and the target is 0, when I use atan2(y2-y1, x2-x1) the output is :
These 2 should be equal. player = y10 target = 0 would mae the player lower the the target which is what 191.25 in degrees for the byte angle.
works fine untill atan2 goes into the neg range then radians jut keeps going +.
x1/y1 = player positions
What I was doing is
Code: Select all
float radians = atan2(y2-y1, x2-x1);
float degree = (angle * (255/2) / PI);
Code: Select all
radians = tan(byteAngle * PI / 127.5); //127.5 = (255/2)
NVM! It was a stupid error on my part.. I forgot to add the second param to printf so it was printing some random number for radians instead of the actualy radians! Woks perfect. Thanks for the help .. If i keep messing with this stuff I may actually learn some math.
Code: Select all
printf("atan2 Angle: %f byte Angle: %f\n", angle, radians);
These 2 should be equal. player = y10 target = 0 would mae the player lower the the target which is what 191.25 in degrees for the byte angle.
atan2 Angle: -1.570796 byte Angle: 4.712389
Press any key to continue . . .
Code: Select all
#include <math.h>
int main()
{
int x1 = 0;
int y1 = 10;
int x2 = 0;
int y2 = 0;
float angle = atan2(y2-y1, x2-x1);
float radians = 191.25 * M_PI / 127.5; //127.5 = (255/2)
printf("atan2 Angle: %f byte Angle: %f\n", angle, radians);
system("pause");
return 0;
}
- Administrator
- Site Admin
- Posts: 5313
- Joined: Sat Jan 05, 2008 4:21 pm
Re: Correct formula to detect if something is facing my posi
My mistake. atan2 goes from -pi to pi. To correct that, just add pi to the result and everything should be fine.
Re: Correct formula to detect if something is facing my posi
Darn, wish I would have figured that one out. Works good now, thanks.
Just incase anyone has a similar problem..
EDIT:
I'll probably just convert the rads to degrees tho to make it easier for me.
Just incase anyone has a similar problem..
Code: Select all
float angle = atan2(y2-y1, x2-x1)+(M_PI);//I find the radian between the target and myself.
float radians = byteAngle * (M_PI / 127.5); //I get the byteAngle from the packet for the target then convertit to radians.
//If they both match within a range I'll set the target is facing me to true.
I'll probably just convert the rads to degrees tho to make it easier for me.
Code: Select all
float angle = atan2(y2-y1, x2-x1)+(M_PI);//I find the radian between the target and myself.
float radToDeg = angle * (127.5 / M_PI);//I'd compare with the degrees for simplicities sake and to not break my mind.
Who is online
Users browsing this forum: No registered users and 0 guests