Rigidbody2D player falls off from KinematicBody2D moving platform

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

I got RigidBody2D ball player and KinematicBody2D moving platform but when im on moving platform ball falls off from it. I want ball to move with platform.

:bust_in_silhouette: Reply From: Varden

Try adding Static or Rigid body as a child of Kinematic body.

You mean that i should add my player as a child of moving platform? If so, how can i do that?

smetek | 2020-08-06 14:29

I tries it and I think you are doing something wrong. Maybe issue is with your code.

  • Kinematic body should be moved by script. Usually it ignores game physics. So you should use it for objects that you decide how to move.
  • Rigid body is body moving by physics only. So if you want to influence its movement by code, then you should do in only inside _physics_process() by using its Methods and never editing its Properties.

You can check this GitHub - VardenTheOne/godot.KinematicBody2D-and-RigidBody2D-test

Varden | 2020-08-06 16:22

But in the example that you send ball falls off from a moving platform too. I want to stick the ball to the platform.

smetek | 2020-08-06 17:55

here’s a short gif what problem i got and what i want Imgur: The magic of the Internet

smetek | 2020-08-06 18:18

Oh, I did not understand initial question correctly. My example is bad, ball on the top is rolling.
So your question is friction between Static and Rigid body. As I know you can not apply physics material to KinematicBody. So you have to manage it with code, or to make movement of platform with accelerations not more than friction, so RigidBody does not slide on it.
You can check my git repo again. Code is dirty, but you can play a bit to see how it influence.

Varden | 2020-08-06 22:58

Yeah it work’s but not with circleshape collision. Turns out that the problem is collision shape because of it ball is moving and rotate constantly.

Solution
I just added another collisionshape2d with rectangleshape2d to the ball (player) which is disabled, and write this:

if $RayCast2D.is_colliding():
	var colliding_with = $RayCast2D.get_collider()
	if colliding_with.is_in_group("MovingPlatforms"):
		global_rotation = 0 
		$CircleShape.disabled = true
		$RectangleShape.disabled = false
else:
	$CircleShape.disabled = false
	$RectangleShape.disabled = true

I know this is not the best solution but i could not find better.

Thanks Varden for helping because without you im sure i would not find the problem.

smetek | 2020-08-07 00:27

You can check if player collides with platform and apply impulse to player to mimic friction.

Varden | 2020-08-07 07:50