What are the physics of pushing objects?

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

I am making a 2D platformer. There are some Kinematicbody2d’s i want the player to be able to push. I also want the player to slow down when they push the object depending on the object’s weight. There is also a function on every phyics_object class called apply_force, which takes an angle and a speed (i know i should make it a Vector2d, i just haven’t wanted to change every time iv’e used it yet) and applies them to the velocity of the object. All of the physics objects use move_and_slide. Here is my code in the player script:

for i in get_slide_count():
	var collision = get_slide_collision(i)
	if collision.collider.is_in_group("Pushable"):
		collision.collider.apply_force(velocity.angle()+90,velocity.length())
		velocity -= Vector2(velocity.length() - collision.collider.weightmuliplier,0).rotated(position.angle_to_point(collision.collider.position))

I didn’t expect this to work, but this at least gives you a good idea of what i’m going for. Any ideas?
If it’s too complicated to explain in one answer, i’d appreciate a good source to learn from. Iv’e been googling this for a while and haven’t found an answer yet.

If you want to use physics then you’d normally use RigidBody. Perhaps not for the player itself but for the pushed objects. RigidBodies have a Mass/Weight. This would have the advantage that RigidBodies can also push each other but the disadvantage that your code would be inaccurate when pushing two adjacent or stacked up objects.

apply_force is only available for RigidBodies (as far as I know). This would be useful for constant forces like gravity, wind or maybe river currents. If you want to “push” a rigidbody then apply_impulse is the right method.

My knowledge of Kinematicbodies is limited though. Therefore only a comment.

wombatstampede | 2019-05-23 08:32

i said i made an apply_force method myself. I should probably use RigidBody though.

lincolnpepper | 2019-05-23 18:59

You said? Ok, now I understand “There is” means, you made.

Anyway. You can take RigidBody just be aware that many maybe handy KineticBody methods are missing.

wombatstampede | 2019-05-24 09:18