0 votes

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.

in Engine by (76 points)

1 Answer

+1 vote
Best answer

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.

by (20,397 points)
selected by

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.

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?

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)

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.

Brilliant, thank you!

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.