Physics Game- Devlog 1


The first thing that came to mind when I thought of a physics-based game is the classic "block-pushing" mechanic found in a lot of games. This kind of mechanic is definitely outside of my Godot comfort zone, so that's why I'm going to try it out!

I found the attached code snipped for CharacterBody2D and RigidBody2D interactions on the Kids can Code website, but as an exercise for myself I'm going to attempt to explain it here.

The for loop is iterating through all the number of times the body collided, and changed direction- this is via the get_slide_collision_count() function.

a variable is created that returns a KinematicCollision2D of the iteration(s). I had never heard of KinematicCollision2D before so let me attempt to also explain this class!

KinematicCollision2D holds collision data from the movement of a PhysicsBody2D" according to the Godot documentation. Essentially, it is returned every time a collision is detected by a PhysicsBody2D.

The information returned includes the colliding object, the collision position, and the remaining motion. I had no idea where to start when detecting collision but basically, if you can access this data, then you can determine a custom response to the collision and create your own logic essentially! Really cool stuff.

Back to the code:

if c.get_collider() is RigidBody2D:

essentially, if the KinematicCollision2D's collider is a RigidBody2D (which our moveable objects are)

then:

c.get_collider().apply_central_impulse(-c.get_normal() * push_force)

This is basically taking the collider, which is a RigidBody2D, and using the RigidBody2D method apply_central_impulse,  "apply a directional impulse without affecting rotation".


(As I'm reading this snippet from the docs it actually seems like the force functions might be a better way to go, since the player will be consistently pushing the block and not hitting it one time...)

lastly just to touch on get_normal(), which returns the colliding body's shape normal at the point of collision. Multiplying the inverted/negative normal by a custom variable push_force makes the object move in the opposite direction of collision. Yay!

This has been a long, drawn out analysis but it helped me a lot to not just steal the code from Kids Can Code, but to understand and now, improve upon it with my new understanding.

Get Physics Game- DH Mechanic Test

Leave a comment

Log in with itch.io to leave a comment.