水平翻转一个角度

发布于 2024-08-03 06:33:37 字数 1374 浏览 3 评论 0原文

我想知道一种在水平轴上翻转角度的方法,而无需执行许多操作。假设我的角度为 0(在代码的坐标系中“指向右侧”),则翻转角度应为 180(指向左侧)。如果是 90(向上),翻转后仍应为 90。89 为 91,依此类推。 我可以对角度所暗示的 X/Y 速度进行操作,但这会减慢速度,而且我觉得这不是正确的方法。 我不太了解数学,所以我可能会用错误的名称来称呼事物......任何人都可以帮忙吗?

编辑:抱歉我花了很长时间,我不得不长时间离开电脑,好吧...... http://img215.imageshack.us/img215/8095/screenshot031v.jpg

这个屏幕截图可能可以。上面的结构是两颗卫星和一根连接到中心白点的光束。两颗卫星应继承白点的角度(出于调试目的可见),因此如果它瞄准某个角度,它们就会跟随。左边的卫星是镜像的,所以我按照建议用180度角来计算,尽管这也是我的第一次尝试。正如你所看到的,它不是镜像的而是翻转的。而当白点旋转时,它会向后旋转。另一个还好。

这是对与其他事物相关联的事物的角度重新计算,pid 将是父级,id 是当前值。 pin.ang是对象链接时复制的角度偏移量,因此旋转时保持位置不变:

if(object[id].mirror)
    object[id].angle = 180 - (object[id].pin.ang + object[pid].angle);
else
    object[id].angle = object[id].pin.ang + object[pid].angle;

这就是具体的旋转部分。 OpenGL。 offx/y 用于偏离中心旋转的物体,例如即将从那里射出的光束,它使其他一切都正确。

glTranslatef(list[index[i]].x, list[index[i]].y, 0);
glRotatef(list[index[i]].angle, 0.0, 0.0, 1.0);
glTranslatef(list[index[i]].offx, -list[index[i]].offy, 0);

