Exemplo n.º 1
0
        public override Intersection Intersect(Vector2 point)
        {
            if (!this.WithinRadius(point) ) { return null; }

            // the exclusion test is the same as the collision test

            Intersection sect = new Intersection();
            sect.surface_normal = Utility.Angle(point - pos);
            sect.position = point;
            return sect;
        }
Exemplo n.º 2
0
        // Not totally stoaked with this implementation
        public Intersection CalculateIntersection(Vector2 point)
        {
            Intersection sect = new Intersection();
            sect.position = point;

            // first shift the point into the reference frame of the hitbox
            point = Utility.Rotate(point - pos, -angle);

            float min_dist = float.MaxValue;
            int best_segment = 0;

            // look through every segment, finding the one closest to the point
            for (int i = 0; i < count; i++)
            {
                Vector2 p1 = corners[i];
                Vector2 p2 = (i == count-1) ? corners[0] : corners[i+1];
                Vector2 pd = p2 - p1;

                float t = Utility.Clamp( Vector2.Dot( point - p1, pd) / pd.LengthSquared() , 0.0f, 1.0f );
                Vector2 projection = p1 + (t * pd);

                float dist_sq = Vector2.DistanceSquared(projection, point);

                if (dist_sq < min_dist)
                {
                    min_dist = dist_sq;
                    best_segment = i;
                }
            }
            
            // return the normal, and compensate for the hitbox rotation
            sect.surface_normal = Utility.WrapAngle(segment_normals[best_segment] + angle);
            sect.overlap = min_dist;
            return sect;
        }
Exemplo n.º 3
0
        public Intersection IntersectCircle(HitboxCircle hitbox)
        {
            // first shift the circle origin into the reference frame of the hitbox
            Vector2 point = Utility.Rotate(hitbox.pos - pos, -angle);


            for (int i = 0; i < count; i++)
            {
                // for each point we test if it falls into the circle
                if (  Vector2.DistanceSquared(point, corners[i]) < hitbox.radius_sq )
                {
                    // If one point falls in, we run with it.
                    Intersection sect = new Intersection();

                    // we take the corner, and shift it into global coordinate frame
                    sect.position = pos + Utility.Rotate(corners[i], angle);

                    // the surface normal is calculated like it is for a circle
                    sect.surface_normal = Utility.Angle(pos - hitbox.pos);

                    // Like a circle, again
                    sect.overlap = (hitbox.radius - (sect.position - hitbox.pos).Length()) * 2;

                    return sect;
                }
            }

            return null;
        }
Exemplo n.º 4
0
        public override Intersection Intersect(Hitbox hitbox)
        {
            if (!this.WithinRadius(hitbox)) { return null; }
            
            if (hitbox is HitboxCircle)
            {
                // if both hitboxes are circles, then the ContainsExclusion test
                // is the same as the collision test.
                // So a collision has occurred

                Intersection sect = new Intersection();

                sect.position = ((hitbox.pos * radius) + (pos * hitbox.radius)) / (radius + hitbox.radius);
                sect.surface_normal = Utility.Angle(pos - hitbox.pos);
                sect.overlap = (radius - (sect.position - pos).Length())*2;

                return sect;
            }
            
            if ( hitbox is HitboxPolygon )
            {
                // YEA. DO THAT.
                return ((HitboxPolygon)hitbox).IntersectCircle(this);
            }

            return null;
        }