I've been reading the CharacterBody2D tutorial on Godot: https://docs.godotengine.org/en/4.2/tutorials/physics/using_character_body_2d.html
It states:
What is a character body?
CharacterBody2D is for implementing bodies that are controlled via code. Character bodies detect collisions with other bodies when moving, but are not affected by engine physics properties, like gravity or friction. While this means that you have to write some code to create their behavior, it also means you have more precise control over how they move and react.
Furthermore:
move_and_collide
This method takes one required parameter: a Vector2 indicating the body's relative movement. Typically, this is your velocity vector multiplied by the frame timestep (delta). If the engine detects a collision anywhere along this vector, the body will immediately stop moving. If this happens, the method will return a KinematicCollision2D object.
move_and_collide is most useful when you just want to move the body and detect collision, but don't need any automatic collision response. For example, if you need a bullet that ricochets off a wall, you can directly change the angle of the velocity when you detect a collision. See below for an example.
move_and_slide
The move_and_slide() method is intended to simplify the collision response in the common case where you want one body to slide along the other. It is especially useful in platformers or top-down games, for example.
The accompanying sample code (basic_movement.gd) demonstrates:
Examples
To see these examples in action, download the sample project: character_body_2d_starter.zip
extends CharacterBody2D var speed = 300 func get_input(): var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") velocity = input_dir * speed func _physics_process(delta): get_input() move_and_collide(velocity * delta)
This object stops the body when it hits a wall.
In physics classes, we often discuss velocity (e.g., when colliding with walls, producing sounds and converting energy which affects the velocity). However, the documentation for Godot states:
"Character bodies detect collisions with other bodies when moving, but are not affected by engine physics properties, like gravity or friction."
When I tried running the sample code, the object stopped when it collided. This can be said as the object's velocity becoming zero.
Is velocity not related to physics in a game engine? It seems puzzling.
Alternatively, if the object comes to a stop without any physical impact, what would have influenced it? Is the wall not physical?
Also, even though it says, "you have to write some code to create their behavior, it also means you have more precise control over how they move and react." their speed automatically becomes zero. What exactly do I need to implement myself?
Goal:
The documentation only mentions "physics properties, like gravity or friction," so I want to clarify what constitutes "physics."
Since it’s unclear what qualifies as physics, I don’t know what to implement. For example, should I not implement anything because velocity automatically becomes zero? Or should I handle what happens after stopping myself?
I plan to start programming for physics, but it’s ambiguous where the game engine's built-in functionality ends and where it doesn’t (due to vague expressions like “like”). This makes it hard to get started programming, so I want to make the starting point clearer.
The sample code bullet.gd seems to calculate the bounce velocity manually (requiring the game developer to determine the bounce velocity themselves).
extends CharacterBody2D var speed = 750 func start(_position, _direction): rotation = _direction position = _position velocity = Vector2(speed, 0).rotated(rotation) func _physics_process(delta): var collision = move_and_collide(velocity * delta) if collision: velocity = velocity.bounce(collision.get_normal()) if collision.get_collider().has_method("hit"): collision.get_collider().hit() func _on_VisibilityNotifier2D_screen_exited(): # Deletes the bullet when it exits the screen. queue_free()
Related: What does the phrase "Kinematic bodies do not collide with other kinematic or static bodies" mean?