예제 #1
0
    /// <summary>
    /// Handles movements and collisions on a constant basis.
    /// </summary>
    void FixedUpdate()
    {
        //Calculate where the object should move this frame
        Vector3 newpos = transform.position + transform.rotation * Vector3.forward * (speed * Time.deltaTime);

        //Collisions with the real World. As the object moves, collisions checks are made each frame at the next position.
        Vector3 collisionpoint;
        Vector3 collisionnormal;

        //First, test the primary ZED. Collisions will look the most accurate if calculated from this one.
        bool primaryhit = ZEDSupportFunctions.HitTestOnRay(zedManager.zedCamera, cam, newpos, transform.rotation, Vector3.Distance(transform.position, newpos),
                                                           distanceBetweenRayChecks, out collisionpoint, false, realWorldThickness);

        if (primaryhit)
        {
            //Call the function to resolve the impact. This allows us to easily override what happens on collisions with classes that inherit this one.
            ZEDSupportFunctions.GetNormalAtWorldLocation(zedManager.zedCamera, collisionpoint, sl.REFERENCE_FRAME.WORLD, cam, out collisionnormal);

            OnHitRealWorld(collisionpoint, collisionnormal);
        }

        if (!primaryhit && testCollisionsUsingAllZEDs) //If set to true, test the rest of the ZEDs as well.
        {
            foreach (ZEDManager manager in ZEDManager.GetInstances())
            {
                if (manager == zedManager)
                {
                    continue;                        //If it's the primary ZED, skip as we've already tested that one.
                }
                if (ZEDSupportFunctions.HitTestOnRay(manager.zedCamera, manager.GetMainCamera(), newpos, transform.rotation, Vector3.Distance(transform.position, newpos), distanceBetweenRayChecks, out collisionpoint, false, realWorldThickness))
                {
                    //Call the function to resolve the impact. This allows us to easily override what happens on collisions with classes that inherit this one.
                    ZEDSupportFunctions.GetNormalAtWorldLocation(manager.zedCamera, collisionpoint, sl.REFERENCE_FRAME.WORLD, manager.GetMainCamera(), out collisionnormal);

                    OnHitRealWorld(collisionpoint, collisionnormal);
                    break; //No need to test the rest of the ZEDs.
                }
            }
        }

        //Collisions with virtual objects
        //Cast a ray to check collisions between here and the intended move point for virtual objects.
        RaycastHit hitinfo;

        if (Physics.Raycast(transform.position, newpos - transform.position, out hitinfo, Vector3.Distance(transform.position, newpos)))
        {
            //Call the function to resolve the impact. This allows us to easily override what happens on collisions with classes that inherit this one.
            OnHitVirtualWorld(hitinfo);
        }

        //Move it to this new place
        transform.position = newpos;

        //Tick down its lifespan and check if we should destroy it.
        lifespan -= Time.deltaTime;
        if (lifespan <= 0f)
        {
            Destroy(gameObject);
        }
    }
예제 #2
0
    /// <summary>
    /// Tests the depth of both the real and virtual in the center of the screen, and returns the world position of the closest one.
    /// </summary>
    /// <param name="crosshairpoint">Where the crosshair should be rendered.</param>
    /// <param name="collisionnormal">The normal vector of the surface aimed at, for rotating the crosshair accordingly if desired.</param>
    /// <returns>False if there is no valid object, real or virtual, on which to place the crosshair. </returns>
    private bool FindCrosshairPosition(out Vector3 crosshairpoint, out Vector3 collisionnormal)
    {
        //Find the distance to the real world. The bool will be false if there is an error reading the depth at the center of the screen.
        Vector3 realpoint;
        bool    foundrealdistance = ZEDSupportFunctions.HitTestOnRay(leftcamera, laserPointerBeadHolder.transform.position, laserPointerBeadHolder.transform.rotation, 5f, 0.01f, out realpoint);
        float   realdistance      = Vector3.Distance(laserPointerBeadHolder.transform.position, realpoint);

        //Find the distance to the virtual. The bool will be false if there are no colliders ahead of you.
        RaycastHit hitinfo;
        bool       foundvirtualdistance = Physics.Raycast(laserPointerBeadHolder.transform.position, laserPointerBeadHolder.transform.rotation * Vector3.forward, out hitinfo);

        //If we didn't find either, return false so the laser and bead can be disabled.
        if (!foundrealdistance && !foundvirtualdistance)
        {
            crosshairpoint  = Vector3.zero;
            collisionnormal = Vector3.zero;
            return(false);
        }

        //Decide if we use the real or virtual distance
        if (!foundvirtualdistance || realdistance < hitinfo.distance)
        {
            //The real world is closer. Give the position of the real world pixel and return true.
            crosshairpoint = realpoint;
            ZEDSupportFunctions.GetNormalAtWorldLocation(realpoint, sl.REFERENCE_FRAME.WORLD, leftcamera, out collisionnormal);
            return(true);
        }
        else
        {
            //The virtual world is closer, or they're tied. Return the world posiiton where the raycast hit the virtual collider.
            crosshairpoint  = hitinfo.point;
            collisionnormal = hitinfo.normal;
            return(true);
        }
    }
    /// <summary>
    /// Checks if another ArUcoDrone is in front of the drone and within range.
    /// Will also check if there's a real object in the way, if checkRealWorldObstaclesBeforeShooting is true.
    /// </summary>
    /// <returns>True if there's a valid target in front of the drone.</returns>
    private bool IsAimingAtTarget()
    {
        //First make sure there's a valid virtual target in front of the drone.
        Ray ray = new Ray(shootAnchorObject.position, shootAnchorObject.rotation * Vector3.forward);

        RaycastHit[] hits = Physics.RaycastAll(ray, shootRangeMeters);

        bool  foundvirtualtarget = false;
        float nearestdist        = Mathf.Infinity;

        foreach (RaycastHit hit in hits)
        {
            ArUcoDrone otherdrone = hit.transform.GetComponent <ArUcoDrone>();

            if (otherdrone != null && otherdrone != this)
            {
                foundvirtualtarget = true;
                if (hit.distance < nearestdist)
                {
                    nearestdist = hit.distance;
                }
            }
        }

        if (!foundvirtualtarget)
        {
            return(false);
        }

        if (checkRealWorldObstaclesBeforeShooting) //Make sure there's not a real-world obstacle in the way of the target.
        {
            //If there is one, check to make sure there's not a real-world object in the way.
            Vector3 collisionpoint; //Not used but required for HitTestOnRay function.
            foreach (ZEDManager manager in ZEDManager.GetInstances())
            {
                bool hitreal = ZEDSupportFunctions.HitTestOnRay(manager.zedCamera, manager.GetLeftCamera(), shootAnchorObject.transform.position, shootAnchorObject.transform.rotation,
                                                                nearestdist, 0.01f, out collisionpoint, false, 0.1f);

                if (hitreal)
                {
                    return(false);
                }
            }

            return(true);
        }
        else
        {
            return(true); //We're not checking against the real world, and we already found a virtual object, so fire.
        }
    }
