java 2d游戏的相机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10560922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Camera for 2d game
提问by Paha
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 for 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. 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.
我对java有点陌生,在真正深入swing之前直接跳进了一个库,所以这可能是缺乏图形知识。我在教程中读到您实际上无法在地图上移动游戏窗口,因此您需要移动地图和对象以使其看起来像相机在移动。
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.
如果我要这样做,它似乎会非常耗费资源。我将不得不遍历每个僵尸、幸存者、对象、hitbox 等来移动它们的坐标。我已经尝试过这种方法一次,但事情并没有真正以相同的速度移动。这可能是由于我以不同的更新速度处理它。
I've seen a few things on using graphics.translate but I don't really understand it. So.. any suggestions to making a camera move would be awesome! Thanks in advance.
我已经看到了一些关于使用 graphics.translate 的东西,但我并不真正理解它。所以..任何让相机移动的建议都会很棒!提前致谢。
回答by jefflunt
You can definitely move the camera. See my answer to this questionexplaining 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 whatto 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. 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.
虽然完全可以使用translate(x, y)
,但仅凭这一点并不能解决剪辑问题(如何知道在屏幕上绘制什么),而且也不能一概而论。此外,当您考虑它时,平移表面和移动相机之间确实没有太大区别。这都是相对的,只要像素朝正确的方向移动,“相对于世界移动相机”或“相对于相机移动世界”并不重要 - 它们是基本相同的操作。
As far as Swing is concerned, be glad you didn't start there. Swing is designed for desktop applications with windows, menus, buttons, complex event systems, and the like. For a number of reasons Swing is terrible for games, not the least of which is the way the rendering pipeline works in 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 而言,庆幸您没有从那里开始。Swing 是为带有窗口、菜单、按钮、复杂事件系统等的桌面应用程序设计的。出于多种原因,Swing 对游戏来说很糟糕,其中最重要的是 Swing 中渲染管道的工作方式;它适用于桌面应用程序,但对于游戏和大多数需要实时和图形密集型的渲染类型来说却是一个绝望的坑。这一切都很好,因为 Swing 无论如何都不是为了解决游戏开发者的问题而构建的。
回答by Alexis King
You're probably looking for, yes, the translate(x, y)
method of the Graphics
class. 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.
您可能正在寻找,是的,类的translate(x, y)
方法Graphics
。此方法可从标准 Java Swing/AWTGraphics
类以及 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). As such, drawing a point at position (40, 50) will draw to the screen position (40, 50). However, translate()
moves this reference point, which in turn affects all other drawing operations.
translate 所做的是移动所有未来绘图操作的原点。这是什么意思?好吧,请记住,原点通常在 (0, 0) 处。因此,在位置 (40, 50) 绘制一个点将绘制到屏幕位置 (40, 50)。但是,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). Then, drawing a point at (40, 50) will actually draw to the screen at (40 + 50, 50 + 60), or (90, 110). This is the power of using translate()
.
例如,这样做translate(50, 60)
会将原点在 x 轴上向右移动 50 个像素,在 y 轴上向下移动 60 个像素(因为在计算机程序员中,屏幕上的 y 值越大越低)。然后,在 (40, 50) 处绘制一个点实际上会在 (40 + 50, 50 + 60) 或 (90, 110) 处绘制到屏幕上。这就是使用的力量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. However, doing translate(0, 0)
will not accomplish this task, and will in fact do absolutely nothing at all. This is because the translate method moves the origin to a relativepoint. Instead, just make sure that for every call translate(x, y)
, you have another call translate(-x, -y)
. This will keep the origin point from getting out of hand.
完成绘制后将原点移回 (0, 0) 通常是个好主意,否则事情可能会搞砸。但是,做translate(0, 0)
并不能完成这项任务,实际上什么也做不了。这是因为 translate 方法将原点移动到一个相对点。相反,只需确保对于每个 call translate(x, y)
,您都有另一个 call translate(-x, -y)
。这将防止原点失控。
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.
这是因为当玩家向右移动时,您希望背景向左移动,否则所有内容都会跟随玩家。