Ejemplo n.º 1
0
        /// <summary>
        /// Inspector - Finds the collision points between an arc extrapolated to be distance long (the PlanetariaRaycastHit structs have no particular order)
        /// </summary>
        /// <param name="arc">A fragment that defines the arc in space (might not be fully used or return collisions after the end of the arc).</param>
        /// <param name="distance">The distance to raycast (may be greater than or less than the length of the arc - or negative).</param>
        /// <param name="layer_mask">The collision mask that defines which objects will be ignored.</param>
        /// <returns>All of the collision points of the Raycast (listed exactly once).</returns>
        private static PlanetariaRaycastHit[] unordered_raycast_all(Arc arc, float distance, int layer_mask, bool collide_with_fields)
        {
            //float angle = arc.angle(); // this would determine the intersections for the un-modified arc (ignoring distance)

            // desired_angle = desired_length * (partial_angle/partial_length) i.e. length * length_to_angle ratio
            float desired_angle = distance * (arc.angle() / arc.length()); // TODO: verify negative distances go backwards

            desired_angle = Mathf.Clamp(desired_angle, -2 * Mathf.PI, 2 * Mathf.PI);

            // primative arc points
            Vector3 arc_left   = arc.position(-arc.angle() / 2);
            Vector3 arc_center = arc.position(-arc.angle() / 2 + desired_angle / 2);
            Vector3 arc_right  = arc.position(-arc.angle() / 2 + desired_angle);

            SerializedArc ray_arc = ArcFactory.curve(arc_left, arc_center, arc_right);

            PlanetariaShape ray_shape = PlanetariaShape.Create(new List <SerializedArc> {
                ray_arc
            }, false);

            // composites
            Vector3 arc_boundary_midpoint = (arc_left + arc_right) / 2;                      // if the arc is like a wooden bow, this is the midpoint of the string
            Vector3 arc_forward           = (arc_center - arc_boundary_midpoint).normalized; // the direction a hypothetical arrow would travel
            Vector3 arc_up = arc.floor().normal;                                             // orthogonal/perpendicular to the imaginary "bow"

            // UnityEngine.Physics.OverlapBox() requirements
            // FIXME: OPTIMIZE: half_extents currently provides unnecessary false positives because the "width" of plane (the depth into the distance and zero height are fine)
            Vector3    half_extents = new Vector3(1, 0, 1);                    // The largest collision "box" for a unit sphere is a radius of 1 in the x-z plane; height along y is 0.
            Vector3    center       = arc_boundary_midpoint + arc_forward * 1; // The center of the "box" must be offset 1 (the radius) along the forward axis from the two arc boundaries.
            Quaternion rotation     = Quaternion.LookRotation(arc_forward, arc_up);

            // SphereColliders (only) that represent potential collisions (not guaranteed).
            Collider[] colliders = Physics.OverlapBox(center, half_extents, rotation, layer_mask, QueryTriggerInteraction.Collide); // TODO: verify this casts properly
            List <PlanetariaRaycastHit> raycast_hits = new List <PlanetariaRaycastHit>();

            Debug.Log(colliders.Length);
            foreach (SphereCollider sphere_collider in colliders)
            {
                PlanetariaCollider planetaria_collider = PlanetariaCache.collider_fetch(sphere_collider);
                if (planetaria_collider.is_field && !collide_with_fields)
                {
                    Debug.LogError("Why?");
                    continue;
                }
                Quaternion geometry_rotation = planetaria_collider.gameObject.internal_game_object.transform.rotation;
                Debug.Log("Found a collider with " + planetaria_collider.shape.Length + " arcs.");
                foreach (Arc geometry_arc in ray_shape.block_collision(planetaria_collider.shape, geometry_rotation))
                {
                    Vector3[] intersections = PlanetariaIntersection.raycast_intersection(arc, geometry_arc, distance, geometry_rotation); // TODO: verify distance is indeed the angle in this scenario
                    Debug.Log("Found an arc with " + intersections.Length + " intersections.");
                    foreach (Vector3 intersection in intersections)
                    {
                        PlanetariaRaycastHit single_collision = PlanetariaRaycastHit.hit(arc, planetaria_collider, geometry_arc, intersection, distance);
                        raycast_hits.Add(single_collision);
                    }
                }
            }
            return(raycast_hits.ToArray());
        }
