+3 votes

Hello everybody,
I have a little issue trying to use the Physics2D and RigidBody2D collision system.
I made a 500x500 window with StaticBody2D and RectangleShapes as border. then I put a Rigidbody2D ball with a Sprite2D and CircleShape2D and removed gravity. now I made the ball constantly accelerate. here is the problem, the ball accelerate has it is intended to, but as it gains more and more speed it lasts by fleeing out of the screen.

So how can I prevent the ball of doing this. I've looked the internet to find the correct solution, but I cannot figure out what is the correct answer for godot.

here is the code for the ball:


extends RigidBody2D var speed = 2000 func ready():
randomize()
set
linearvelocity(Vector2(randrange(-10,10),rand_range(-10,10))) func getspeed():
var vect = get
linear_velocity()
return (abs(vect.x) + abs(vect.y)) func integrateforces(state):
var vect = getlinearvelocity()
var spd = get_speed() if spd < speed - 1: vect *= 1.025 elif spd > speed + 1: vect *= 0.975 set_linear_velocity(vect)


and there is a screenshot of the project:
screenshot
Thank you for your time.

in Engine by (18 points)
edited by

5 Answers

+3 votes
Best answer

I had this issue on a 3D prototype.
I solved it by increasing the fixed fps value in Project settings, Physics .
It worked but I don't know if a better solution is avalaible.

by (509 points)
selected by

that solved my issue, thank you.
It's still doing strange bounce at very high speed but it is unlikely that this kind of thing happen in my game.

If you want a ball who behaves like a classic breakout game you can try with a kinematic2D instead.
Just give it a direction vector and a speed in his move() function.
His bouncing code may look like this.

 if is_colliding():
   direction = get_collision_normal().reflect(direction)  #bouncing like a breakout ball.
0 votes

You probably want to uncheck the "Custom Integrator" box. integrate_forces gets called even if it's unchecked, it just turns off the internal force integration. Does your ball collide with the boundaries at all?

by (48 points)

The ball collides with the boundaries, but when it become too fast it just pass trough. no matter if I checked the Custom integration button or not.

Huh. And I see you have continuous collision turned on. Maybe it's a physics bug then... :(

Oh, wait. Have you tried accelerating by applying a force or impulse instead? Maybe setting the velocity directly is somehow overriding the collision response when it gets too big? Dunno.

No more luck with that :-/
I tried:

extends RigidBody2D

var speed = 4000

func ready():
randomize()
apply
impulse(Vector2(randrange(-30,30),randrange(-30,30)),Vector2(randrange(-30,30),randrange(-30,30))) func get_speed(vect):
return (abs(vect.x) + abs(vect.y)) func integrateforces(state):
var vect = getlinearvelocity()
var spd = get_speed(vect) if spd < speed - 1: apply_impulse(Vector2(0,0),vect) elif spd > speed + 1: apply_impulse(Vector2(0,0),-vect) #set_linear_velocity(vect)

0 votes

Make your collision boundaries thicker

by (436 points)

Hello, it is nor a viable solution for my game (an arknaoid like game)

Then increase physics fixed step

0 votes

Hi,

Maybe you need to tweak the physics settings. You can find them under Project Settings -> Physics 2D . Try lowering cell_size (It has to a power of 2)

see http://docs.godotengine.org/en/latest/tutorials/2d/physics_introduction.html?highlight=cell_size

for more information.

by (17 points)

I've already tried this. It does not seam to affect anything.

+2 votes

This is my solution:

func limitVelocity(delta):
    var currentVel = get_linear_velocity()
    if (currentVel.length() > MAX_BALL_SPEED):
        currentVel = currentVel.normalized()*MAX_BALL_SPEED
    set_linear_velocity(currentVel)
by (683 points)

Nice solution. Thanks for posting it.

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.

Categories