How do I find the mesh that is collided with on a gridmap?

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

I need to figure our which mesh my kinematicbody is colliding with from a gridmap. I need to determine if it is a ramp so that I can rotate the body to make it go over the ramp.

:bust_in_silhouette: Reply From: Gokudomatic2

I think you can’t really find it out. You can only trying to figure which cell was at the collision point.
But if you only want to know if your body is colliding with a ramp, you can check the normal of the collision. If it’s not straight horizontal (a wall) or vertical (the floor), then it’s a ramp.

Thank you for your help, but I don’t understand how to do this. How do I find the angle from the Vector3?

Eyeball Monkey Media | 2016-07-02 16:44

Since you’re using a KinematicBody, it has the method get_collision_normal(), which looks like this:
normal collision – GeoGebra

After that, a dot product of the normal on the velocity will tell you if it’s perpendicular (vertical wall, dot product being zero) or something else.
To know where to orient your body to climb up the ramp, doing the dot product of the up vector (Vector3(0,1,0)) on the normal, then multiply the result to a normalized vector of the normal, then substract it from the up vector to get the final vector that slides along the ramp towards the top:
vector to climb – GeoGebra

Gokudomatic2 | 2016-07-02 17:27

Thank you! How do I figure out how to rotate my body with this?

Eyeball Monkey Media | 2016-08-05 23:59

There are many different ways to do it. But the simplest is to create a transform, for instance with the method looking_at, and apply it to the node.

And if you still didn’t do it, you should definitely read the official documentation about vectors and transforms. It explains the fundamentals about all this and it contains lot of examples in gdscript.

Gokudomatic2 | 2016-08-06 06:52