2
votes

Well I'm trying to make a circle(with dynamic position) and another circle that SHOULD not pass through the first one --- like in the image below

The reason why I am doing this way is because there can be a lot of other objects on the big one.

circle image

but the problem is that Physics.Raycast is not always detecting my sphere:

broken circle image

the blue lines are raycasts

Basically what i do is raycast through the whole big circle and find out where the smaller one is.

Here is my code for detecting objects on the big circle:

void GetCollisions () {
    Angle FixedRenderAngle, k;
    RaycastHit hit;
    Vector3 p1, p2;
    float d, d1, radius;

    for ( int i = 0; i < Radiuses.Count; i++ ) {
        if ( Angles[i] == null )
            Angles[i] = new List<Angle>();

        radius = Radiuses[i];
        FixedRenderAngle = 360f / (float)NumberOfCalls;
        AnglesLast[i] = 0;
        Angle j = 0f, pj;
        bool exception = false;

        for ( int l = 0; l < NumberOfCalls; l++) {
            j+= FixedRenderAngle;
            pj = j - FixedRenderAngle;
            p1 = pj.PointByRadius ( radius );
            p2 = j.PointByRadius ( radius );
            Debug.DrawRay ( p1, p2 - p1, Color.cyan, 3f );
            if ( Physics.Raycast ( p1, p2 - p1, out hit, Vector3.Distance ( p1, p2 ) ) ) {
                d = Vector3.Distance ( p1, p2 );
                d1 = Vector3.Distance ( p1,hit.point );
                k =  pj + (j -pj) * (d1/d); 

                AddToAngles ( ref k, i );
            }

            if ( Physics.Raycast ( p2, p1 - p2, out hit, Vector3.Distance ( p1, p2 ) ) ) {
                if ( AnglesLast[i] == 0 ) exception = true;

                d = Vector3.Distance ( p1, p2 );
                d1 = Vector3.Distance ( p1,hit.point );
                k =  pj + (j -pj) * (d1/d); 

                AddToAngles ( ref k, i );
            }
        }

        if ( exception == true ) {
            Angle tmp = Angles[i][AnglesLast[i] - 1];
            for ( int l = AnglesLast[i] - 1; l > 0 ; l-- )
                Angles[i][l] = Angles[i][l-1];
            Angles[i][0] = tmp;
        }
    }
}

Angle is a class with float numbers representing degrees(0 to 360) and it's function PointByRadius is returning a Vector3 representing the location where should it be on a circle based on the angle and with a given radius.

Did I do something wrong or there is something that i don't know about raycasts? Any help would be appreciated. (Of course any other method for this would help)

1
is there a reason why you dont want to "ask" the small circle where it is? - Tom
@Tom Basically I can use the circle position to do all this which will only work for regular meshes(sphere. cube, etc...), but it won't work for an irregular mesh. - Vali Rosca
@Kay Could you explain that please? I didn't quite understand what do you mean by that. - Vali Rosca
I mean do really need ray casting? Using the physics engine the trivial way i.e. setting up rigidbodies and colliders for all objects is easier. If you mark the colliders as Is Trigger, you can react whenever the objects collide. - Kay
@Kay Well what I really want here is to hide everything that is inside the circle so I don't see how triggers are helping me here(my camera is orthographic). For example: Atomic+ - something like this. - Vali Rosca

1 Answers

0
votes

Physics.SphereCast? It's somewhat like OverlapSphere but maybe more what you need? (it's kinda hard to tell exactly what you're trying to do though... e.g. when you say circle, do you mean actual circles moving only on X-Y or they moving through Z? which circle are you trying to hide things in, the big or small one? are you trying to visually hide a segment of the larger circle?)

Depending on how fast the objects are moving and sizes, raycasts (and PhysX colliders in general) can get wonky, at least in my experience. You might also try messing with your physics timestep lengths to see if that's part of the problem, like do it more often and see if you still get the problem behavior. Not sure, just throwing ideas out.