Prevent the blocks from falling through collission body .

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By THE HELIX

I need to make the viewport as a barrier / a wall to hold the blocks which are falling along the y-axis from above.I put four staticbody 2D with collission shape around the viewport to create a barrier.

Here is the project Project . Now if you press the circles they are cleared.If you change velocity. x = 0 and velocity. y = 0 in the circle script the circle do not come down on clearing lower circles.If you change velocity. x = 1 and velocity. y = 1 the circles go through the static body.You can see it by not clicking on the circles and click on them when more circles fall on them. When you put velocity = 1 the circles merge with each other and circles on bottom fall of when circles above them increase.You can see the circles passing through static body if you make visible collision on , Press on the circles after few moments of the circles falling down
Can you please help me solve it ?

:bust_in_silhouette: Reply From: johnygames

I think the reason why the circles pass through other circles is because you have written a line of code which increases the velocity every tick: velocity.y += gravity*delta. This causes the circle to gain extremely high speed after a few seconds. Remove that and what you get is smoothly descending circles. I changed your _physics_process() function to:

func _physics_process(delta):
velocity.y = 100
move_and_slide(velocity)

I also noticed you update the position of the circle directly. You should be using the move_and_collide() function instead. When you update the position of the kinematicbody directly, you override the physics process and that causes glitches.

Does this answer your question?

Thanks for your guidance ; I put the above code in the project . Now the circles are not passing through the collission body. But one problem is that they begin to vibrate after some time of coming down.How to stop the circles from shaking ?

Another problem You told to use move_and_collide() function instead I didn’t understand where to use move_and_collide() in the programme ?

THE HELIX | 2019-09-28 14:41

You should use the move_and_slide() method. It should be used right after you have set the velocity vector to whatever speed you wish to have. So something like:

func _physics_process(delta):
	velocity.y = 200
	move_and_slide(velocity, Vector2( 0, 0 ), true, 2, 0.785398, false)

Take note of the arguments passed in the move_and_slide() method; this might reduce the jittering of the circles a bit.

johnygames | 2019-09-28 20:59

Can you please explain why you used this code
move_and_slide(velocity, Vector2( 0, 0 ), true, 2, 0.785398, false) as I am new to coding . There is a slight jittering on executing it.

THE HELIX | 2019-10-05 17:45

I used that code so as to set inertia to 0 and reduce the number of collisions. Check the docs Using KinematicBody2D — Godot Engine (3.1) documentation in English

Apparently it doesnt work all that well. I think you need to implement your own physics behaviour when the circles collide. The jittery motion must be attributed to the fact that the circles keep their initial velocity after they have piled up. You could code some behaviour where the circles stop moving if they are too close to one another and start moving again if there is free space around them.

johnygames | 2019-10-06 13:29

Can you please tell how to code such that the circles stop moving if they are too close to one another and start moving again if there is free space around them ?

THE HELIX | 2020-01-24 02:07

:bust_in_silhouette: Reply From: Player0330

Yous should us max function

var maxFallSpeed = 100
func _physics_process(delta):
    velocity.y = min(velocity.y, maxFallSpeed)
    moveandslide(velocity)

this will give more flexibility as it will increase speed till a certain point and then maintain it instead of only maintaining it.