How to fix movable box physics?

Godot Version

Godot 4

Question

Help Programming Physics
I’m new to game making, and I’m having troubles making a movable box physics.
Among the many things that don’t work correctly, these are the main ones:

  • The box rolls instead of sliding
  • When it rolls, sometimes it kidnaps the player with it and launches it in the air (prob because the player walks on the box while it’s rolling and the game starts freaking out)
  • When many boxes are piled up, they stop moving for no apparent reason (even on flat floor)

I have no idea on where to start to fix any of these problems.

When the game starts, all the blocks go into a “pushable” group
This is the code that I have right now in the player:

#MOVEMENT
#use of rays to stop getting stuck on 1 pixel jumps
var direction = Input.get_axis("ui_left", "ui_right")
	if direction:
		velocity.x = direction * SPEED
		if velocity.x > 0: 
			$Sprite2D.flip_h = false
			$lowRay.target_position.x = 15
			$highRay.target_position.x = 15
		else: 
			$Sprite2D.flip_h = true
			$lowRay.target_position.x = -15
			$highRay.target_position.x = -15
		if($lowRay.is_colliding() and !$highRay.is_colliding()): #ledge detection
			velocity.y = -40
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
		
	#PUSHING BLOCKS
	for i in get_slide_collision_count():
		var collision = get_slide_collision(i)
		var collision_box = collision.get_collider()
		if collision_box.is_in_group("pushable") and abs(collision_box.get_linear_velocity().x) < MAX_PUSH:
			collision_box.apply_central_impulse(collision.get_normal() * -PUSH_FORCE)
	move_and_slide()

Any help is appreciated
Thanks in advance

IMPORTANT UPDATE IN THE ANSWERS

Hello
can you change this code

if collision_box.is_in_group("pushable") and abs(collision_box.get_linear_velocity().x) < MAX_PUSH:

to this

if collision_box is RigidBody2D:

‘sorry for my bad english’

Hi.
Try checking Lock Rotation.

image

Oh, and I’d probably recommend you disable “Can Sleep” if the cubes freeze in position.

Deleted this comment.

Brain flub - forgot we were discussing 2D and I mentioned Jolt which is 3D only

1 Like

My plan for the game was to make boxes change materials (wood, iron, helium…) so they need different properties. I also want some of those boxes to not get pushed ever if they are made of a heavy material.

I tried deactivating “Can Sleep”, but it seemed to do nothing.
I think the problem might be somehow the collision with the floor, and it gets stuck in 0 pixel gaps from 2 different blocks somehow.

The Lock Rotation fixes the rolling problem, but if possible I would like the blocks to still be able to roll, just not while I push them. (like when they go down a slope, they’ll be slightly rotated to have all the bottom side of the box touch the slope)

What I mean is by activating the lock rotation I lose the slope mechanic, while deactivating it I lose the sliding mechanic. Isn’t there something in the middle?

I also found out that even single blocks get stuck on the floor randomly, so it must be something about the contact with the floor

UPDATE: I found out that the problem that causes literally EVERY SINGLE ONE of the problems I listed before is the way I did the floor (I used a tilemap where each tile had a physics layer).
I’ll try generating the floor hitbox programmatically and update you all on the result.

Ok, I changed the whole hitbox of the floor with a single big collisionPolygon and the blocks no longer get stuck on thin air.

The rolling problem on the other hand still stands. My new theory is that I push the block from the top, causing it to roll. If anyone has any idea on how to fix, all the ideas are appreciated.

Ok I have no idea on how to fix it, if anyone has any proposal just give it to me and i’ll try it

Isn’t Jolt physics exclusively for 3D?

1 Like

You are correct. Brain flub on my part. Edited my comment.

1 Like