简体   繁体   English

LibGDX使世界动起来,而玩家却没有

[英]LibGDX make the world move and the player not

I'm working on a simple game with libGDX and want to make the main character to be fixed in the center of the screen and the world move when I press a button. 我正在使用libGDX进行简单的游戏,希望将主角固定在屏幕中央,并在按下按钮时移动世界。 I was wondering how to do... I was thinking to add a physicsBody to the world that contains other bodies and apply impulses to it when the button is pressed, is this possible in libGDX? 我想知道该怎么做...我正在考虑向包含其他物体的世界添加physicsBody并在按下按钮时对其施加脉冲,这在libGDX中可能吗? And if it is, can i apply other impulses or forces to the bodies contained in the world's physicsBody? 如果是的话,我可以向世界PhysicsBody中包含的身体施加其他冲力或力吗? I think this way would be the best for me if it is possible, because i have to work a lot with physics, but if you have other ideas tell me please 我认为如果可能的话,这种方式对我来说是最好的,因为我必须从事很多物理工作,但是如果您有其他想法请告诉我

There's no need to think about applying forces to all the non-character objects, that's just going to get messy very quickly. 无需考虑对所有非字符对象施加力,这只会很快变得混乱。

The simple solution is to move your camera so that it always looks at your character. 简单的解决方案是移动相机,使其始终对准您的角色。 So your game loop may look something like:- 因此,您的游戏循环可能类似于:

  1. Process input 流程输入
  2. Update physics, character and other entity positions. 更新物理,角色和其他实体位置。
  3. Move camera to point at character's new position. 移动相机以指向角色的新位置。
  4. Render 给予

This way, you can update your game world without having to think about the camera at all. 这样,您完全不必考虑摄像头即可更新游戏世界。 Then, when it comes to rendering, you can position your camera and render your graphics without needing to know anything about the game physics. 然后,在进行渲染时,您可以定位相机并渲染图形,而无需了解游戏物理原理。 It keeps the physics and rendering relatively independent, and makes it much easier to change things in the future. 它使物理和渲染相对独立,并使将来更改事物变得更加容易。

For example, you may later decide that you want the camera to follow your character for the most part, but then follow a baddy whilst it is their turn. 例如,您以后可以决定希望摄像机在大多数情况下跟随角色,但是在轮到他们时跟随坏人。 This is now easy to do, you just specify the character / entity to look at in your game logic, and then position the camera to look at whatever target that is, before you render. 现在,这很容易做到,您只需在游戏逻辑中指定要查看的角色/实体,然后在渲染之前将相机放置在目标物体上即可。

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

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