Example #1
0
    public override float ReturnOutput()
    {
        //Raycasting begins, draw a ray from emitter to the receiver
        Ray ray = new Ray(Emitter.transform.position, Emitter.transform.forward);

        BulletSharp.Math.Vector3 fromUltra  = ray.origin.ToBullet();
        BulletSharp.Math.Vector3 toCollider = ray.GetPoint(10).ToBullet();

        Vector3 toColliderUnity = toCollider.ToUnity();

        //Callback returns all hit point results in order to avoid non colliders interfere with the ray test
        AllHitsRayResultCallback raysCallback = new AllHitsRayResultCallback(fromUltra, toCollider);

        //Retrieves bullet physics world and does a ray test with the given coordinates and updates the callback object
        BPhysicsWorld world = BPhysicsWorld.Get();

        world.world.RayTest(fromUltra, toCollider, raysCallback);
        List <BulletSharp.Math.Vector3> colliderPositions = raysCallback.HitPointWorld;

        BulletSharp.Math.Vector3 colliderPosition = BulletSharp.Math.Vector3.Zero;

        float distanceToCollider = 0;

        //Set the initial distance as the distance between emitter and receiver
        if (main != null && main.IsMetric)
        {
            distanceToCollider = sensorOffset;
        }
        else
        {
            distanceToCollider = AuxFunctions.ToFeet(sensorOffset);
        }

        //Loop through all hitpoints (exclude the origin), if there is at least one hitpoint less than the distance between two sensors,
        //something should block the beam between emitter and receiver
        foreach (BulletSharp.Math.Vector3 pos in colliderPositions)
        {
            if ((pos - fromUltra).Length < distanceToCollider && !pos.Equals(BulletSharp.Math.Vector3.Zero))
            {
                distanceToCollider = (pos - fromUltra).Length;
                colliderPosition   = pos;
            }
        }
        //Again if the line connects to the middle of the field nothing is blocking the beam
        Debug.DrawLine(fromUltra.ToUnity(), colliderPosition.ToUnity(), Color.blue);

        if (distanceToCollider < sensorOffset)
        {
            //Something is there
            state = "Broken";
            return(1);
        }
        else
        {
            //Nothing in between
            state = "Unbroken";
            return(0);
        }
    }
Example #2
0
    //Step #2
    public override float ReturnOutput()
    {
        //Raycasting begins
        Ray ray = new Ray(gameObject.transform.position, transform.forward);

        BulletSharp.Math.Vector3 fromUltra  = ray.origin.ToBullet();
        BulletSharp.Math.Vector3 toCollider = ray.GetPoint(MaxRange).ToBullet();

        Vector3 toColliderUnity = toCollider.ToUnity();

        //Callback returns all hit point results in order to avoid non colliders interfere with the ray test
        AllHitsRayResultCallback raysCallback = new AllHitsRayResultCallback(fromUltra, toCollider);

        //Retrieves bullet physics world and does a ray test with the given coordinates and updates the callback object
        BPhysicsWorld world = BPhysicsWorld.Get();

        world.world.RayTest(fromUltra, toCollider, raysCallback);

        //Gets the position of all hit points of the ray test
        List <BulletSharp.Math.Vector3> colliderPositions = raysCallback.HitPointWorld;

        BulletSharp.Math.Vector3 colliderPosition = BulletSharp.Math.Vector3.Zero;

        float distanceToCollider = MaxRange;

        //Loop through all hit points and get the shortest distance, exclude the origin since it is also counted as a hit point
        foreach (BulletSharp.Math.Vector3 pos in colliderPositions)
        {
            if ((pos - fromUltra).Length <= MaxRange && (pos - fromUltra).Length < distanceToCollider && !pos.Equals(BulletSharp.Math.Vector3.Zero))
            {
                distanceToCollider = (pos - fromUltra).Length;
                colliderPosition   = pos;
            }
        }

        //Draw a line to view the ray action
        //When the ray links to the middle of the field, it means the sensor is out of range
        Debug.DrawLine(fromUltra.ToUnity(), colliderPosition.ToUnity(), Color.green, 5f);


        //setting shortest distance of a collider to the maxRange, then if any colliders are closer to the sensor,
        //their distanceToCollider value becomes the new shortest distance
        float shortestDistance = MaxRange;

        if (!isMetric)
        {
            distanceToCollider = AuxFunctions.ToFeet(distanceToCollider);
        }
        //A check that might be useful in the future if use a bundle of rays instead of a single ray
        if (distanceToCollider < shortestDistance)
        {
            shortestDistance = distanceToCollider;
        }

        return(shortestDistance);
    }
Example #3
0
    //Step #2
    public override float ReturnOutput()
    {
        //Raycasting begins
        Ray ray = new Ray(gameObject.transform.position, transform.forward);

        BulletSharp.Math.Vector3 fromUltra  = ray.origin.ToBullet();
        BulletSharp.Math.Vector3 toCollider = ray.GetPoint(MaxRange).ToBullet();

        Vector3 toColliderUnity = toCollider.ToUnity();

        //Callback returns all hit point results in order to avoid non colliders interfere with the ray test
        AllHitsRayResultCallback raysCallback = new AllHitsRayResultCallback(fromUltra, toCollider);

        //Retrieves bullet physics world and does a ray test with the given coordinates and updates the callback object
        BPhysicsWorld world = BPhysicsWorld.Get();

        world.world.RayTest(fromUltra, toCollider, raysCallback);

        //Gets the position of all hit points of the ray test
        List <BulletSharp.Math.Vector3> colliderPositions = raysCallback.HitPointWorld;

        BulletSharp.Math.Vector3 colliderPosition = BulletSharp.Math.Vector3.Zero;

        float distanceToCollider = MaxRange;

        if (main != null && main.IsMetric)
        {
            distanceToCollider = MaxRange;
            foreach (BulletSharp.Math.Vector3 pos in colliderPositions)
            {
                if ((pos - fromUltra).Length < MaxRange && !pos.Equals(BulletSharp.Math.Vector3.Zero))
                {
                    distanceToCollider = (pos - fromUltra).Length;
                    colliderPosition   = pos;
                }
            }
        }
        else
        {
            distanceToCollider = AuxFunctions.ToFeet(MaxRange);
            foreach (BulletSharp.Math.Vector3 pos in colliderPositions)
            {
                if (AuxFunctions.ToFeet((pos - fromUltra).Length) < distanceToCollider && !pos.Equals(BulletSharp.Math.Vector3.Zero))
                {
                    distanceToCollider = AuxFunctions.ToFeet((pos - fromUltra).Length);
                    colliderPosition   = pos;
                }
            }
        }

        //Draw a line to view the ray action
        //When the ray links to the middle of the field, it means the sensor is out of range
        Debug.DrawLine(fromUltra.ToUnity(), colliderPosition.ToUnity(), Color.green, 5f);

        return(distanceToCollider);
    }
Example #4
0
 public override float GetSensorRange()
 {
     if (main.IsMetric)
     {
         return(MaxRange);
     }
     else
     {
         return(AuxFunctions.ToFeet(MaxRange));
     }
 }