#How to properly handle round RigidBody2D with a CharacterBody2D?

11 messages · Page 1 of 1 (latest)

knotty zinc
#

Hi everyone! I'm working on a project where I want to use CharacterBody2D object for a player and a RigidBody2D object for a ball, which can be pushed and tossed around. Found some tutorials but they're very brief and are not addressing issues I'm encountering. My main problem is that my player object is getting tossed around when the applied push force to the ball is high enough.
I'm currently using this code inspired by GDQuest tutorial:

var collisions_count = character.get_slide_collision_count()
    for i in collisions_count:
        var collision = character.get_slide_collision(i) as KinematicCollision2D
        var collider = collision.get_collider()
        if collider is RigidBody2D:
            character.velocity.x = input_direction.x * push_strength
            collider.apply_impulse(input_direction * push_strength * delta)

Is there perhaps some tutorial addressing this topic?

empty vector
#

I think that your character is moving much faster than the ball and then the friction from the ball rolling is pushing you up. Here are some things you could try:

  • Change your character collision shape to a rectangle or capsule
  • Make the ball slightly bigger
  • Reduce the mass of the ball or set its lock_rotation=true
  • Use character.move_and_slide() instead of changing the velocity directly
knotty zinc
#

Thank for the tips. lock_rotation has completely broken the movement but slowing down the character worked. I'm a little surprised that RigidBody2D could affect CharacterBody2D as it should be controlled only by code. I guess not? Is there a way to disable the friction effect of the ball onto my character?

#

When I walk on the ball it rotates erratically, probably due to the friction.

empty vector
#

I don't think you can disable friction only between the ball and the character. If you reduce it on either they will have a harder time stopping. You can try reducing friction on the ball and increasing its damping to make it stop.

CharacterBody2D still needs to interact with the world somehow. What do you mean rotates erratically? That is the correct physical thing to do. If you mean make it more predictable/intuitive from a player standpoint, you could use a box shape collider without rotation on the ball and then rotate the sprite manually when it moves.

knotty zinc
#

The rotation looks alright but the ball rotates much quicker than it should considering it's mass. It's either the friction causing it or applying too many impulses because of high number of collisions per second.

#

Maybe I should somehow detect if I'm on the ball and stop creating impulses in such scenario...

empty vector
#

Maybe that would work. Or if you want it to be more physically accurate you could change the charater to a RigidBody instead of Character body and play around with the masses.

knotty zinc
#

Played with it in the past and it never felt okay. It's probably possible to make a game where player object is RigidBody2D with it feeling good but at this time I don't have the knowledge needed to make it work. Will stick to tossing a ball for the moment.
Either way thank you for your assistance, I'm appreciating it.

empty vector
#

Yes, a RigidBody for a player is not usually a very good idea. It's better to use the CharacterBody but you need to experiment a lot with the movement to get it just right

#

Especially in this case since you want some sort of physical interaction with other bodies