RigidBody2D vs KinematicBody2D for a flappy birds clone

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By alfandango

I’m playing around with godot 3 and trying to build a simple flappy birds clone as a learning exercise.

I’m trying to understand whether it is better to use RigidBody2D or KinematicBody2D for the bird.

The bird needs to:

  • Ascend when an input (e.g mouse click) is detected
  • Descend when there is no input
  • Emit a “hit” signal when it collides with an object then stop and descend quickly to the ground.

My understanding is that RigidBody2D has built in physics into the object. This means that the decent of the bird will happen by default, is that correct? Therefore I would need to apply a force to the bird to make it ascend in response to an input.

This sounds like a natural way to do this however the documentation also says:

By default, rigid bodies do not keep track of contacts, because this can require a huge amount of memory if many bodies are in the scene. To enable contact reporting, set the contacts_reported property to a non-zero value.

This would indicate this may not be a good method for a flappy bird.

The next option is KinematicBody2D. However as the docs state, this object is not affected by the physics properties like gravity or friction. Is there a way to manually invoke or utilize these properties easily with code?

Thanks in advance.

:bust_in_silhouette: Reply From: kidscancode

RigidBody2D will work fine for this. Enable the contact monitor as recommended in the docs. Note the qualifier:

because this can require a huge amount of memory if many bodies are in the scene

You’re not going to have many bodies in the scene of a flappy bird game. Gravity will happen automatically, and you can apply an impulse when the button is pressed.

That said, KinematicBody2D is a fine choice too. The difference is that kinematic body must be moved by you in code. To have gravity, apply an acceleration to the bird’s y velocity and then move the bird by its velocity.

Flappy bird is simple enough you could fairly quickly make it both ways and decide which you like better. It’s a good idea to know how to work with both body types anyway.

this can require a huge amount of memory if many bodies are in the scene

What exactly constitutes many bodies though?

ultimately what I eventually want to build won’t be a flappy birds clone. I want to make an infinite side runner where the objective is to simply dodge objects. These objects may be static obstacles or fast moving enemies.

alfandango | 2018-06-04 07:36

To have gravity, apply an acceleration to the bird’s y velocity and then move the bird by its velocity.

This may be a stupid question, is this part of the API and therefore has an inbuilt method for this to be achieved?

alfandango | 2018-06-04 09:55

No, that’s what “moved by you” means. You keep a velocity vector and accelerate by adding to it every frame:

extends KinematicBody2D

var velocity = Vector2()
var gravity = 500

func _physics_process(delta):
    velocity.y += gravity
    move_and_collide(velocity * delta)

kidscancode | 2018-06-04 13:51

What exactly constitutes many bodies though?

Well, that depends on what else is going on in the game. If it’s hundreds, maybe you’ll start to see a problem. If it’s thousands, you almost certainly will.

kidscancode | 2018-06-04 13:53

Brilliant, thank you!

alfandango | 2018-06-04 13:54

Well, that depends on what else is going on in the game. If it’s hundreds, maybe you’ll start to see a problem. If it’s thousands, you almost certainly will.

Thanks, unfortunately this part of a docs is somewhat ambiguous. It would be nice to present some kind of metric and context for when this becomes an issue. Like you say, small game probably not an issue. Larger game, may be an issue but what is a larger game.

However like you say I will just have a play around and see what happens.

alfandango | 2018-06-04 13:58