开发者

How can I uniformly randomly rotate a Vector2 between 0 and 360 degrees?

开发者 https://www.devze.com 2023-04-01 03:12 出处:网络
I have a Vector2: Vector2 v = new Vector2(1,0开发者_StackOverflow中文版); How can I randomly rotate this vector (about (0,0)), with a uniform distribution in the range [0,360) degrees?To randomly r

I have a Vector2:

Vector2 v = new Vector2(1,0开发者_StackOverflow中文版);

How can I randomly rotate this vector (about (0,0)), with a uniform distribution in the range [0,360) degrees?


To randomly rotate a vector v, counter clockwise:

Vector2 v = new Vector2( 1,0 );

Random rnd = new Random();
double rotationAngle = 2.0 * Math.PI * rnd.nextDouble();

Vector2 vRotated = new Vector2( 
   (v.x)*Math.cos(rotationAngle) + (v.y)*Math.sin(rotationAngle),
   (v.y)*Math.cos(rotationAngle) - (v.x)*Math.sin(rotationAngle)
);

The transformation mathematics comes from here

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号