Пример #1
0
        // line intersection helper function: does line segment (p1,p2) intersect segment (p3,p4) ?
        public Intersect line_intersect(Vec p1, Vec p2, Vec p3, Vec p4)
        {
            Intersect result = new Intersect()
            {
                intersect = false
            };

            var denom = (p4.y - p3.y) * (p2.x - p1.x) - (p4.x - p3.x) * (p2.y - p1.y);

            if (denom == 0.0)
            {
                result.intersect = false;
            }                                               // parallel lines

            var ua = ((p4.x - p3.x) * (p1.y - p3.y) - (p4.y - p3.y) * (p1.x - p3.x)) / denom;
            var ub = ((p2.x - p1.x) * (p1.y - p3.y) - (p2.y - p1.y) * (p1.x - p3.x)) / denom;

            if (ua > 0.0 && ua < 1.0 && ub > 0.0 && ub < 1.0)
            {
                var up = new Vec(p1.x + ua * (p2.x - p1.x), p1.y + ua * (p2.y - p1.y));
                return(new Intersect {
                    ua = ua, ub = ub, up = up, intersect = true
                });                                                                   // up is intersection point
            }
            return(result);
        }
Пример #2
0
        //private void util_add_box(List<Wall> lst, double x, double y, double w, double h) {
        //    lst.Add(new Wall(new Vec(x, y), new Vec(x + w, y)));
        //    lst.Add(new Wall(new Vec(x + w, y), new Vec(x + w, y + h)));
        //    lst.Add(new Wall(new Vec(x + w, y + h), new Vec(x, y + h)));
        //    lst.Add(new Wall(new Vec(x, y + h), new Vec(x, y)));
        //}

        // helper function to get closest colliding walls/items
        public Intersect stuff_collide_(Vec p1, Vec p2, bool check_walls, bool check_items)
        {
            Intersect minres = new Intersect()
            {
                intersect = false
            };

            // collide with walls
            if (check_walls)
            {
                for (int i = 0, n = this.walls.Count; i < n; i++)
                {
                    Wall      wall = this.walls[i];
                    Intersect res  = line_intersect(p1, p2, wall.p1, wall.p2);
                    if (res.intersect)
                    {
                        res.type = 0; // 0 is wall
                        if (!minres.intersect)
                        {
                            minres = res;
                        }
                        else       // check if its closer
                        {
                            if (res.ua < minres.ua)
                            {
                                // if yes replace it
                                minres = res;
                            }
                        }
                    }
                }
            }

            // collide with items
            //if (check_items) {
            //    for (int i = 0, n = this.items.Count; i < n; i++) {
            //        Item it = this.items[i];
            //        Intersect res = line_point_intersect(p1, p2, it.p, it.rad);
            //        if (res.intersect) {
            //            res.type = it.type; // store type of item
            //            if (!minres.intersect) { minres = res; } else {   // check if its closer
            //                if (res.ua < minres.ua) {
            //                    // if yes replace it
            //                    minres = res;
            //                }
            //            }
            //        }
            //    }
            //}

            return(minres);
        }
Пример #3
0
        public Intersect  line_point_intersect(Vec A, Vec B, Vec C, double rad)
        {
            Intersect result = new Intersect {
                intersect = false
            };

            var v = new Vec(B.y - A.y, -(B.x - A.x)); // perpendicular vector
            var d = Math.Abs((B.x - A.x) * (A.y - C.y) - (A.x - C.x) * (B.y - A.y));

            d = d / v.length();
            if (d > rad)
            {
                return(result);
            }

            v.normalize();
            v.scale(d);
            double ua = 0.0;
            var    up = C.add(v);

            if (Math.Abs(B.x - A.x) > Math.Abs(B.y - A.y))
            {
                ua = (up.x - A.x) / (B.x - A.x);
            }
            else
            {
                ua = (up.y - A.y) / (B.y - A.y);
            }
            if (ua > 0.0 && ua < 1.0)
            {
                result = new Intersect {
                    ua = ua, up = up, intersect = true
                };
            }
            return(result);
        }