- Home /
How to calculate the real position after rotate with a original point without using Transform in 2D game
Now I get the original point's position, direction and the angle with 2 object. And I want to physics2D.CircleCastAll to hit some object. So I want to get the real position from some script. Please Help>"< and thankyou
Ah, could you elaborate? As I understand it, you want to calculate where a given point lands when you rotate it around another given point by a specific angle, is that accurate?
Answer by Ymrasu · Aug 12, 2020 at 11:38 AM
I don't quite understand what you are asking with physics2d.circlecastall, but the from the picture:
Vector3 RotatePointAroundPoint(Vector3 point1, Vector3 point2, float angle)
{
angle *= Mathf.Deg2Rad;
var x = Mathf.Cos(angle) * (point1.x - point2.x) - Mathf.Sin(angle) * (point1.y - point2.y) + point2.x;
var y = Mathf.Sin(angle) * (point1.x - point2.x) + Mathf.Cos(angle) * (point1.y - point2.y) + point2.y;
return new Vector3(x,y);
}
You can use this to rotate a point around another point by a certain amount of degrees. Your picture looks like the small object is rotating 120 degrees clockwise (negative) around the big object. So you can use the function like this:
newPosition = RotatePointAroundPoint(oldPosition, otherPosition, -120f);
If you just want to continually rotate around you would use the delta angle or rotation like this:
void Update()
{
// Rotate -90 degrees per second (negative for clockwise)
var rotateBy = -90f * Time.deltaTime;
newPosition = RotatePointAroundPoint(oldPosition, otherPosition, rotateBy);
}
Ok, after finally seeing your code I think I know what you want now. You T, P, and O to remain in that triangle formation as it rotates (toward target) around the object? This is what I got:
void Update()
{
Vector3 dir = (target.position - transform.position).normalized;
float angle = getAngle(transform.position, target.position);
Vector3 clusterPosition = dir * 5f + transform.position;
clusterPosition = RotatePointAroundPoint(clusterPosition, transform.position, angle);
var angleDir = Quaternion.Euler(new Vector3(0f, 0f, angle * Mathf.Rad2Deg - 90f));
T.position = clusterPosition + angleDir * a;
P.position = clusterPosition + angleDir * b;
O.position = clusterPosition + angleDir * c;
}
float getAngle(Vector2 me, Vector2 target)
{
return Mathf.Atan2(target.y - me.y, target.x - me.x);
}
I had to modify your getAngle function, the rotate function is unchanged.
Yes! That exactly I need. But can I ask one more questions? Whithout using Transform, and I design some shape. When I get the position, how can I rotate the shape by following the direction(angle) and how can i get the transform.position but not world.position by following the object
Would this work for you?
Quaternion rotation = Quaternion.LookRotation(direction, Vector3.back);
I'm not sure what you meant about transform.position but not world.position.
Thankyou so much for your reply. I am sorry that with my explanation. Actually, i just want a script(like a function) in encapsulation for calculate the position like this picture. But the script have not contain a Transform. So I need some code for me to doing something like this. But it is not use Transform
The transform.position is mean Self(relatively) position. Like the picture, although the original point in(0,1000) in world position, but the left hand side of this object is also (-1,1) relatively position nearly the object.
Thankyou for your help^^