相机运动。如何移动相机?

发布于 2024-10-12 22:36:28 字数 212 浏览 3 评论 0原文

我目前设置了一个开放式 gl 渲染器,它显示一个 2d 正方形和另一个 2d 形状。通过使用按键,用户可以上下左右移动方块。这是通过根据用户是否按上、下、左、右而改变的值来平移方块来完成的,例如,如果用户按右,则意味着 gl.glTranslatef(rightdisplacement, 0, 0); 的平移;运动工作正常,但我不知道如何让相机在正方形继续移动时跟随它。我想移动正方形并使相机朝同一方向移动。

I currently have an open gl renderer set up which displays a 2d square and another 2d shape. By using the keys the user is able to move the square up down left and right. This is done through translating the square based on values altered by whether the user presses up down left right eg if the user presses right it would mean a translation of gl.glTranslatef(rightdisplacement, 0, 0); etc. The movement works fine but I cant figure out how to get the camera to follow the square as it continues moving. I would like to move the square and have the camera move in the same direction.

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

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

发布评论

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

评论(1

深白境迁sunset 2024-10-19 22:36:28

如果您正在进行 2D 渲染,您可能会调用 glOrtho 某处定义坐标系。完全是猜测,但您的代码可能类似于:

glOrtho( 0, screenWidth, 0, screenHeight, -1, 1 );

这是您定义相机位置的位置。创建相机位置 x 和 y 位置变量,然后调用

glOrtho( camX - screenWidth/2, camX + screenWidth/2, camY - screenHeight/2, camY + screenHeight/2, -1, 1 );

确保每帧都调用此方法,因为相机位置显然会发生变化。您的渲染代码可能类似于

// clear framebuffer

glMatrixMode( GLES10.GL_PROJECTION );                
glLoadIdentity();        
glOrtho( camX - screenWidth/2, camX + screenWidth/2, camY - screenHeight/2, camY + screenHeight/2, -1, 1 );

glMatrixMode( GLES10.GL_MODELVIEW );                 
glLoadIdentity();

// draw your stuff                                 

If you're doing 2D rendering you are probably making a call to glOrtho somewhere to define your coordinate system. Total speculation, but your code might look something like:

glOrtho( 0, screenWidth, 0, screenHeight, -1, 1 );

This is where you define your camera position. Create camera position x and y position variables and instead call

glOrtho( camX - screenWidth/2, camX + screenWidth/2, camY - screenHeight/2, camY + screenHeight/2, -1, 1 );

Make sure that this is called every frame as the camera position will obviously change. Your render code might look something like

// clear framebuffer

glMatrixMode( GLES10.GL_PROJECTION );                
glLoadIdentity();        
glOrtho( camX - screenWidth/2, camX + screenWidth/2, camY - screenHeight/2, camY + screenHeight/2, -1, 1 );

glMatrixMode( GLES10.GL_MODELVIEW );                 
glLoadIdentity();

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