예제 #4
0
    /// <summary>
    /// Tests the depth of the real world based on the pointer origin position and rotation.
    /// Returns the world position if it collided with anything.
    /// </summary>
    /// <param name="pointerbeadpoint">The world space position where the pointer is pointing.</param>
    /// <returns>True if a valid real world point was found.</returns>
    bool FindPointerPosition(out Vector3 pointerbeadpoint)
    {
        //Find the distance to the real world. The bool will be false if there is an error reading the depth at the center of the screen.
        Vector3 realpoint;
        bool    foundrealdistance = ZEDSupportFunctions.HitTestOnRay(leftcamera, rayOrigin.position, rayOrigin.rotation, 5.0f, 0.05f, out realpoint);

        //If we didn't find, return false so the laser and bead can be disabled.
        if (!foundrealdistance)
        {
            pointerbeadpoint = Vector3.zero;
            return(false);
        }
        else //Output the world position of the collision.
        {
            pointerbeadpoint = realpoint;
            return(true);
        }
    }
예제 #5
0
    /// <summary>
    /// Tests the depth of the real world based on the pointer origin position and rotation.
    /// Returns the world position if it collided with anything.
    /// </summary>
    /// <param name="pointerbeadpoint">The world space position where the pointer is pointing.</param>
    /// <returns>True if a valid real world point was found.</returns>
    bool FindPointerPosition(out Vector3 pointerbeadpoint)
    {
        //Find the distance to the real world. The bool will be false if there is an error reading the depth at the center of the screen.
        Vector3 realpoint;

        foreach (ZEDManager manager in ZEDManager.GetInstances()) //Check all cameras, in case it's hitting something the main ZEDManager can't see.
        {
            if (ZEDSupportFunctions.HitTestOnRay(zedManager.zedCamera, leftcamera, rayOrigin.position, rayOrigin.rotation, 5.0f, 0.05f, out realpoint))
            {
                pointerbeadpoint = realpoint;
                return(true); //No need to check the other cameras.
            }
        }

        //No camera was able to see a collision.
        pointerbeadpoint = Vector3.zero;
        return(false);
    }
예제 #6
0
    /// <summary>
    /// Handles movements and collisions on a constant basis.
    /// </summary>
    void FixedUpdate()
    {
        //Calculate where the object should move this frame
        Vector3 newpos = transform.position + transform.rotation * Vector3.forward * (Speed * Time.deltaTime);

        //Collisions with the real World. As the object moves, collisions checks are made each frame at the next position.
        Vector3 collisionpoint;

        if (ZEDSupportFunctions.HitTestOnRay(_leftCamera, newpos, transform.rotation, Vector3.Distance(transform.position, newpos), DistanceBetweenRayChecks, out collisionpoint, false, RealWorldThickness))
        {
            //Call the function to resolve the impact. This allows us to easily override what happens on collisions with classes that inherit this one.
            Vector3 collisionnormal;
            ZEDSupportFunctions.GetNormalAtWorldLocation(collisionpoint, sl.REFERENCE_FRAME.WORLD, _leftCamera, out collisionnormal);

            OnHitRealWorld(collisionpoint, collisionnormal);
        }

        //Collisions with virtual objects
        //Cast a ray to check collisions between here and the intended move point for virtual objects.
        RaycastHit hitinfo;

        if (Physics.Raycast(transform.position, newpos - transform.position, out hitinfo, Vector3.Distance(transform.position, newpos)))
        {
            //Call the function to resolve the impact. This allows us to easily override what happens on collisions with classes that inherit this one.
            OnHitVirtualWorld(hitinfo);
        }

        //Move it to this new place
        transform.position = newpos;

        //Tick down its lifespan and check if we should destroy it.
        Lifespan -= Time.deltaTime;
        if (Lifespan <= 0f)
        {
            Destroy(gameObject);
        }
    }