0
\$\begingroup\$

I'm coding a game with Flutter and the flame_forge2d package (which uses Box2D internally). I want to make a game whose principle is similar to that of the game One More Bounce (a game where you have to move a ball forward by placing bars on which the ball bounces).

In One More Bounce, the player controls a ball that ALWAYS bounces (no matter the forces applied). The advantage of such behavior is that the ball never gets stuck in a corner (which would be bad since the aim of the game is precisely to make the ball bounce by placing paddles at the bottom). I would like to have the same behavior but everything I have tested so far does not allow the ball to bounce in all situations. I don't know if the One More Bounce developers used Box2D or a custom collision system. Anyway, I would like to have the same behavior!

When the ball hits another object, this is what I do (that does not prevent the ball from stopping) :

  @override
  void endContact(Object other, Contact contact) {
    super.endContact(other, contact);

    final linearVelocity = body.linearVelocity.clone();
    final length = linearVelocity.normalize();

    if (length < 1) {
      body.linearVelocity = linearVelocity * 1;
    }
  }

If you have any ideas on how to do this (or if we literally need to change the basic behavior of the ball), I'm all ears.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

This line

body.linearVelocity = linearVelocity * 1;

does nothing as you are multiplying with one and length is never less than one as it is the length of the normalised vector which is one.

To get a bounce that never stops, instead of overriding the postContact try setting the friction of both the ball and the platform to zero.

\$\endgroup\$
1
  • \$\begingroup\$ linearVelocity.normalize() normalize the vector and returns its length before normalization. So it can be less than 1. After that, body.linearVelocity = linearVelocity * 1; changes the speed of the ball to a minimal. Also, the friction are already set to 0. But the ball stops under some conditions (usually in corners or when hitting some target around a corner) \$\endgroup\$
    – Alex Irié
    Commented Apr 30 at 9:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .