Javascript required
Skip to content Skip to sidebar Skip to footer

How Can Texture Add to the Verisimilitude of a Picture? Chapter 2 Quizlit

Going forrad in this series of Godot tutorials, nosotros volition often need to know when 2 objects come up into contact with each other. In game development, this is known as collision detection. In this tutorial, nosotros will learn how collisions work in Godot.

When a collision is detected, you lot typically want something to happen. For example, when the role player sprite comes in contact with a wall, we want it to stop. Instead, if it moves over an object, we want it to be able to pick information technology up. This is known every bit standoff response.

Godot offers 4 kinds of collision objects to provide both collision detection and response. Let's try to understand how each one works and when to use them with practical examples.

KinematicBody2D

KinematicBody2D nodes find collisions with other bodies, just don't automatically move according to physical properties such as gravity or friction. They must be moved manually by the user through scripts.

We have already seen an example in the Player movement tutorial, where we used KinematicBody2D to move the actor.

When moving a kinematic torso, y'all should not set its position directly. Instead, y'all must utilize the move_and_collide() or move_and_slide() methods. If after the movement the body has collided, as a response y'all may want your body to bounce, to slide along a wall or to modify the properties of the other torso. The way you handle the collision response depends on the method used to motion KinematicBody2D:

  • When using move_and_collide(), the function returns a KinematicCollision2D object, which contains data about the standoff and the colliding body. Any collision response must be coded manually using this information (KinematicCollision2D contains information most the colliding object, the remaining motion, the collision position, etc.)
  • When using move_and_slide(), if the body collides with another one, it will slide along the other body rather than cease immediately. You don't have to write any code to go this behaviour.

As mentioned above, nosotros already take a KinematicBody2D object in our project, the Histrion node. Then, let's open the SimpleRPG project to complete its configuration.

If you lot remember, this node has a warning that we need to correct.

This alarm is due to the lack of a collision shape. Collision shapes are used to define the body collision bounds and to discover contact with other objects.

The most common manner to assign a shape to a collision object is past calculation a CollisionShape2D or CollisionPolygon2D node as its child. These nodes let you to draw the shape direct in the editor workspace.

Add together a CollisionShape2D child node to Actor.

In the Inspector, set Shape to New RectangleShape2D. Now, the sprite of the actor volition be overlaid with a rectangle that we can manipulate to set the size of the collision shape. Resize it to exist the aforementioned size of our sprite.

As we learn the other types of colliders, we'll see how they interact with our player's KinematicBody2D.

StaticBody2D

StaticBody2D are bodies that take parts in collision detection but, equally their name implies, exercise not motion in response to collisions. They are mostly used for objects that are office of the environs like wall, tree, rocks, and and then on.

To test StaticBody2D, first of all download the following prototype and import information technology in Godot (recollect to turn off Filter in the Import console).

Download "SimpleRPG Map Tiles" map_tiles.png – five KB

Add a StaticBody2D node as a kid of Root and rename information technology Rock. So, add a Sprite node and a CollisionShape2D to Rock.

Select Rock'due south Sprite and drag the map_tiles.png file to the Texture holding in the Inspector. The sprite will appear in the upper left corner of the game screen.

We do not want to use the whole image, but merely a small region of it. To exercise this, in the Inspector detect the Region section and enable it.

At present that Region is enabled, in the lower part of the editor you will see that in that location is a new panel called Texture Region. Open up it.

There are diverse ways to select the region that interests united states of america. In our case, all the elements are arranged in a sixteen×16 pixels grid, so the simplest solution is to ready Snap Manner to Grid Snap, set the two Step values to 16px and and then select the square that contains the rock.

Now select Rock's CollisionShape2D, ready the Shape belongings to New CircleShape2D and, in the editor, resize the circle to cover just the rock.

Finally, select the Rock node and set its Position, in the Transform section, to (220, xc).

Now, if you lot launch the game and movement the histrion, you will see that it will non be able to move through the stone!

If you lot notice, there is a small problem: when the histrion is near the rock, he is drawn nether the grass. You tin can solve this problem by setting the role player's Z Index to ane (the stone has Z Index 0 and volition therefore be drawn behind). To practice this, select Thespian and in the Inspector find the Z Index department under Node2D and fix Z Alphabetize to 1.

RigidBody2D

RigidBody2D is the node that implements imitation 2D physics. Y'all practice not command information technology directly. Instead, you apply forces to information technology (gravity, impulses, etc.) and Godot's physics engine calculates the resulting motility, including collisions with other bodies and collision responses, such as bouncing, rotating, etc. Every bit in KinematicBody2D and in StaticBody2D, you must assign ane or more shape to RigidBody2D.

To try information technology, add together a RigidBody2D node as a child of Root and rename it MovingRock. Then, add together a Sprite node and a CollisionShape2D to MovingRock.

Exactly with the same procedure we saw for StaticBody2D, set the Sprite texture and CollisionShape2D shape, then motion MovingRock to Position (210, 30).

If you lot play the game now, you will run across that MovingRock will start to fall down, striking Rock and begin to rotate as it continues its fall. You lot have just seen Godot's physics engine in activity for the commencement time!

You can modify RigidBody2D behavior setting its properties in the Inspector or through scripts. The body's behavior is also afflicted by the world's properties, every bit set in Project Settings → Physics.

Our game is a pinnacle-down RPG, so we don't need gravity. With MovingRock selected, set the Gravity Scale value to 0 in the Inspector. As well in the Inspector, set Linear → Damp to 10 and Athwart → Clammy to 5 to add together friction.

Run once again the game. At present MovingRock doesn't fall. Yous can utilize your player to push and rotate the rock!

Note that so far yous take not entered any line of code!

Area2D

The last type of Godot's collision object is Area2D. Information technology is an surface area that detects when other collision objects overlap, enter or go out from it. An Area2D node can also be used to override physics properties, such as gravity or damping, in a defined expanse. They also receive mouse and touchscreen input.

Add an Area2D node every bit a child of Root and rename information technology Flowers. And then, add together a Sprite node and a CollisionShape2D to Flowers. Fix the Sprite texture to use the bloom image from map_tiles.png, and add to CollisionShape2D a rectangle shape covering the flowers (by now you should have learned how to practise it!). Lastly, move Flowers to Position (140, 100).

Attach a new script to Flowers. We want to connect the enter signal of Area2D to this script to remove flowers from the game when the player move over them, as if the player had picked them up.

Select Blossom and go to the Node panel. Here you volition run into a list of all the Area2D signals you tin connect. Choose body_entered and press Connect…

In the window that opens, choose Flowers as the node to connect.

Printing the Connect button. The Flowers.gd script volition open and the method that volition handle the bespeak volition be added automatically. Write this code for the _on_Flowers_body_entered() method:

          func _on_Flowers_body_entered(body): 	if torso.proper name == "Player": 		get_tree().queue_delete(self)        

This method checks if the body entered in the expanse is Player. If true, information technology will delete itself from the current nodes tree.

Run the game and effort to move over the flowers.

Conclusions

In this tutorial we learned the foundations of the Godot's physics engine, in particular all types of collision objects and their features. In the adjacent tutorial, we will utilise this noesis to create game maps with terrain that responds to collisions with the histrion.

Did you relish this article? And so consider buying me a java! Your support will help me encompass site expenses and pay writers to create new blog content.

redmondoftelith.blogspot.com

Source: https://www.davidepesce.com/2019/10/02/godot-tutorial-6-physics-and-collisions/