How to i get the face of the collision

Godot Version

4.2.stable

Question

How would i get the two points of the face of the collision polygon that my bullet (characterbody2d) has collided with? Im trying to make a 2d splatoon-like game and i need the bullets to ink the face of the tilemap that im using. Image for scale

Is your bullet a RigidBody or a CharacterBody? That changes how you can access the collision data.

The bullet is a characterbody2d node

Okay, so firstly make sure you have the motion mode set to floating. This will allow the bullet to treat everything as a floor, including ceilings and floors. It will make this next bit easier. In addition, I’m assuming that you’re just using rectangular shapes, if you’re not it’s going to get harder, but should still be possible.

First, working out where the collision took place, you can do that by using get_last_slide_collision ( ) to get the collision, then call get_position() on that collision to get the global_coordinates. Since tilemaps are on a set grid, with each cell being a set size, you should be able to work out the start and end position by rounding it to the nearest multiple of whatever your cell size is.

To get the side that has been hit, what I would do is use the get_wall_normal() when the collision happens. This takes the information of the normal of the wall that’s just been collided with. A normal is a vector that faces outwards perpendicularly. So if you got the normal of the floor, it would be a vector that points up, and the normal of the ceiling would point down. You could use this to work out which side has been hit.

Using the position of the collision and the normal of the “wall” you should be able to find the correct coordinates for the side that has been hit.

Hope this helps!

thank you!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.