コード例 #1
0
        // Check raycast collision

        public override RaycastHit CheckRay(Vector2 start, Vector2 direction)
        {
            Vector2[] points = GetPoints();

            Vector2 end = start + direction;

            LineHit closest = PhysicsUtils.CheckLineCollision(points[3], points[0], start, end);

            for (int i = 0; i <= 2; i++)
            {
                Vector2 p1 = points[i];
                Vector2 p2 = points[i + 1];

                LineHit hit = PhysicsUtils.CheckLineCollision(p1, p2, start, end);

                if (hit.hit)
                {
                    if (!closest.hit || Vector2.Distance(hit.hitPoint, start) < Vector2.Distance(closest.hitPoint, start))
                    {
                        closest = hit;
                    }
                }
            }

            if (!closest.hit)
            {
                return(null);
            }

            RaycastHit raycastHit = new RaycastHit(closest.hitPoint, entity, Vector2.Distance(closest.hitPoint, start));

            return(raycastHit);
        }
コード例 #2
0
 public PolyLineCreator(int id, int defaultLength, Brush stroke, double strokeThickness, LineHit lineHit, ShapeSerializer shapeSerializer) : base(shapeSerializer)
 {
     this.id              = id;
     this.defaultLength   = defaultLength;
     this.stroke          = stroke;
     this.strokeThickness = strokeThickness;
     this.lineHit         = lineHit;
     exampleFactory       = new Lazy <PolyLineObject>(() => new PolyLineObject(id, new Point(0, 0), new Point(0, 0), Brushes.Transparent, 0, lineHit));
 }
コード例 #3
0
        public RectangleCreator(int id, int defaultLength, int defaultHeight, Brush stroke, double strokeThickness, Brush fill, LineHit lineHit, ShapeSerializer shapeSerializer) : base(shapeSerializer)
        {
            this.id              = id;
            this.defaultLength   = defaultLength;
            this.defaultHeight   = defaultHeight;
            this.stroke          = stroke;
            this.strokeThickness = strokeThickness;
            this.fill            = fill;
            this.lineHit         = lineHit;

            exampleFactory = new Lazy <RectangleObject>(() => new RectangleObject(id, new Point(0, 0), new Point(0, 0), Brushes.Transparent, 0, Brushes.Transparent, lineHit));
        }
コード例 #4
0
ファイル: Collider.cs プロジェクト: SeelPatel/SealEngine
        // Find the hitpoints of two colliding polygons  via Line collision

        public Vector2[] FindPolygonHitpoints(Line[] poly1, Line[] poly2)
        {
            List <Vector2> collisionPoints = new List <Vector2>();

            foreach (Line line1 in poly1)
            {
                foreach (Line line2 in poly2)
                {
                    LineHit lineHit = PhysicsUtils.CheckLineCollision(line1.p1, line1.p2, line2.p1, line2.p2);

                    if (lineHit.hit)
                    {
                        collisionPoints.Add(lineHit.hitPoint);
                    }
                }
            }

            return(collisionPoints.ToArray());
        }
コード例 #5
0
        // Check for the collision of two lines using cramers rule
        public static LineHit CheckLineCollision(Vector2 p1, Vector2 p2, Vector2 q1, Vector2 q2)
        {
            Vector2 a = p2 - p1;
            Vector2 b = q2 - q1;

            Vector2 c = q1 - p1;

            // Check if parallel;

            LineHit linehit = new LineHit();

            linehit.hitPoint = Vector2.Zero;
            linehit.hit      = false;

            if (SealMath.Approximately(a.X / b.X, a.Y / b.Y))
            {
                return(linehit);
            }

            // Use Cramers Rule to solve

            float D = (a.X * b.Y) - (a.Y * b.X);

            float D_x = (c.X * b.Y) - (c.Y * b.X);

            float D_y = (a.X * c.Y) - (a.Y * c.X);

            float x = D_x / D;

            float y = D_y / D;


            if ((0 <= x && x <= 1) && (0 <= y && y <= 1))
            {
                linehit.hitPoint = p1 + x * a;
                linehit.hit      = true;
            }

            return(linehit);
        }
コード例 #6
0
 public FreeRectangleBehaviour(RectangleState state, LineHit lineHit) : base(state)
 {
     this.lineHit = lineHit;
 }
