0 votes

Hello everybody,
i´m new here and I´m doing a small flipper game in order to learn how to code with Godot 3.
Up to now what puzzles me the most is a problem that occurs with collision shape, but it is something that doesn´t happens all the time: either I´m doing something wrong, or there is a bug with the physic engine.

The game starts with a ball (Rigidbody, mode rigid) laying on top of a spring (Rigidbody, mode character).When the player hit the down arrow, the spring is compressed down (the ball follows it, because gravity is enable) and when the button is released the spring come back to original position and push the ball into the game table.
enter link description here

Here´s the script for the spring movement:

extends RigidBody2D
export (float) var SPEED_UP
export (float) var SPEED_DOWN
const lower_point=850
const upper_point=720

func _ready():
    pass

func _physics_process(delta):
    if Input.is_action_pressed("ui_down") and position.y<lower_point:
        set_linear_velocity(linear_velocity+Vector2(0,SPEED_DOWN))
    elif Input.is_action_pressed("ui_down") and position.y>=lower_point:
        set_linear_velocity(Vector2(0,0))
    else:
        if position.y<upper_point+2:
            set_linear_velocity(Vector2(0,0))
            position.y=upper_point
        elif position.y>720:
            var dist=position.y-upper_point
            set_linear_velocity(Vector2(0,-SPEED_UP*dist))
#
    if position.x>684 or position.x<682:
        position.x=683

It works fine most of the time, but then magic happens: somehow when the spring is in a lower position, the ball find a way to "push" the collision shape of the spring by side, and i dont mean the entire "spring" node (rigid body + sprite + collision), but the collision shape only (at least it looks like that): in fact, the sprite remain perfectly in position, and even if i attach another sprite as a child of said uncooperative collision shape I cant see any strange movement.

However from the moment in which the first ball move the collision shape away, all the further ball simply fall through the spring: collision shape is no more there!

Hope i made myself clear, do somebody know what´s happening here?

in Engine by (1,459 points)

1 Answer

0 votes
Best answer

For such complicated to visualize behaviors I would recommend providing the project or a short video or gif. It's quite hard to picture what's actually wrong.

by (840 points)
selected by

https://drive.google.com/file/d/1XQRbmaMhibqRQmbpdWAQbhWLj29ZliPy/view?usp=sharing

here´s the zipped project folder, any help would be greatly appreciated! (together with any suggestion unrelated to the posted problem).

If you want to encounter the "bug" just play the game for a few match, the ball will start falling through the spring after a few shots

It's actually pretty simple. You did a common mistake, which is not properly getting rid of all the objects after the game is lost and you restart. What's happening is that some collision shapes literally get stuck with each other and therefore produce weird behavior. To solve this you should make use of Godot's scene system. Create one game scene and one start scene. You can then switch between them, having Godot making sure everything is removed properly.

To switch to another scene (You can get the scene path very quickly i you right click on the scene file in Godot's resource explorer and select "Copy path"):

change_scene(scene_path)

To achieve an overlay effect:

var other_scene = preload(scene_path)
func _ready():disable_every_input_in_old_scene()
var new_scene = other_scene.instance()
add_child(new_scene) # just add to root node

When you remove the overlay, just do a fadeout animation and put a functioncall event at the end of it, which will remove the instanced scene. That's it. :) When just changing scenes via change_scene, you don't need to get rid of all the objects, Godot does it for you.

Thank you a lot!! I will surely put your suggestion in practice in my next more complex project, for now i simply made the spring appear and disappear together with the ball (basically the same thing that you suggested, but instead of reloading the entire scene i only reload the spring).

I´m puzzled tho, should I always expect this kind of physic bugs?

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 Frequently asked questions and 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 [email protected] with your username.