Godot Version
4.5
Question
So I’m making collision with Godot, and just did the work to calculate the direction between the player and the thing they collided with, but I’m not sure how much I should push the player back, does anyone here know what I should set it to? The player is an Area2D.
func _on_body_entered(body: Node3D) -> void:
var playerPosition = self.position;
var collidedObjectPosition = body.position;
var vector = Vector2(playerPosition.x-collidedObjectPosition.x, playerPosition.z collidedObjectPosition.z);
var direction = atan2(vector.y, vector.x);
self.position.x += cos(direction);
self.position.z += sin(direction);
Seems like this could work for a circle shape, but ultimately why are you not using a Physics Body such as CharacterBody3D? The physics engine under the hood is well-suited to resolving collisions, better than re-implementing it via Areas
Because I like implementing things myself. I wanna know how to push back the player the right amount.
Use character body. If you want to handle collision resolve yourself, use move_and_collide() instead move_and_slide()
You would need to use different collision resolution algorithms for different shape-shape overlaps. Spheres are pretty easy with an overlap depth of distance^2 - (a.radius + b.radius)^2 and a direction as you found (though you do not need to convert to radians atan2 and back to direction cos/sin, only limit or normalize the difference), Convex shapes may use the “separating axis theorem”
You may have more fun and learning opportunities creating your own game without an engine, pygame for instance will handle creating windows, taking input, and blitting sprites but you have to program the collision etc. Otherwise going against the grain with a game engine will lead to more strange bugs that aren’t related to the problems you want to solve.
1 Like