Ejemplo n.º 2
0
        private static Vector3 intersection(float longitude, float latitude)                                      // FIXME: Rect canvas
        {
            Arc     x_boundary = ArcFactory.curve(Vector3.down, equator_longitude(longitude), Vector3.up);        // full-circle
            Arc     y_boundary = ArcFactory.curve(Vector3.left, prime_meridian_latitude(latitude), Vector3.left); // semi-circle
            Vector3 corner     = PlanetariaIntersection.arc_arc_intersection(x_boundary, y_boundary, 0).data;

            return(corner);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Mutator - find all intersections along x=0 or z=0 arcs in southern hemisphere.
 /// </summary>
 /// <param name="block">The block (set of arcs) to be inspected.</param>
 private static void find_discontinuities(PlanetariaCollider collider)
 {
     foreach (Arc arc in collider.shape.arcs)
     {
         for (int dimension = 0; dimension < 2; ++dimension) // Intersect already gets quadrants 3-4 by proxy
         {
             float     angle         = (Mathf.PI / 2) * dimension;
             Vector3   begin         = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle));
             Vector3   end           = Vector3.down;
             Vector3[] intersections = PlanetariaIntersection.arc_path_intersections(arc, begin, end, 0);
             add_intersections(arc, intersections);
         }
     }
 }
