Re: Sphere SFML v0.75alpha
Reply #135 –
So Atan2 will give an angle. This is the direction to offset the distance check by.
Cos will give you the amount of X to use, Sin will give you the amount of Y to use.
So, in fact it kinda turns the persons base into an oval or a circle if they have a square base.
Image 1 is a person to the left (the player) trying to talk to the person on the right. Notice the 8 pixels do not go far enough.
Image 2 includes the correction. Since you are to the left Atan2 will return PI. Cos(PI) is 1, and so 8 pixels (the size of the bases width divided by 2). That's enough to puncture the base and give you a distance within 8 pixels of the player. Atan2 is used since we need to add this offset to each direction you could approach the other sprite at.
In fact if I assumed a square base I don't need all of that math. I just add size/2 to the distance and just use that.
Line[] pBase = person.GetBounds();
int w = (int)(pBase[0].End.X - pBase[0].Start.X);
float dx = person.Position.X - compare.Position.X;
float dy = person.Position.Y - compare.Position.Y;
if ((int)Math.Sqrt(dx * dx + dy * dy) <= _talk_dist + w/2)
return person.Name;
Since Atan2 would have just given an extra distance all around it can be simplified out. But that's not true if you approach from the east or the north for rectangular sprites, and so this accounting has to be made.