Christian Tapia
Christian Tapia

Reputation: 34166

2D Line reflection on 2 overlapped lines

The situation is this:

enter image description here

My 2D Line collision detect is working well as the reflection does too. But the problem I have is that when a line collides with the vertex of the triangle (that are just 3 2DLine = line(1), line(2), line(3)), it sometimes reflects on the expected line, but sometimes it reflects on the wrong line.

In other words, on the image above: The red line sometimes reflects on the expected line(3) and sometimes on the line(1), and when it reflects on the wrong line (on the image, line 1) it gets inside the triangle and reflects until it collides a vertex.

I know the problem is that lines of the triangle are overlapped, my function checks first if the red line is colliding with the line(1), then if it does with line(2) and finally with line(3), so the red line first reflects on the line(1), but I cannot think in another way to solve this.

Upvotes: 1

Views: 177

Answers (1)

Dan Bechard
Dan Bechard

Reputation: 5291

You can:

(a) Set the new velocity vector to (C - P1), where C is the center of the triangle and P1 is the top vertex. In other words, bounce away from the triangle.

(b) Ignore vertex collisions.

This depends on how accurate you want your simulation to be. For a game, I generally choose to ignore such rare cases rather than writing special code to handle them "correctly". As long as the result looks reasonable, chances are nobody will notice or care.

Upvotes: 1

Related Questions