简体   繁体   English

具有OpenGl和C ++的2D平台游戏相机

[英]2D platform game camera with OpenGl and C++

I am trying to make a 2D platform game using OpenGl and glut with C++. 我正在尝试使用OpenGl制作2D平台游戏,并使用C ++制作过剩。 You can move your player around with the left and right arrow keys and jump with space. 您可以使用左右箭头键移动播放器并使用空格跳转。 I have all the platforms loaded into the game through a text file and printed to the screen. 我通过文本文件将所有平台加载到游戏中并打印到屏幕上。 This all works really good. 一切都很好。 The problem I am having is with the camera. 我遇到的问题是相机。 When the right arrow key is pressed I have the players x position to increase. 当按下右箭头键时,我让玩家x的位置增加。 The problem is that when this happens I can not get the actual camera to move as well. 问题在于,当发生这种情况时,我无法让实际的相机移动。 This makes me think that instead of moving the player, I should use glTranslatef to translate all the platforms to the left. 这让我觉得不应该移动播放器,我应该使用glTranslatef将所有平台翻译到左边。 This seems a bit odd to me and I am just wondering if this is how it should be done. 这对我来说有点奇怪,我只是想知道这是不是应该怎么做。 So I guess the final question is, should I translate the entire scene or move the player? 所以我想最后一个问题是,我应该翻译整个场景还是移动播放器?

Move the camera to follow the player by glTranslate ing in the opposite direction as the player. 移动相机以跟随播放器通过glTranslate与播放器相反的方向移动。

You should think of the camera like an in game object similar to the player and other movable items and the level as a static object with a fixed position. 您应该将摄像机视为类似于播放器和其他可移动项目的游戏对象,并将该级别视为具有固定位置的静态对象。 This makes placing in-game items and other things much easier. 这使得放置游戏内物品和其他东西变得更加容易。

actually when you "move the camera" in OpenGL, since there is actually no camera, what is done internally, is exactly that, moving everything on the scene in the oposite direction. 实际上当你在OpenGL中“移动相机”时,由于实际上没有相机,内部完成的工作就是这样,在相反的方向移动场景中的所有内容。

As for the solution, if you're using glut, you can use 至于解决方案,如果你使用过剩,你可以使用

gluLookAt(x,  y,  z,
          ex, ey, ez,
          0,  1,  0)

where (x, y, z) is the coordinate where you want the camera to be, (ex, ey, ez) is the direction vector that you want the camera to look into (in reference to (x, y, z) coordinates) and (0, 1, 0) is the up vector. 其中(x,y,z)是您想要相机的坐标,(例如,ey,ez)是您希望相机查看的方向向量(参考(x,y,z)坐标) )和(0,1,0)是向上矢量。 This function does all the matrix transformations necessary. 此函数执行所有必要的矩阵变换。 More info here 更多信息在这里

If you're not using glut, but only raw OpenGL, the same link also explains the equivalent opengl calls you have to use to achieve exactly the same effect as gluLookAt 如果您没有使用过剩,但只使用原始OpenGL,同样的链接也解释了您必须使用的等效opengl调用来实现与gluLookAt完全相同的效果

So when you want to move the camera (only right and left probably, since it's a platform game) you only have to change the x value 所以当你想移动相机时(可能只是左右两边,因为它是一个平台游戏)你只需要改变x值

I got this working using the following.. 我使用以下工作了...

gl.PushMatrix()
gl.Translatef((1280/2)-float32((player.Body.Position().X)), 0, 0.0)

//Rest of drawing Code Code

gl.PopMatrix()

This is using OpenGL + Chipmunk Physics (in GoLang but should apply as these are just c-bindings). 这是使用OpenGL + Chipmunk Physics(在GoLang,但应该适用,因为这些只是c绑定)。 1280 in this case is screen Width. 在这种情况下,1280是屏幕宽度。

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

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