Ejemplo n.º 4
0
        public bool is_self_intersecting()
        {
            List <Arc> edges = generate_edges();

            for (int left = 0; left < edges.Count; ++left)
            {
                for (int right = left + 1; right < edges.Count; ++right)
                {
                    optional <Vector3> intersection = PlanetariaIntersection.arc_arc_intersection(edges[left], edges[right], 0);
                    if (intersection.exists)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 5
0
        private void initialize()
        {
            arc_angle = arc_visitor.arc.angle();

            float floor_length   = arc_visitor.arc.length();           // edge case
            float ceiling_length = arc_visitor.arc.length(2 * offset); // corner case

            arc_length = Mathf.Max(floor_length, ceiling_length);      // use longer distance to make movement feel consistent

            left_angle_boundary = -arc_angle / 2;
            if (concave(arc_visitor[-1], offset)) // set left boundary
            {
                optional <Vector3> intersection = PlanetariaIntersection.arc_arc_intersection(arc_visitor[0], arc_visitor[-2], offset);
                left_angle_boundary = arc_visitor.arc.position_to_angle(intersection.data);
            }

            right_angle_boundary = +arc_angle / 2;
            if (concave(arc_visitor[+1], offset)) // set right boundary
            {
                optional <Vector3> intersection = PlanetariaIntersection.arc_arc_intersection(arc_visitor[0], arc_visitor[+2], offset);
                right_angle_boundary = arc_visitor.arc.position_to_angle(intersection.data);
            }
        }
Ejemplo n.º 6
0
        public static optional <BlockCollision> block_collision(CollisionObserver observer, Arc arc, PlanetariaCollider collider, PlanetariaTransform transformation, PlanetariaRigidbody rigidbody)
        {
            optional <ArcVisitor> arc_visitor = collider.shape.arc_visitor(arc);

            if (!arc_visitor.exists)
            {
                Debug.LogError("This should never happen");
                return(new optional <BlockCollision>());
            }

            Quaternion block_to_world = collider.gameObject.internal_game_object.transform.rotation;
            Quaternion world_to_block = Quaternion.Inverse(block_to_world);

            Vector3 last_position    = world_to_block * rigidbody.get_previous_position();
            Vector3 current_position = world_to_block * rigidbody.get_position();

            float extrusion = transformation.scale / 2;
            optional <Vector3> intersection_point = PlanetariaIntersection.arc_path_intersection(arc, last_position, current_position, extrusion);

            if (!intersection_point.exists) // theoretically only happens with moving objects for discrete collision checks
            {
                // these functions are general inverses of one another, but also serve to constrain/normalize the position to the arc path.
                float intersection_angle = arc.position_to_angle(current_position);
                if (Mathf.Abs(intersection_angle) <= arc.angle() / 2)      // if the intersection is valid
                {
                    intersection_point = arc.position(intersection_angle); // set the collision to the extruded collision point
                }
            }
            if (!intersection_point.exists)
            {
                Debug.LogError("Research why this happened.");
                return(new optional <BlockCollision>());
            }
            BlockCollision result = new BlockCollision();
            float          angle  = arc.position_to_angle(intersection_point.data);

            result.geometry_visitor = ShapeVisitor.geometry_visitor(arc_visitor.data, angle, extrusion, collider.gameObject.internal_game_object.transform);
            intersection_point.data = block_to_world * intersection_point.data;
            result.distance         = Vector3.Angle(intersection_point.data, rigidbody.get_previous_position()) * Mathf.Deg2Rad;
            result.overshoot        = Vector3.Angle(intersection_point.data, rigidbody.get_position()) * Mathf.Deg2Rad;
            result.observer         = observer;
            result.self             = observer.collider();
            result.other            = collider;

            PlanetariaPhysicMaterial self  = result.self.material;
            PlanetariaPhysicMaterial other = result.other.material;

            result.elasticity = PlanetariaPhysics.blend(
                self.elasticity, self.elasticity_combine,
                other.elasticity, other.elasticity_combine);

            result.friction = PlanetariaPhysics.blend(
                self.friction, self.friction_combine,
                other.friction, other.friction_combine);

            result.magnetism =
                -(self.magnetism - other.magnetism * self.induced_magnetism_multiplier) *
                (other.magnetism - self.magnetism * other.induced_magnetism_multiplier);

            return(result);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Inspector (Cache Mutator) - Updates the cache so that spherical rectangle calculations avoid recomputing old values.
        /// </summary>
        /// <param name="canvas">A Rect (measuring radians) representing the start and stop angles relative to Quaternion.identity. X/Y Range: (-2PI, +2PI).</param>
        public static void cache_spherical_rectangle(Rect canvas)
        {
            if (cached_canvas != canvas)
            {
                Vector3 lower_left   = intersection(canvas.xMin, canvas.yMin);
                Vector3 lower_center = intersection(canvas.center.x, canvas.yMin);
                Vector3 lower_right  = intersection(canvas.xMax, canvas.yMin);

                Vector3 middle_left   = intersection(canvas.xMin, canvas.center.y);
                Vector3 middle_center = intersection(canvas.center.x, canvas.center.y);
                Vector3 middle_right  = intersection(canvas.xMax, canvas.center.y);

                Vector3 upper_left   = intersection(canvas.xMin, canvas.yMax);
                Vector3 upper_center = intersection(canvas.center.x, canvas.yMax);
                Vector3 upper_right  = intersection(canvas.xMax, canvas.yMax);

                Arc biangle_segment1 = ArcFactory.curve(upper_center, upper_right, -upper_center);
                Arc biangle_segment2 = ArcFactory.curve(lower_center, lower_right, -lower_center);
                cached_left_biangle_focus      = PlanetariaIntersection.arc_arc_intersection(biangle_segment1, biangle_segment2, 0).data;
                cached_left_positive_partition = Bearing.attractor(cached_left_biangle_focus, middle_left); // used for a dot product to determine if the angle applied for UV is +/-
                if (Vector3.Dot(cached_left_positive_partition, middle_center) >= 0)
                {
                    cached_left_start_angle = Vector3.Angle(cached_left_biangle_focus, middle_center) * Mathf.Deg2Rad;
                    cached_left_end_angle   = Vector3.Angle(cached_left_biangle_focus, middle_left) * Mathf.Deg2Rad;
                }
                else
                {
                    cached_left_start_angle = Vector3.Angle(-cached_left_biangle_focus, middle_center) * Mathf.Deg2Rad + Mathf.PI;
                    cached_left_end_angle   = Vector3.Angle(-cached_left_biangle_focus, middle_left) * Mathf.Deg2Rad + Mathf.PI;
                }

                biangle_segment1                = ArcFactory.curve(upper_center, upper_left, -upper_center);
                biangle_segment2                = ArcFactory.curve(lower_center, lower_left, -lower_center);
                cached_right_biangle_focus      = PlanetariaIntersection.arc_arc_intersection(biangle_segment1, biangle_segment2, 0).data;
                cached_right_positive_partition = Bearing.attractor(cached_right_biangle_focus, middle_right); // used for a dot product to determine if the angle applied for UV is +/-
                if (Vector3.Dot(cached_right_positive_partition, middle_center) >= 0)
                {
                    cached_right_start_angle = Vector3.Angle(cached_right_biangle_focus, middle_center) * Mathf.Deg2Rad;
                    cached_right_end_angle   = Vector3.Angle(cached_right_biangle_focus, middle_right) * Mathf.Deg2Rad;
                }
                else
                {
                    cached_right_start_angle = Vector3.Angle(-cached_right_biangle_focus, middle_center) * Mathf.Deg2Rad + Mathf.PI;
                    cached_right_end_angle   = Vector3.Angle(-cached_right_biangle_focus, middle_right) * Mathf.Deg2Rad + Mathf.PI;
                }

                biangle_segment1                = ArcFactory.curve(middle_left, upper_left, -middle_left);
                biangle_segment2                = ArcFactory.curve(middle_right, upper_right, -middle_right);
                cached_lower_biangle_focus      = PlanetariaIntersection.arc_arc_intersection(biangle_segment1, biangle_segment2, 0).data;
                cached_lower_positive_partition = Bearing.attractor(cached_lower_biangle_focus, lower_center); // used for a dot product to determine if the angle applied for UV is +/-
                if (Vector3.Dot(cached_lower_positive_partition, middle_center) >= 0)
                {
                    cached_lower_start_angle = Vector3.Angle(cached_lower_biangle_focus, middle_center) * Mathf.Deg2Rad;
                    cached_lower_end_angle   = Vector3.Angle(cached_lower_biangle_focus, lower_center) * Mathf.Deg2Rad;
                }
                else
                {
                    cached_lower_start_angle = Vector3.Angle(-cached_lower_biangle_focus, middle_center) * Mathf.Deg2Rad + Mathf.PI;
                    cached_lower_end_angle   = Vector3.Angle(-cached_lower_biangle_focus, lower_center) * Mathf.Deg2Rad + Mathf.PI;
                }

                biangle_segment1                = ArcFactory.curve(middle_left, lower_left, -middle_left);
                biangle_segment2                = ArcFactory.curve(middle_right, lower_right, -middle_right);
                cached_upper_biangle_focus      = PlanetariaIntersection.arc_arc_intersection(biangle_segment1, biangle_segment2, 0).data;
                cached_upper_positive_partition = Bearing.attractor(cached_upper_biangle_focus, upper_center); // used for a dot product to determine if the angle applied for UV is +/-
                if (Vector3.Dot(cached_upper_positive_partition, middle_center) >= 0)
                {
                    cached_upper_start_angle = Vector3.Angle(cached_upper_biangle_focus, middle_center) * Mathf.Deg2Rad;
                    cached_upper_end_angle   = Vector3.Angle(cached_upper_biangle_focus, upper_center) * Mathf.Deg2Rad;
                }
                else
                {
                    cached_upper_start_angle = Vector3.Angle(-cached_upper_biangle_focus, middle_center) * Mathf.Deg2Rad + Mathf.PI;
                    cached_upper_end_angle   = Vector3.Angle(-cached_upper_biangle_focus, upper_center) * Mathf.Deg2Rad + Mathf.PI;
                }

                cached_north_hemisphere = Bearing.attractor(middle_center, upper_center);
                cached_east_hemisphere  = Bearing.attractor(middle_center, middle_right);

                cached_canvas = canvas;
            }
        }