简体   繁体   English

2D游戏相机

[英]Camera for 2d game

I'm currently making an awesome (in my mind) zombie game and I need to know a good way to make a camera. 我目前正在制作一款很棒的(在我看来)僵尸游戏,我需要知道一种制作相机的好方法。 I am using the Slick2d library along with MarteEngine for java. 我正在将Slick2d库和MarteEngine用于Java。

I'm kinda new to java and jumped straight into a library before really getting deep into swing and such so this is probably a lack of graphics knowledge. 我是Java的新手,在真正深入学习之前就直接进入了一个库,所以这可能是缺乏图形知识的原因。 I read on a tutorial that you can't actually move the window of the game on the map so instead you need to move the map and objects to make it seem like the camera is moving. 我在一个教程中读到,您实际上无法在地图上移动游戏窗口,因此您需要移动地图和对象以使其看起来像照相机在移动。

If I was to do it this way it seems like it would be very resource intensive. 如果我以这种方式进行操作,这似乎将非常耗费资源。 I would have to loop through every zombie, survivor, object, hitbox, etc to move their coordinates. 我将不得不遍历每个僵尸,幸存者,物体,命中盒等,以移动其坐标。 I've already tried this method once and things weren't really moving the same speed. 我已经尝试过这种方法一次,但事情并没有真正以相同的速度移动。 That may have been due to me handling it with different update speeds. 那可能是由于我以不同的更新速度来处理它。

I've seen a few things on using graphics.translate but I don't really understand it. 我已经看到了一些有关使用graphics.translate的信息,但我不太了解。 So.. any suggestions to making a camera move would be awesome! 所以..关于相机移动的任何建议都很棒! Thanks in advance. 提前致谢。

You can definitely move the camera. 您绝对可以移动相机。 See my answer to this question explaining how to move the camera, render the world relative to said camera, and some other useful tips on how to do this and how to decide what to draw on the screen as the camera moves. 我回答这个问题解释如何移动摄像头,摄像头说,世界相对的渲染,以及如何做到这一点其他一些有用的技巧,以及如何决定什么屏幕的摄像机移动上绘制。

While it's totally possible to use translate(x, y) , that alone doesn't solve clipping issues (how to know what to draw on the screen), and it's not a catch all. 尽管完全可以使用translate(x, y) ,但这本身并不能解决剪切问题(如何知道在屏幕上绘制的内容),也不是全部。 Also, when you think about it, there's really not much of a difference between translating the surface and moving the camera. 此外,考虑一下时,平移表面和移动相机之间实际上并没有太大区别。 It's all relative, and so long as the pixels are moving in the right direction, it doesn't really matter if you're "moving the camera relative to the world" or "moving the world relative to the camera" - they're essentially the same operation. 这都是相对的,只要像素朝着正确的方向移动,是“相对于世界移动相机”还是“相对于相机移动世界”都没关系-它们是基本相同的操作。

As far as Swing is concerned, be glad you didn't start there . 就Swing而言, 很高兴您没有从那里开始 Swing is designed for desktop applications with windows, menus, buttons, complex event systems, and the like. Swing专为具有窗口,菜单,按钮,复杂事件系统等的桌面应用程序而设计。 For a number of reasons Swing is terrible for games, not the least of which is the way the rendering pipeline works in Swing; 出于多种原因,Swing对于游戏来说很糟糕,其中最重要的就是渲染管道在Swing中的工作方式。 it's fine for desktop apps, but becomes a pit of despair for games and most types of rendering that need to be real-time, and graphics intensive. 这对于桌面应用程序来说很好,但是对于游戏和大多数需要实时且图形密集型的渲染类型,这却令人绝望。 This is all okay because Swing wasn't built to solve the problems of game developers anyway. 没关系,因为Swing并不是为解决游戏开发人员的问题而构建的。

You're probably looking for, yes, the translate(x, y) method of the Graphics class. 是的,您可能正在寻找Graphics类的translate(x, y)方法。 This method is available from both the standard Java Swing/AWT Graphics class as well as the Slick2D version, so keep in mind that this is not a Slick2D-specific feature. 标准Java Swing / AWT Graphics类和Slick2D版本都可以使用此方法,因此请记住,这不是Slick2D特定的功能。

What translate does is move the origin point of all future drawing operations. 翻译所做的是移动所有将来绘图操作的原点。 What does this mean? 这是什么意思? Well, keep in mind that the origin point is usually at (0, 0). 好吧,请记住,原点通常在(0,0)。 As such, drawing a point at position (40, 50) will draw to the screen position (40, 50). 这样,在位置(40,50)绘制一个点将绘制到屏幕位置(40,50)。 However, translate() moves this reference point, which in turn affects all other drawing operations. 但是, translate()会移动该参考点,进而影响所有其他绘图操作。

For example, doing translate(50, 60) would move the origin point 50 pixels to the right on the x axis and 60 pixels down on the y axis (since in computer programmer, larger y values are lower on the screen). 例如,执行translate(50, 60)会将原点在x轴上向右移动50个像素,在y轴上向下移动60个像素(由于在计算机编程器中,较大的y值在屏幕上较低)。 Then, drawing a point at (40, 50) will actually draw to the screen at (40 + 50, 50 + 60), or (90, 110). 然后,在(40,50)处绘制一个点实际上将在(40 + 50,50 + 60)或(90,110)处绘制到屏幕上。 This is the power of using translate() . 这就是使用translate()

It's usually a good idea to move your origin point back to (0, 0) once you're done drawing, or things could get screwed up. 完成绘制后,将原点移回(0,0)通常是个好主意,否则可能会搞砸。 However, doing translate(0, 0) will not accomplish this task, and will in fact do absolutely nothing at all. 但是,执行translate(0, 0)不会完成此任务,并且实际上根本不做任何事情。 This is because the translate method moves the origin to a relative point. 这是因为平移方法将原点移动到相对点。 Instead, just make sure that for every call translate(x, y) , you have another call translate(-x, -y) . 相反,只需确保对于每个调用translate(x, y) ,您都有另一个调用translate(-x, -y) This will keep the origin point from getting out of hand. 这样可以避免原点失控。

If you want to use this method to have the camera follow one of your entities, it is important to notice that you want to translate the origin using the negative of that entity's position. 如果要使用此方法使摄像机跟随您的一个实体,请务必注意要使用该实体位置的负值平移原点。 For instance, to follow a player with a position vector 'pos': 例如,要为玩家提供位置矢量“ pos”:

g.translate(-player.pos.x, -player.pos.y);

// Render everything

g.translate(player.pos.x, player.pos.y);

This is because you want the background to move to the left when the player moves to the right, otherwise you'll have everything following the player. 这是因为当播放器向右移动时,您希望背景向左移动,否则,您将跟随播放器移动所有内容。

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

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