- Home /
find point projection on circle around another point
Hello
-red is player position
-blue are objectives position
-orange are the points i need to find, always positioned on the black circle around the player, even if the objective point is inside of it.
How can i do it?
Thaaanks <3
Answer by Bunny83 · Feb 03, 2018 at 11:54 AM
Simple vector math.
Given:
Vector3 R_Pos;
Vector3 B_Pos;
float circleRadius;
Solution:
// vector from R to B
Vector3 dir = B_Pos - R_Pos;
// same direction but the length is reduced / expanded to circleRadius
dir = dir.normalized * circleRadius;
Vector3 O_Pos = R_Pos + dir;
thank you very much @Bunny83 !! this was actually the first try i made, but since i have a 2D game but still have to use Vector3s, the Z-axis was the one that misleaded me. I had to set the Z axis the same on all objects in order to have it working correctly. BTW thanks again!
If it's an actual 2d game (using the x-y-plane) you can simply use "Vector2"s ins$$anonymous$$d. You can directly assign a Vector3 to a Vector2 variable as they have an implicit conversion operator. This would naturally get rid of any z differences. It would also be a bit more efficient to use Vector2s as all operations on Vector2 only have to deal with 2 components ins$$anonymous$$d of 3.
Though even the problem is a 2d problem the question didn't explicitly ask for a 2d solution. 2d problems can also exist in 3d in an arbitrarily choosen plane. However if we talk about circles and points in a 2d setup we would assume that they lie in the same plane.