How to use test_motion from RigidBody2D?

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

The docs states

Return whether the body would collide, if it tried to move in the given vector. This method allows two extra parameters: A margin, which increases slightly the size of the shapes involved in the collision detection, and an object of type Physics2DTestMotionResult, which will store additional information about the collision (should there be one).

But how can I get the Physics2DTestMotionResult? I tried doing it like this:

var p
if (test_motion(vector, 0.08, p)):
   var k = p.get_motion_remainder()

But I get the following error:

Invalid call. Nonexistent function 'get_motion_remainder' in base 'Nil'.

What’s the right way to do it?

I’m interested too on the answer to this question.

genete | 2016-05-04 09:11

:bust_in_silhouette: Reply From: genete

You have to create a new Physics2DTestMotionResult before use it:
Try this:

var p =Physics2DTestMotionResult.new()
if (test_motion(vector, 0.08, p)):
   var k = p.get_motion_remainder()

Probably you have to free the Physics2DTestMotionResult after used. Not sure.