简体   繁体   English

旋转所有矩形角

[英]Rotating all rectangle corners

I'm working on a game where you are a spaceship. 我正在开发一款你是飞船的游戏。 This spaceship has to be able to rotate. 该太空飞船必须能够旋转。 The rectangle has two arrays x[] , y[] containing all the corners positions of the rectangle. 矩形具有两个数组x[]y[]包含矩形的所有角位置。 But when I apply the rotation formula, I get a rather wierd rotation. 但是,当我应用旋转公式时,我得到了一个相当奇怪的旋转。 To try to explain it, it looks like it's rotating the bottom left of the screen. 为了解释它,它看起来好像在旋转屏幕的左下角。

To make these corner arrays i take in an x position, y position, width and height. 为了使这些角数组,我采取x位置,y位置,宽度和高度。

Making of the corner-arrays 角落阵列的制作

public Vertex2f(float x, float y, float w, float h){
    this.x[0] = x; 
    this.y[0] = y;

    this.x[1] = x+w;
    this.y[1] = y;

    this.x[2] = x+w;
    this.y[2] = y+h;

    this.x[3] = x;
    this.y[3] = y+h;
}

My rotation function 我的旋转功能

public void rotate(float angle){
    this.rotation = angle;

    double cos = Math.cos(rotation);
    double sin = Math.sin(rotation);

    for(int i = 0; i < x.length; i++){
        x[i] = (float)(cos * x[i] - sin * y[i]);
        y[i] = (float)(sin * x[i] + cos * y[i]);

    }

}

If it helps I am using LWJGL/OpenGL in java for all the graphics and Slick2d to load and init the sprites I am using. 如果有帮助,我将在Java中为所有图形使用LWJGL / OpenGL,并使用Slick2d加载并初始化我正在使用的精灵。

Try this one: 试试这个:

public void rotate(float angle){
    this.rotation = angle;

    double cos = Math.cos(rotation);
    double sin = Math.sin(rotation);

    double xOffset = (x[0]+x[2])/2;
    double yOffset = (y[0]+y[2])/2;

    for(int i = 0; i < 3; i++){
        x[i] = (float)(cos * (x[i]-xOffset) - sin * (y[i]-yOffset)) + xOffset;
        y[i] = (float)(sin * (x[i]-xOffset) + cos * (y[i]-yOffset)) + yOffset;

    }

}

you have to rotate around center of your rectangle. 您必须围绕矩形的中心旋转。 Otherwise center is in x=0 and y=0 否则中心在x = 0和y = 0

edited: 编辑:

public void rotate(float angle){
    this.rotation = angle;

    double cos = Math.cos(rotation);
    double sin = Math.sin(rotation);

    double xOffset = (x[0]+x[2])/2;
    double yOffset = (y[0]+y[2])/2;

    for(int i = 0; i < 3; i++){
        double newX = (float)(cos * (x[i]-xOffset) - sin * (y[i]-yOffset)) + xOffset;
        double newY = (float)(sin * (x[i]-xOffset) + cos * (y[i]-yOffset)) + yOffset;

        x[i] = newX;
        y[i] = newY;
    }
}

see other thread 查看其他主题

The problem with the formulas 公式的问题

    x[i] = (float)(cos * x[i] - sin * y[i]);
    y[i] = (float)(sin * x[i] + cos * y[i]);

apart from the missing rotation center is that you change x[i] in the first formula but expect to use the original value in the second formula. 除了缺少旋转中心之外,您还可以在第一个公式中更改x[i] ,但希望在第二个公式中使用原始值。 Thus you need to use local variables lx, ly as in 因此,您需要使用局部变量lx, ly

    float lx = x[i] - xcenter;
    float ly = y[i] - ycenter;
    x[i] = xcenter + (float)(cos * lx - sin * ly);
    y[i] = ycenter + (float)(sin * lx + cos * ly);

If the object already is rotated with an angle of rotation , then this code adds the angle angle to the total rotation angle. 如果对象已用的旋转角度rotation ,那么这个代码将角angle的总旋转角度。 If instead the given argument angle is to be the new total rotation angle, then the sin and cos values need to be computed with the angle difference. 相反,如果给定的自变量angle是新的总旋转角度,则需要使用角度差来计算sincos值。 That is, start the procedure with, for instance, 也就是说,例如,使用

public void rotate(float angle){


    double cos = Math.cos(angle - rotation);
    double sin = Math.sin(angle - rotation);

    this.rotation = angle;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM