在 OpenGL 中围绕一个点旋转一条线
Rotating a line around a point in OpenGL
我正在尝试围绕不是 (0,0) 的点旋转一条线。
对于这个程序,我尽量不使用 glRotatef()
并尝试使用三角函数。然而,线的长度总是会改变。
有什么建议吗?
这是我的代码:
public class World implements GLEventListener, KeyListener
{
double xOne = 0.1;
double yOne = 0.1;
double xTwo = 0;
double yTwo = 0.01;
double i = 220.987;
public World()
{
}
public void init(GLAutoDrawable gld)
{
Animator theAnimator = new Animator(gld);
theAnimator.start();
}
public void display(GLAutoDrawable gld)
{
GL gl = gld.getGL();
gl.glEnable(gl.GL_BLEND);
gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glBegin(gl.GL_LINES);
gl.glColor3d(1,0,0);
gl.glVertex2d(xOne ,yOne);
gl.glColor3d(0,0,1);
gl.glVertex2d(xTwo, yTwo);
gl.glEnd();
gl.glFlush();
//UPDATE
}
public void reshape(GLAutoDrawable gLAutoDrawable, int _int, int _int2, int _int3, int _int4)
{
}
public void displayChanged(GLAutoDrawable gLAutoDrawable, boolean _boolean, boolean _boolean2)
{
}
public void keyTyped(KeyEvent ke)
{
}
public void keyPressed(KeyEvent ke)
{
if(ke.getKeyCode() == KeyEvent.VK_SPACE)
{
xTwo = (Math.cos(Math.toRadians(i))+xOne);
yTwo = (Math.sin(Math.toRadians(i))+yOne);
i++;
}
}
public void keyReleased(KeyEvent ke)
{
}
}
对了,(xOne, yOne)
是驻点
通常你会通过平移到 (0,0),围绕 (0,0) 旋转,然后平移回来来做到这一点。所以这是一个 3 步过程:
- 从你的分数中减去 (xOne,yOne)。
- 使用三角函数绕 (0,0) 旋转。 (我假设您已经知道如何执行此操作,或者您可以查找它。)
- 再次将 (xOne,yOne) 加回您的积分。
就您而言,我认为您的代码几乎已经正确了。你缺少的是乘以线的长度。使用距离公式计算 (xOne,yOne) 和 (xTwo,yTwo) 之间的距离,然后将该距离乘以正弦值和余弦值,然后再添加 xOne 和 yOne。
了解转换是在 OpenGL 中有效工作的必要条件。
转换处理如何以两种不同的方式表示一个系统。因此,位于 (3,4) 的点可以转换为原点位于 (3,4) 的 "alternate universe" 中位于 (0,0) 的点。
绕一个点旋转很简单,基本上就是公式
(x', y') = (x-a, y-a) (where x's is the "new x" and x is the "old" x)
同样,这也很容易逆转
(x, y) = (x'+a, y'+a)
旋转变换看起来像
(x', y') = ((x*cos(a) + y*sin(a)), (-x*sin(a)+y*cos(a)))
它为您提供了一个新的 x' 和 y',用于围绕原点旋转弧度的点。
因此,要在 OpenGL 中旋转,首先要将所有点变换到新的原点,即旋转原点。然后围绕原点旋转。然后将所有点从新原点移回旧原点。
就像詹姆斯上面说的,我只是用了更多的词。
我正在尝试围绕不是 (0,0) 的点旋转一条线。
对于这个程序,我尽量不使用 glRotatef()
并尝试使用三角函数。然而,线的长度总是会改变。
有什么建议吗?
这是我的代码:
public class World implements GLEventListener, KeyListener
{
double xOne = 0.1;
double yOne = 0.1;
double xTwo = 0;
double yTwo = 0.01;
double i = 220.987;
public World()
{
}
public void init(GLAutoDrawable gld)
{
Animator theAnimator = new Animator(gld);
theAnimator.start();
}
public void display(GLAutoDrawable gld)
{
GL gl = gld.getGL();
gl.glEnable(gl.GL_BLEND);
gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glBegin(gl.GL_LINES);
gl.glColor3d(1,0,0);
gl.glVertex2d(xOne ,yOne);
gl.glColor3d(0,0,1);
gl.glVertex2d(xTwo, yTwo);
gl.glEnd();
gl.glFlush();
//UPDATE
}
public void reshape(GLAutoDrawable gLAutoDrawable, int _int, int _int2, int _int3, int _int4)
{
}
public void displayChanged(GLAutoDrawable gLAutoDrawable, boolean _boolean, boolean _boolean2)
{
}
public void keyTyped(KeyEvent ke)
{
}
public void keyPressed(KeyEvent ke)
{
if(ke.getKeyCode() == KeyEvent.VK_SPACE)
{
xTwo = (Math.cos(Math.toRadians(i))+xOne);
yTwo = (Math.sin(Math.toRadians(i))+yOne);
i++;
}
}
public void keyReleased(KeyEvent ke)
{
}
}
对了,(xOne, yOne)
是驻点
通常你会通过平移到 (0,0),围绕 (0,0) 旋转,然后平移回来来做到这一点。所以这是一个 3 步过程:
- 从你的分数中减去 (xOne,yOne)。
- 使用三角函数绕 (0,0) 旋转。 (我假设您已经知道如何执行此操作,或者您可以查找它。)
- 再次将 (xOne,yOne) 加回您的积分。
就您而言,我认为您的代码几乎已经正确了。你缺少的是乘以线的长度。使用距离公式计算 (xOne,yOne) 和 (xTwo,yTwo) 之间的距离,然后将该距离乘以正弦值和余弦值,然后再添加 xOne 和 yOne。
了解转换是在 OpenGL 中有效工作的必要条件。
转换处理如何以两种不同的方式表示一个系统。因此,位于 (3,4) 的点可以转换为原点位于 (3,4) 的 "alternate universe" 中位于 (0,0) 的点。
绕一个点旋转很简单,基本上就是公式
(x', y') = (x-a, y-a) (where x's is the "new x" and x is the "old" x)
同样,这也很容易逆转
(x, y) = (x'+a, y'+a)
旋转变换看起来像
(x', y') = ((x*cos(a) + y*sin(a)), (-x*sin(a)+y*cos(a)))
它为您提供了一个新的 x' 和 y',用于围绕原点旋转弧度的点。
因此,要在 OpenGL 中旋转,首先要将所有点变换到新的原点,即旋转原点。然后围绕原点旋转。然后将所有点从新原点移回旧原点。
就像詹姆斯上面说的,我只是用了更多的词。