# Is there a way to detect collisions between two objects if one has no hitbox? c#

###Godot Version
Godot Engine v4.2.2.stable.mono.official

###Question
I have a line2d node where each segment is affected by gravity, I want the gravity to stop being applied on a point if it is colliding with something (e.g. the ground) which has a physics body and hitbox. The points however don’t have a hitbox. Is there a way around this?

``````public int segmentAmount = 250;
public bool gravityActive = true;
public float gravity = 25.0f;

if(gravityActive)
{
for (int i = 0; i <= segmentAmount; i++)
{
var curPosG = GetPointPosition(i);
SetPointPosition(i, new Vector2(curPosG.X, curPosG.Y + gravity));
}
}
``````
1 Like

You could try to do a ray-cast downwards for each point of the line, so, if the distance of the ray-cast hit is less or equal to the width of the line you can stop the gravity, and relocate the point to avoid overlapping with the floor.

Check the ray-casting tutorial for a more detailed explanation.

Then you would have issues with the segments overlapping the floor if it isn’t completely flat depending of the number of points per distance, but that’s another problem.

2 Likes

There is an issue with optimization, since if there are too many points then it gets lagged out a lot.

Then you could try to divide the line in multiple chunks, and make a larger ray-cast for each chunk only, having an horizontal ray-cast of the length of the chunk, and lower than the lowest point in the chunk.

So, if that chunk senses a collider it could check for every point instead of the whole chunk, avoiding to ray-cast every point of the line, you could disable to ray-cast too the points that have collided with the floor. And if neither the chunk ray-cast or the points are colliding, you could disable again the point’s ray-cast of that chunk.

Maybe it would have problems if all chunks collide at the same time, but that’s the first way that comes into my mind, like if it’s the same flat collider, comparing distances of points with the floor instead of ray-casting or just reducing the ray-cast number sacrificing accuracy, but I’m sure there could be more ways to optimize the process.

1 Like