0 votes

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 ?

asked Sep 20, 2019 in Projects by THE HELIX (128 points)

2 Answers

0 votes
Best answer

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 physicsprocess() function to:

func physicsprocess(delta):
velocity.y = 100
moveandslide(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?

answered Sep 20, 2019 by johnygames (1,532 points)
selected Dec 6, 2019 by THE HELIX

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 moveandcollide() function instead I didn't understand where to use move_and_collide() in the programme ?

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 moveandslide() method; this might reduce the jittering of the circles a bit.

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.

I used that code so as to set inertia to 0 and reduce the number of collisions. Check the docs https://docs.godotengine.org/en/3.1/tutorials/physics/using_kinematic_body_2d.html#which-movement-method-to-use

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.

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 ?

0 votes

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.

answered Sep 22, 2019 by Player0330 (28 points)
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.