在Java中的Vector2d类中旋转

Dav*_*les 0 java vector rotation

我已经在这工作了一个小时,只是无法得到它.

我有一个Vector2d类:

public class Vector2d
{
    public double x = 0.0;
    public double y = 0.0;

    ....
}
Run Code Online (Sandbox Code Playgroud)

这个vector类有一个rotate()方法,这会给我带来麻烦.

第一个片段似乎使x和y值越来越小.第二个工作正常!我错过了一些简单的东西吗?

public void rotate(double n)
{
    this.x = (this.x * Math.cos(n)) - (this.y * Math.sin(n));
    this.y = (this.x * Math.sin(n)) + (this.y * Math.cos(n));
}
Run Code Online (Sandbox Code Playgroud)

这有效:

public void rotate(double n)
{
    double rx = (this.x * Math.cos(n)) - (this.y * Math.sin(n));
    double ry = (this.x * Math.sin(n)) + (this.y * Math.cos(n));
    x = rx;
    y = ry;
}
Run Code Online (Sandbox Code Playgroud)

我在那里找不到任何差异

Tro*_*our 9

this.x当你真正想要的是原始值时,第一行设置其值在第二行中使用this.x.第二个版本工作正常,因为你没有改变this.x.