Is there any way to use only the tip (arrowhead) of the raycast for collisions?

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

stupid question probably, but I’m new to godot and coding in general so I haven’t managed to get anything but the raycast to sort of work.

For context I’m making a farming sim. Planting has been difficult to implement though. I’ve been trying to fix a bug where you can plant multiple seeds in the same plot. The seed/plant is an animated sprite, and I couldn’t find another way to check if there is a sprite already at a location so I used a raycast to send signals if it collided with the sprite.

However, when your inside the area of the other seed you can’t plant in the next plot because the other half of the raycast is still colliding. Is there anyway to deactivate collisions to part of the raycast so that your only checking if the arrowhead collides? or if not, anyway to detect if there is a sprite at a specific location before planting?

I’m also open to other suggestions.

:bust_in_silhouette: Reply From: aXu_AP

You might want to change the raycast into an area with small collision shape (1x1 rectangle). Offset the area in such a way, that it matches the logic for placing the plant. In code, change ray.is_colliding() to area.get_overlapping_areas().size() > 0 to check if there are any plants under the target area (or get_overlapping_bodies() if they’re physics bodies instead of areas). This is more or less a drop-in solution.

Second way could be asking physics server directly in code, is there a shape at certain point. For how to achieve this, follow the tutorial for raycasting but change intersect_ray() into a intersect_point()(docs).

Note: I assumed here that plants are planted in a grid or otherwise pretermined plots. If the system is more freehanded, just use such area that covers the entire plant.