Using test_move() in 3.1

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

I’ve been trying to use KinematicBody2D’s test_move() to see if my character is next to a wall, but not touching it, but I’m having a little trouble with it since the actual documentation is just

bool test_move( Transform2D from, Vector2 rel_vec, bool infinite_inertia=true )

Checks for collisions without moving the body. Virtually sets the node's position, scale and rotation to that of the given Transform2D, then tries to move the body along the vector rel_vec. Returns true if a collision would occur.

and all of the tutorials I could find just show an older version of the function where the only argument was a Vector2 which is a little more straightforward.

As far as I understand, the Transform2D is the objects initial position to test from, the Vector2 give the direction it should move, and infinite inertia deals with whether or not the object rotates on impact.

My issue right now is with the Transform2D, so far the only function I could find in the documentation that seems like it should return the objects transform in that format is CollisionObject2D’s

shape_owner_get_transform( int owner_id ) const

Returns the shape owner's Transform2D.

but no matter what I try I always seem to get an identity matrix out of it, the bit of code I ended up with trying to figure this out looks like

var transform2d = Transform2D(owner.shape_owner_get_transform(owner.get_shape_owners()[0]))

if owner.test_move(transform2d, Vector2(-20, 0), true):
  print("on your left").

I’m not sure which function I’m misunderstanding, whether it’s test_move(), shape_owner_get_transform(), or get_shape_owners() which I used to get the owner_id but any help would be appreciated.

:bust_in_silhouette: Reply From: TheLoveDoctor

Gonna answer my own question here since I solved it shortly after posting the question. My issue is that owner.shape_owner_get_transform(owner.get_shape_owners()[0]) is not how one would go about getting a body’s transformation. Instead I needed to construct a Transform2D with my character’s.

in the end my next_to_wall() function looked like:

func next_to_wall(distance):
	var transform2d = Transform2D(0.0, owner.position)
	if owner.test_move(transform2d, Vector2(distance, 0), true):
		return true
		return false

Note I entered 0.0 for the first argument because that just worked for my case, otherwise it would be owner.rotation.

owner.test_move(owner.get_transform(), (_velocity * delta), true)

StillWaiting | 2019-12-19 22:07