在Godot中的碰撞墙

huangapple go评论120阅读模式
英文:

Collision walls in Godot

问题

我正在使用Godot Engine创建游戏,但我无法弄清楚如何创建一堵墙来防止玩家走出屏幕。

玩家可以走出屏幕窗口。如何防止这种情况发生?

我尝试使用CollisionShape2D节点,但我不认为我知道如何正确使用它,使其按照我想要的方式工作。

英文:

I'm creating a game using Godot Engine and I can't figure out how to create a wall to prevent a player from walking out of the screen.

The player is able to walk out of the screen window. How do I prevent it from happening?

I tried to use the CollisionShape2D node but I don't think I know how to use it properly in a way that it works like I want it to.

答案1

得分: 0

CollisionShape2DCollisionPolygon2D 本身不执行任何操作。它们的目的是为父级 CollisionObject2D 提供形状。

有两种类型的 CollisionObject2DArea2DPhysicsBody2D

Area2D 会检测其他物体或区域,但不会阻止它们移动,这通常不是您想要的。

有三种类型的 PhysicsBody2D

  • CharacterBody2D:用于通过脚本移动,通常用于制作角色控制器。
  • RigidBody2D:由物理引擎移动,通常用于创建可以被其他物体推动的物体。
  • StaticBody2D:要么不移动(或具有预定义的运动)。这是您想要的类型。

因此,您可以设置一个带有子级 CollisionShape2DCollisionPolygon2DStaticBody2D

此外,我还要指出,物理体与物理体相互作用。例如,如果您将角色创建为 Sprite2D,因为 Sprite2D 只关心图形而不具备碰撞功能,它不会被 StaticBody2D 阻止。

因此,通常将角色创建为可以发生碰撞的物体(通常是 CharacterBody2D,有时是 RigidBody2D),当然,它会有一个子级 CollisionShape2DCollisionPolygon2D,还可以将 Sprite2D 或其他所需内容作为子级。

通过将 Sprite2D 和其他 Node2D 作为物理体的子级,它们将随物理体一起移动。

如果您尚未决定使用 CharacterBody2D 还是 RigidBody2D,我会在其他地方更详细地介绍要使用哪种类型的节点。 一个很好的概括是:如果其他东西可以推动它,请使用 RigidBody2D。通过访问链接,您可以了解在特定情况下应该使用哪种类型的细微差别和建议。

对于 CollisionShape2D,要将其 shape 属性设置为 Shape2D

关于创建游戏边界,建议使用 WorldBoundaryShape2D 来创建每个边界。但是,足够厚的 RectangleShape2D 也可以工作。

或者,虽然不建议,您可以使用覆盖游戏区域的 CollisionPolygon2D,并将其 build_mode 设置为 BUILD_SEGMENTS,这将导致一个空心多边形,因此其他物体可以在其中。

英文:

Note: What I explain here also holds for the 3D counterpart of these nodes.


CollisionShape2D and CollisionPolygon2D do nothing on their own. Their purpose is to provide a shape for a parent CollisionObject2D.


There are two kinds of CollisionObject2Ds: Area2D and PhysicsBody2D.

The Area2D will detect other bodies or areas, but won't stop them from moving, which is not what you want.

There are three kinds of PhysicsBody2D:

  • CharacterBody2D: which is intended to be moved via script, usually to make character controllers.
  • RigidBody2D: which is moved by the physics engine, usually to make bodies that can be pushed around by other bodies.
  • StaticBody2D: which either do not move (or have a predefined motion). This is the one you want.

Thus, you would setup a StaticBody2D with a child CollisionShape2D or CollisionPolygon2D.


Now, I'm also going to point out that physic bodies interact with physic bodies. If you, for example, have created your character as an Sprite2D, since the Sprite2D only cares about graphics and does not have collisions it will not be stopped by the StaticBody2D.

So you usually make your character as a body that can collide (usually a CharacterBody2D, sometimes a RigidBody2D), and it would, of course, have a child CollisionShape2D or CollisionPolygon2D, and also as child it can have your Sprite2D or whatever else you need.

By making the Sprite2D and other Node2Ds children of the physics body, they will move with it.


If you haven't decided between CharacterBody2D and RigidBody2D, I go into more length on which kind of node to use for what elsewhere. A very good abstract is the following: If something else can push it, use a RigidBody2D. following the link you get the nuance and what to use for specific cases.


For the CollisionShape2D to set its shape property to a Shape2D.

About making the boundary for the game, the recommendation is to use WorldBoundaryShape2D for each edge. However, thick enough RectangleShape2D would also work.

Alternatively, although not recommended, you could use a CollisionPolygon2D covering the play area, and set its build_mode to BUILD_SEGMENTS, which results in a hollow polygon, so other bodies can be inside of it.

huangapple
  • 本文由 发表于 2023年7月28日 04:56:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定