# [SOLVED] Need help with my custom throw system

My raycast goes through all divisions of the curve. How do I prevent from going to the next division if something has already been hit? Maybe that’s the problem.

Edit: Yes, that was the problem. It’s working now.

@Albertos actually, that is not the only problem. I have re-created the setup you use. At least in a sense, where you use the Bezier curves for ray hits.

The problem occures at the joints of the curve segments. Say, you have a curve with only 2 segments: AB and BC. The problem you describe, where the curve goes through the obstacle in your case is when point B is exactly at the surface of the obstacle object.

When that happens, the ray you cast from A to B does not detect the hit. And also the ray from B to C will not detect it, resulting in a pass through.

I remember you have raised this issue in the past and I think one of the proposals that time was to increase the number of segments in your cuve. However, given the root cause of the problem, you are actaully making it worse by adding more joints, that may end up on the surface. Also, increasing the precision or the number of segments on the curve, increases the number of ray cast iterations you would have to doeach frame to detect a hit. This might be expensive.

It is a valid problem though, so after some thought, I came up with a solution I believe could help you. Let’s get back to example with only to 2 segments: AB and BC. So, the problem happens when B is right at the surface of the obstacle. In order to mitigate it, I create another segment - from the middle of AB to the middle of BC. Now, when we check the hit, we first check the AB segment, then BC. If both don’t give a result - we check the middle segment between them.

In this case, if B is at the surface and rays fail to detect a hit, the middle section will hit definitely, since B will be in the middle of it. This way you will not end up with free joints at the surface - one of the segments will guarantee to hit. I have made an example project to demonstrate it:

https://playcanvas.com/editor/scene/934492

1 Like

Hay @LeXXik, a quick response as I have to go to work.

Thank you very much for your input!

I now use two beziers. One for the raycast, which now consists of 3 segments because more segments indeed make it less stable. The raycast hit then determines the position of point 3 of the second bezier, which consists of more segments for a smooth line.

You may be able to optimize these scripts.

https://playcanvas.com/editor/scene/908116

Yes, your option will work, because you have only 3 segments. This means each segment is long enough, so most probably the last segment will hit the surface in the middle of it, and you would adjust the surface end point.

However, with this approach an issue might come in the precision of your throws - for example, try to move the cursor on top of one of your boxes. You can see how the target point is jumping around unpredictably. This is due to the low precision of your curve.

In my approach the curve has 20 segments and very stable in detection, without jitter. You should consider it.

I hate glitching things! Only when I took a quick look at your project, I see that the line didn’t stop at the hit point of the obstacle. Can this be adjusted?

Yes, it is possible. You can do it by making a projection from the point of intersection with the surface to the curve. Once you did the projection you know the normalized position of the cut. Using this position you can split the original curve into a new one.

I have adjusted the example to demonstrate it.

Hay @LeXXik! Thanks again for your detailed explanation and example project. I try to translate your example into my game but unfortunately I will not get there. You use a different way of coding than I am used to. Also, I see that the obstacles you use in the example are pre-processed in the script, and I don’t think I can do that for all obstacles in my game.

Yes, the coding styles are different, but the technique actually matches the way you calculate the hit. Perhaps, it would be easier, if you could give me write access to that small project and reset it to the state before you tried to implement my solution.

I can adjust a few lines to show how it can be done.

Just wanted to update on the topic. After implementing the ability to cast convex shapes, I have changed the `.raycastFirst()` method to `.sphereCast()`. This eliminated the need in checking secondary segments. Also, switched to native `pc.Curve()` to do the same thing without a need in third party bezier library.