コード例 #7
0
 public PointPolyLineBehaviour(PoliLineState lineState, LineHit lineHit) : base(lineState)
 {
     this.lineHit = lineHit;
 }
コード例 #8
0
        public PolyLineObject(int id, System.Windows.Point start, System.Windows.Point end, Brush stroke, double strokeThickness, LineHit lineHit)
        {
            Freeze();

            ShapeId = id;

            state = new PoliLineState {
                StrokeThickness = strokeThickness, Points = new List <System.Windows.Point> {
                    start, end
                }
            };

            freeBehaviour    = new FreePoliLineBehaviour(state, lineHit);
            currentBehaviour = freeBehaviour;

            behaviours = new List <ShapeBehaviour <PoliLineState> > {
                new PointPolyLineBehaviour(state, lineHit),
                new SelectPolyLineBehaviour(state),
                freeBehaviour
            };

            Shape  = new DrawingVisual();
            Stroke = stroke;

            Unfreeze();
        }
コード例 #9
0
        public RectangleObject(int id, Point leftTop, Point rightBottom, Brush stroke, double strokeThickness, Brush fill, LineHit lineHit)
        {
            Freeze();

            ShapeId = id;

            var rightTop   = new Point(rightBottom.X, leftTop.Y);
            var leftBottom = new Point(leftTop.X, rightBottom.Y);

            state = new RectangleState {
                TopLeft = leftTop, TopRight = rightTop, BottomRight = rightBottom, BottomLeft = leftBottom, StrokeThickness = strokeThickness
            };

            freeBehaviour = new FreeRectangleBehaviour(state, lineHit);

            behaviours = new List <ShapeBehaviour <RectangleState> > {
                new ResizeRectangleBehaviour(state, lineHit),
                new SelectRectangleBehaviour(state),
                freeBehaviour,
            };

            currentBehaviour = freeBehaviour;

            Shape = new DrawingVisual();

            Stroke = stroke;
            Fill   = fill;

            Unfreeze();
        }
コード例 #10
0
 public FreePoliLineBehaviour(PoliLineState lineState, LineHit lineHit) : base(lineState)
 {
     this.lineHit = lineHit;
 }
コード例 #11
0
 public ResizeRectangleBehaviour(RectangleState shapeState, LineHit lineHit) : base(shapeState)
 {
     this.lineHit = lineHit;
 }
コード例 #12
0
        public void Start()
        {
            var host = new Host();

            var lineHit = new LineHit();

            var propCache = new PropertyCache();

            var shapeSerializer = new ShapeSerializer(propCache);

            var sceneStore = new JsonSceneStore();

            var rotateParam = new RotateParam {
                Angle = 0
            };
            var rotateView = new IntInputView {
                DataContext = new RotateViewModel(rotateParam)
            };

            var thiknessParam = new ThiknessParam {
                Thikness = 5
            };
            var thiknessView = new IntInputView {
                DataContext = new ThiknessViewModel(thiknessParam)
            };

            var colorParam = new ColorParam {
                Color = Colors.Black
            };
            var colorView = new ColorView {
                DataContext = new ColorViewModel <IShapeObject>(colorParam)
            };

            var fillParam = new FillParam {
                Color = Colors.Transparent
            };
            var fillView = new ColorView {
                DataContext = new ColorViewModel <IFillObject>(fillParam)
            };


            var vm = new HostViewModel(
                host.Canvas,

                new List <ShapeToolParam> {
                new ShapeToolParam("Поворот", rotateView, rotateParam),
                new ShapeToolParam("Толщина", thiknessView, thiknessParam),
                new ShapeToolParam("Цвет", colorView, colorParam),
                new ShapeToolParam("Заливка", fillView, fillParam),
            },

                sceneStore,

                new ShapeTool(0, LoadImage("pack://application:,,,/Painter;component/Resources/Images/line.png"),
                              new PolyLineCreator(0, 50, Brushes.Black, 10, lineHit, shapeSerializer)),
                new ShapeTool(1, LoadImage("pack://application:,,,/Painter;component/Resources/Images/rectangle.png"),
                              new RectangleCreator(1, 50, 50, Brushes.Black, 10, Brushes.Transparent, lineHit, shapeSerializer))
                );

            host.DataContext = vm;


            App.Current.MainWindow = host;
            host.Show();
        }