0 votes

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.

in Engine by (22 points)
edited by

1 Answer

0 votes
Best answer

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 nexttowall() 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.

by (22 points)
owner.test_move(owner.get_transform(), (_velocity * delta), true)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.