简体   繁体   English

OpenGL:围绕3D空间中的点旋转

[英]OpenGL: Rotate around a point in 3D space

I've been doing some hunting through similar questions and haven't been able to find exactly what I'm looking for. 我一直在通过类似的问题做一些寻找,但一直无法找到我想要的东西。

I'm attempting to rotate an object around (0, 0, 0), regardless of its x, y, and z offset. 我试图围绕(0、0、0)旋转对象,而不管其x,y和z偏移量如何。

To give an example of this, consider the following relevant situation: 举一个例子,考虑以下相关情况:

I have a moon that is rotating around a planet centred at (0, 0, 0). 我有一个月亮绕着以(0,0,0)为中心的行星旋转。 The moon has an initial offset of (1, 1, 1). 月亮的初始偏移为(1、1、1)。 I want to rotate the moon, and other moons, around the planet each frame. 我想在每个框架上绕行星旋转月亮和其他卫星。

To accomplish this, I've started off with the following code: 为此,我从以下代码开始:

    for (GMoon moon : moonList)
    {
        Point offset = moon.getOffset();

        float newRot = moon.getRotation() + moon.getRotSpeed();
        moon.setRotation(newRot);

        // Return to the identity matrix
        gl.glLoadIdentity();

        // Translate and rotate the planet
        gl.glTranslatef(0.0f, 0.0f, OBJECT_DISTANCE);
        gl.glRotatef(newRot, 0, 0, 1);
        gl.glTranslatef(offset.getX(), offset.getY(), offset.getZ());

        // Draw the moon
        moon.draw(gl);
    }

(OBJECT_DISTANCE is equal to -10 for viewing purposes). (出于查看目的,OBJECT_DISTANCE等于-10)。

Instead of rotating in front of the planet, it simply rotates around it. 与其在行星前方旋转,不如在行星周围旋转。

Am I doing something obviously wrong? 我做错了什么吗?

Thank you very much for your time. 非常感谢您的宝贵时间。


EDIT: I'm under the assumption that I need to rotate each axis by a certain component based on the offset of the moon. 编辑:我假设我需要根据月球的偏移量将每个轴旋转某个组件。 I'm not quite sure how such a component should be calculated. 我不太确定应该如何计算这样的分量。

I am assuming you would like to rotate the moon around the planet as well as around it's center point. 我假设您想绕地球以及绕其中心点旋转月球。 To reach this: 要达到此目的:

And there is also a similar question to this I think: Rotating an object around a fixed point in opengl 我认为还有一个类似的问题: 在opengl中围绕固定点旋转对象

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

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