RPG game assistant wants to automatically fight monsters
After getting the latest monster information, you need to face the monster
Otherwise, there is no way to attack and release skills when facing monsters and so on.
There are many situations in the direction of game design
The first
2D, 2.5D old games such as Legends
His orientation is generally very fixed 4 orientations or 8 orientations
That is to say, it is not freely oriented, and can only be oriented in four directions, east, south, north, and west.
Or east, northeast, north, northwest, west, southwest, southeast, and 8 directions
Then the orientation calculation is also very simple
Just calculate the relationship between the coordinates of the monster and the character
For example
X monster > X character && Y monster > Y character
Then if you want to face the monster, just modify the orientation value to face the northeast
Generally, the 8 orientations are represented by 8 numerical values from 0-7
the second
2D version free orientation
It is said to be a 2D version of free orientation, but in fact many 3D games are using it
A very simple understanding is that the game is 3D, but the character can only turn in 2D plane, and the head cannot be lifted up and overlooked
For example, QQ Westward Journey
In general, the orientation value of this game is uniformly arranged on the coordinate system from 0 to a value
We can easily calculate it by calculating its changing law
For example, QQ Westward Journey increases counterclockwise from the positive X axis to 0
It has been increased to 256 and then overlaps with the X axis and becomes 0
Then we know the law of its change, we can implement the code
as follows
DWORD Call_Angle towards the monster (FLOAT x, FLOAT y)
{
T character attribute A;
AC initialize();
FLOAT X=A.fX;
FLOAT Y=A.fY;
DWORD B=0;
if (x>X&&y>Y)//first quadrant
{
B=(DWORD)(atan2(yY,xX)/3.1415926 *180);//real angle
B=B*64/90 ;//Same ratio game angle
B=0+B;
}
if (x<X&&y>Y)//second quadrant
{
B=(DWORD)(atan2(y-Y,X-x)/3.1415926 *180);
B=B*64/90 ;
B=128-B;
}
if (x<X&&y<Y)//third quadrant
{
B=(DWORD)(atan2(Y-y,X-x)/3.1415926 *180);
B=B*64/90;
B=128+B;
}
if (x>X&&y<Y)//fourth quadrant
{
B=(DWORD)(atan2(Y-y,x-X)/3.1415926 *180);
B=B*64/90;
B=256-B;
}
return B;
}
third
through the first and second
We can clearly feel the upgrade from 8-position to multi-position
But this is still not enough no matter how many parts you divide the coordinate system or not absolute freedom
then what should we do?
The game invents a more free orientation
For example, Tianya Mingyue Knife
has such a design concept
Take the character as the center and spend a circle with a general radius of 1 (it doesn't matter how much it is, we can multiply it)
Connect the line with the monster, the intersection of the circle is our direction
Then this becomes a free orientation, you can be as accurate as you want
It is also very simple to calculate
The origin of the character, the intersection point of the circle, the perpendicular line from the intersection point to the X-axis, and the intersection point of the X-axis form a triangle
The origin of the character, the coordinate point of the monster, the intersection point of the vertical line from the coordinate point of the monster to the X axis and the X axis form a triangle
2 triangles are similar triangles
Then the small triangle h / 1 = (Y monster - Y character) / distance
Small triangle l / 1 = (X monster - X character) / distance
The obtained h and l are the coordinates of our circle intersection
There are further ways of sublimation
is the orientation of the 3D version
Then he won't be a circle anymore
It's a 3D ball
But the algorithm remains unchanged
only the distance has changed
Similar triangles are still similar triangles
h=(Y monster-Y character)/distance
l=(X monster-X character)/distance
z=(Z monster-Z character)/distance
Among them distance=sqrt((person.fX-fX) (person.fX-.fX)+(person.fY-fY) (person.fY-fY)+(person.fZ-fZ)*(person.fZ-fZ ));
fourth
Generally used by FPS games
It's also the same angle as the space shuttle, etc.
is the dive angle, the roll angle and the rotation angle
The rotation angle represents its own rotation, which is almost useless in the game
Then the dive angle represents the angle between the gun direction and the horizontal plane
The swing angle represents the angle at which the direction of the gun and the center line swing left and right
The calculation method can refer to the second arc tangent method