Hi there.
I was looking for a code in order to implement a radar system
What I found is reported below:
function DrawRadarBlip(go, spotTexture) {
var gameObjPos = go.transform.position; // prendo la posizione dell’oggetto
var dist = Vector3.Distance(playerPos.position, gameObjPos); // calcolo la distanza vettoriale tra giocatore e oggetto
var dx = playerPos.position.x - gameObjPos.x; // calcolo la direzione lungo X
var dz = playerPos.position.z - gameObjPos.z; // calcolo la direzione lungo Z
I think I can understand almost the code apart from the VALUE 270 that is subtracted from the angle the player needs to face to.
Why do we need to add that value?
Let’s say for instance that the player is in the origin of the axes (0,0) and the object is at coordinates (2,2), the arctan gives me a 45 degree angle. Then cos(45) = 0.70 and sin(45) = 0.70 that is obviously correct.
But if we calculate 45 - 270 = (-225 degree), Before calculating the cosine and sine, what we obtain is cos(-225) = -0.70 and sin(-225) = 0.70.
So the first calculation seems more correct to me.
I hope I have explained the problem correctly.
function DrawRadarBlip(go, spotTexture) {
var gameObjPos = go.transform.position; // prendo la posizione dell'oggetto
var dist = Vector3.Distance(playerPos.position, gameObjPos); // calcolo la distanza vettoriale tra giocatore e oggetto
var dx = playerPos.position.x - gameObjPos.x; // calcolo la direzione lungo X
var dz = playerPos.position.z - gameObjPos.z; // calcolo la direzione lungo Z
var deltay = Mathf.Atan2(dx, dz) * Mathf.Rad2Deg -270 - playerPos.eulerAngles.y;
radarSpotX = dist * Mathf.Cos(deltay * Mathf.Deg2Rad) * mapScale;
radarSpotY = dist * Mathf.Sin(deltay * Mathf.Deg2Rad) * mapScale;
GUI.DrawTexture(Rect(radarWidth/2.0 + radarSpotX, radarHeight/2.0 + radarSpotY, 2, 2), spotTexture);
GUI.DrawTexture(Rect(radarWidth, radarHeight, 2, 2), spotTexture);
}
Now I think the value 270 is related to the fact that the GUI.DrawTexture method has the origin at the top left corner
but I’m getting really confused as now I cannot understand the meaning of the following line:
var deltay = Mathf.Atan2(dx, dz) * Mathf.Rad2Deg -270 - playerPos.eulerAngles.y;
why dx e dz are calculated subtracting the player’s position from the target and not viceversa?
if the player is at (0,0) and the object at (2,2) I would expect to have a direction of (2,2) = (2-0,2-0) and not (0-2,0-2) = (-2,-2).
what’s the geometric meaning of calculating the Atan2?
if it is the angle whose tangent is dx/dz the geometric/visual result on the circumference doesn’ make sense to me
I know it’s difficult to explain but I really like to understand the geometrical meaning of the previous snippet.
I would really appreciate if someone could explain the visual meaning to me, even by example on the circumference.