当旋转速度(每次重绘添加到当前角度的整数,顺时针旋转为正,如下一个: http://img216.imageshack.us/img216/7/screenshot032ulr.jpg

所以它绝对不是 180 角,尽管它有多么明显。镜像是通过反转纹理坐标来完成的,因此它不会影响角度。恐怕这可能是 GL 轮换问题上的一个怪癖。

I want to know a way to flip an angle in a horizontal axis, without having to do many operations. Say I have an angle of 0 ("pointing right" in my code's coordinate system), the flipped angle should be 180 (pointing left). If 90 (pointing up), flipped it should still be 90. 89 is 91, and so on.
I can operate on the X/Y speeds implied by the angle but that would slow things down, and I feel it's not the proper way to go.
I don't know much math so I might be calling things by the wrong name...Can anyone help?

EDIT: Sorry I took long, I had to be out of the computer for long, OK...
http://img215.imageshack.us/img215/8095/screenshot031v.jpg

This screenshot might do.The above structure are two satellites and a beam linked to the white dot in the center. The two satellites should inherit the angle of the white dot (it's visible for debug purposes), so if it's aiming at an angle, they will follow. The satellite at the left is mirrored, so I calculated it with 180-angle as suggested, although it was my first try as well. As you can see it is not mirrored but flipped. And when the white dot rotates, it rotates backwards. The other does alright.

This is the angle recalculation for something linked to something else, pid would be the parent, and id the current. pin.ang is the angle offset copied when the object is linked to another, so it keeps position when rotated:

if(object[id].mirror)
    object[id].angle = 180 - (object[id].pin.ang + object[pid].angle);
else
    object[id].angle = object[id].pin.ang + object[pid].angle;

And this is the specific rotation part. OpenGL. the offx/y is for things rotated off-center, like the beam about to come out there, it renders everything else right.

glTranslatef(list[index[i]].x, list[index[i]].y, 0);
glRotatef(list[index[i]].angle, 0.0, 0.0, 1.0);
glTranslatef(list[index[i]].offx, -list[index[i]].offy, 0);

The rotation also seems to miss when the rotation speed (an integer added every redraw to the current angle, positive for rotating clockwise, like in this next one:
http://img216.imageshack.us/img216/7/screenshot032ulr.jpg

So it's definitely not 180-angle, despite how obvious it'd be. The mirroring is done by just reversing the texture coordinates so it doesn't affect angle. I am afraid it might be a quirk on the GL rotation thing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

壹場煙雨 2024-08-10 06:33:37

反射量(仅查看数学)将为(180 - 角度)

Angle | Reflection
------+-----------
    0 |        180
   90 |         90
   89 |         91
   91 |         89
  360 |       -180
  270 |        -90

请注意,如果您跌落到“水平面”以下,则会产生负面影响 - 您可以保持原样,或者将其作为特殊处理案件。

The reflected amount (just looking at the maths) would be (180 - angle)

Angle | Reflection
------+-----------
    0 |        180
   90 |         90
   89 |         91
   91 |         89
  360 |       -180
  270 |        -90

Note the negatives if you fall below the "horizontal plane" - which you could leave as they are, or handle as a special case.

ぇ气 2024-08-10 06:33:37

不就是简单的

结果=180-(你的角度)

Isn't it simply

result = 180-(your angle)

独留℉清风醉 2024-08-10 06:33:37

正如已经解释过的,您可以通过用 180 度减去您的角度来找到对角。例如:

180 - yourangle

直接操纵X/Y速度不会很麻烦。您只需将 X 速度的方向乘以负 1 即可反转,例如:speedx = (-1) * speedx。这将改变左右方向,例如:向左移动的物体将开始向右移动,反之亦然,并且垂直速度不受影响。

如果您使用正弦/余弦 (sin/cos) 重新计算 X/Y 速度分量,则 *(-1) 方法可能会更有效。最终这取决于您的程序的上下文。如果您正在寻找更好的解决方案,请使用更多详细信息更新您的问题。

As already explained, you find the opposite angle by subtracting your angle from 180 degrees. Eg:

180 - yourangle

Directly manipulating the X/Y speeds would not be very cumbersome. You simply reverse the direction of the X speed, by multiplying it by minus 1, example: speedx = (-1) * speedx. This would change the left-right direction, eg: something moving to the left would start moving to the right, and vice versa, and the vertical speed would be unaffected.

If you're using sine/cosine (sin/cos) to recalculate your X/Y speed components, then the *(-1) method would probably be more efficient. Ultimately it depends on the context of your program. If you're looking for a better solution, update your question with more details.

新雨望断虹 2024-08-10 06:33:37

该解决方案适用于 -Y 方向的角度(如手表)!对于 +X 方向(如学校数学),您需要交换 X 和 Y。

public static float FlipAngleX(float angle)
{
    angle = NormalizeAngle(angle);

    angle = TwoPi - angle;

    return angle;
}

public static float FlipAngleY(float angle)
{
    angle = NormalizeAngle(angle);

    if (angle < Pi)
    {
        angle = Pi - angle;
    }
    else
    {
        angle = TwoPi - angle + Pi;
    }
    return angle;
}

/// <summary>
/// Keeps angle between 0 - Two Pi
/// </summary>
public static float NormalizeAngle(float angle)
{
    if (angle < 0)
    {
        int backRevolutions = (int)(-angle / TwoPi);
        return angle + TwoPi * (backRevolutions + 1);
    }
    else
    {
        return angle % TwoPi;
    }
}

This solution is for -Y oriented angles (like a watch)! For +X orientation (like school math) you need to swap X and Y.

public static float FlipAngleX(float angle)
{
    angle = NormalizeAngle(angle);

    angle = TwoPi - angle;

    return angle;
}

public static float FlipAngleY(float angle)
{
    angle = NormalizeAngle(angle);

    if (angle < Pi)
    {
        angle = Pi - angle;
    }
    else
    {
        angle = TwoPi - angle + Pi;
    }
    return angle;
}

/// <summary>
/// Keeps angle between 0 - Two Pi
/// </summary>
public static float NormalizeAngle(float angle)
{
    if (angle < 0)
    {
        int backRevolutions = (int)(-angle / TwoPi);
        return angle + TwoPi * (backRevolutions + 1);
    }
    else
    {
        return angle % TwoPi;
    }
}
守护在此方 2024-08-10 06:33:37

啊,看来问题毕竟来自负数,我确保它们是正数,现在旋转效果很好,我什至不需要重新计算角度......
感谢大家,由于每个回复的一些内容,我最终弄清楚了。

Aah, seems the problem came from negative numbers after all, I ensured them being positive and now the rotation does fine, I don't even need to recalculate angle...
Thanks to everyone, I ended up figuring out due to bits of every response.

情感失落者 2024-08-10 06:33:37

逆时针翻转到顺时针(右侧 270 -> 右侧 90)

角度 - 360

--

垂直翻转(顶部 180 -> 顶部 0/360)

Math. Normalize(angle - 180)

--

两者:

float Flipped_vertical = angle - 360
float Flipped_vertical_and_horizo​​ntal = Math.Normalize(flipped_vertical- 180)

to flip counter clockwise to clockwise (270 on right -> 90 on right)

angle - 360

--

to flip vertical (180 on top -> 0/360 on top)

Math.Normalize(angle - 180)

--

both:

float flipped_vertical = angle - 360
float flipped_vertical_and_horizontal = Math.Normalize(flipped_vertical- 180)

你与清晨阳光 2024-08-10 06:33:37

只是 360-angle 会水平翻转你的角度,但不会垂直翻转

just 360-angle will flip your angle horizontaly but not verticaly

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文