How to emulate KinematicBody's move_and_collide() method for RigidBody in Kinematic mode?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By zejji
:warning: Old Version Published before Godot 3 was released.

Summary:

When using a KinematicBody, there is a useful move_and_collide() method which allows the user to move the body while preventing it from clipping through other collision bodies. What is the best way to emulate this behaviour with a RigidBody in MODE_KINEMATIC?

Details:

I have implemented a FPS puzzle game (in Godot 3.0 alpha 2) where the player can pick up and position objects (e.g. putting a box on a table). The objects which can be picked up are currently KinematicBodies and the collision detection is handled by using KinematicBody’s move_and_collide() method to prevent the objects from clipping through other objects.

However, I realised that, when you put an object down, gravity needs to act on the object. Accordingly, I converted the KinematicBodies into RigidBodies and specified that they should be in MODE_KINEMATIC when being carried by the user (switching back to MODE_RIGID when dropped). However, my original code did not work any more because the move_and_collide and move_and_slide methods are no longer available.

I have created a GitHub issue asking whether it is possible to make KinematicBody methods available when RigidBody is in kinematic mode - see here:

However, in the interim, I would be grateful for any suggestions as to how best to emulate KinematicBody’s move_and_collide() method for RigidBody in Kinematic mode? Presumably this would need to be handled somehow in the _physics_process() callback? Also, the docs state here that:

As a warning, don’t change RigidBody’s position every frame or very
often. Sporadic changes work fine, but physics runs at a different
granularity (fixed hz) than usual rendering (process callback) and
maybe even in a separate thread, so changing this from a process loop
will yield strange behavior.

Is this warning relevant when the RigidBody is in kinematic mode? If so, how can we move the RigidBody so that it follows the player’s first-person view while being ‘carried’ by the player?

Surely it can’t be impossible to implement an object that can be picked up and then become subject to gravity when put down?

Just a thought, hypothetical:
you could juggle a node between being RigidBody and KinematicBody. Anytime you’d need either “feature” you would cast a RigidBody up to PhysicsBody then back down to KinematicBody and the other way around. In C++ you have cast_to<t>() where t is your target class. IDK how casting works in GDScript, though.

snwflk | 2017-11-30 16:07

Many thanks for this suggestion - I’ll give this a try this weekend! I am using GDScript exclusively so I’ll first need to check whether it’s possible (and, if so, how) to do the casting you describe…

zejji | 2017-12-01 11:29

Well, I don’t know… it’s a long shot and it might not work (hence “hypothetical” :D). I didn’t get to test it. It theoretically works but you loose some info on casting upwards.

snwflk | 2017-12-02 10:09

:bust_in_silhouette: Reply From: jjmontes

I’m using TestMotion which allows you to at least check whether the movement would be valid. But I agree that the methods you mentioned would seem to better suit this use case.