2D top down collision behavior
-
Hi there! I'm like many now transitioning from Unity to Godot 4.1 but I only quite recently started trying my hand at game developing (and code).
I am struggling to replicate everything I have in Unity like how my tank (RigidBody2D with collision polygon) behaves when colliding with something:
https://youtu.be/4Aj60v9hVLY
the rocks are static body but they behave the same if they are character body.
The tank is propelled by continuous force (impulse) when you press the controls.
I've tried changing various values in the inspector: mass, inertia, friction, damp, damp mode, disable mode..func control(delta): # Apply a force to the tank in the direction of the player's input. if Input.is_action_pressed("forward"): var impulse = Vector2(moveForce, 0).rotated(rotation) apply_central_impulse (impulse) if Input.is_action_pressed("back"): var impulse = Vector2(-moveForce/2, 0).rotated(rotation) apply_central_impulse (impulse) # Apply a torque to the tank in the direction of the player's input. if Input.is_action_pressed("turnRight"): apply_torque_impulse(rotationSpeed) if Input.is_action_pressed("turnLeft"): apply_torque_impulse(-rotationSpeed) func _process(delta): control(delta)
-
Ok so I figured it out. The reason it was eluding me was that it was two separate things which led me to all the wrong conclusions. .
First setting inertia back to 0 (which makes it automatically compute inertia and clearly that's good enough)
Second one of the objects I was colliding with for testing had a strange wonky collision box. It was a slight rhombus although I saw nothing in its inspector to indicate why, and it was slightly scaled up. I am kind of amazed how such a seemingly tiny change had made that object into something that would teleport my tank to space every time I touched one of its corners (not in the video). -
In Unity:
- You use Rigidbody2D components to give objects physics properties
- You apply forces and impulses to move objects
- Objects generally have a "bouncy" and natural-feeling movement when applying forces
In Godot:
- You use RigidBody2D nodes to give objects physics properties
- You generally apply forces in
_physics_process()
, not_process()
- Objects tend to move in a stiffer, less natural way when applying forces
I think the reason your tank moves differently in Godot is that:
-
Godot uses a different physics engine under the hood: Godot Physics, while Unity uses Physics2D. (In Godot 3 they were using Bullet Engine)
-
Godot's default physics parameters tend to result in stiffer movement
-
You're applying forces in _process() instead of _physics_process()
To get Godot physics to feel more like Unity:
- Move your control() function call to _physics_process(), not _process()
- Increase the mass and/or lower the friction/damp of your RigidBody2D
- Experiment with the PhysicsEngine settings in Project -> Project Settings -> Physics -> 2D Physics
- You may need to reduce the force you're applying to compensate for Godot's stiffer movement
Replicating the exact "feel" of Unity physics in Godot can be tricky. You'll need to experiment with Godot's physics parameters and tune them to your liking.
-
the process part is fair enough my bad, but I've tried all kinda of values in the inspector and there's no difference if they are set to single digits. Well I've tried changing all the physicsengine settings I could find but they didnt seem to change anything about the collision. Maybe this engine cant handle impulse or something
-
Ok so I figured it out. The reason it was eluding me was that it was two separate things which led me to all the wrong conclusions. .
First setting inertia back to 0 (which makes it automatically compute inertia and clearly that's good enough)
Second one of the objects I was colliding with for testing had a strange wonky collision box. It was a slight rhombus although I saw nothing in its inspector to indicate why, and it was slightly scaled up. I am kind of amazed how such a seemingly tiny change had made that object into something that would teleport my tank to space every time I touched one of its corners (not in the video). -
-