Exemplo n.º 1
0
        public RayHitResult HitWithClick(RayCastInfo clickInfo)
        {
            var globalRay     = clickInfo.GlobalRay;
            var sphere        = getSphere(master);
            var segmentLength = (sphere.Center - globalRay.Point).Length() * 3;
            var segment       = new LineSegment3(globalRay.Point, globalRay.Point + globalRay.Direction * segmentLength);

            sphere.Intersect(segment, out var p1, out var p2);

            if (!p1.HasValue)
            {
                return(RayHitResult.Failure());
            }
            var d1 = (p1.Value - globalRay.Point).Length();

            Vector3 globalPoint;
            float   distance;

            if (p2.HasValue)
            {
                var d2   = (p2.Value - globalRay.Point).Length();
                var use1 = (d1 <= d2) ^ inverted;
                if (use1)
                {
                    globalPoint = p1.Value;
                    distance    = d1;
                }
                else
                {
                    globalPoint = p2.Value;
                    distance    = d2;
                }
            }
            else
            {
                globalPoint = p1.Value;
                distance    = d1;
            }

            // todo: consider rotation
            var localPoint = (globalPoint - sphere.Center) / sphere.Radius;

            return(new RayHitResult
            {
                Successful = true,
                Node = Node,
                Distance = distance,
                GlobalHitPoint = globalPoint,
                LocalHitPoint = localPoint,
                IntTag = intTag,
                StrTag = strTag
            });
        }
        public RayHitResult HitWithClick(RayCastInfo clickInfo)
        {
            var hitResult = hittable.HitWithClick(clickInfo);
            var cRect     = GetRectComponent();

            // todo: make overridable and override in more specific components
            if (cRect.DragByBorders &&
                (1 - hitResult.LocalHitPoint.X.Abs()) * cRect.Rectangle.HalfWidth * RichTextBox.DefaultPixelScaling > RichTextBox.DraggablePaddingInPixels &&
                (1 - hitResult.LocalHitPoint.Y.Abs()) * cRect.Rectangle.HalfHeight * RichTextBox.DefaultPixelScaling > RichTextBox.DraggablePaddingInPixels)
            {
                return(RayHitResult.Failure());
            }
            return(hitResult);
        }
Exemplo n.º 3
0
        public RayHitResult HitWithClick(RayCastInfo clickInfo)
        {
            var globalRay = clickInfo.GlobalRay;
            var globalTransformInverse = (planeTransform * Node.GlobalTransform).Invert();
            var localRay = globalRay * globalTransformInverse;

            var t = -localRay.Point.Z / localRay.Direction.Z;

            if (t <= 0f)
            {
                return(RayHitResult.Failure());
            }

            var point     = localRay.Point + localRay.Direction * t;
            var rectangle = getLocalRectangle(master);

            if (!rectangle.ContainsPoint(point.Xy))
            {
                return(RayHitResult.Failure());
            }

            var globalT     = t / globalTransformInverse.Scale;
            var offset      = getHitDistanceOffset(master);
            var globalPoint = globalRay.Point + globalRay.Direction * globalT;
            var localPoint  = new Vector3(
                (point.X - rectangle.Center.X) / rectangle.HalfWidth,
                (point.Y - rectangle.Center.Y) / rectangle.HalfHeight,
                0);

            return(new RayHitResult
            {
                Successful = true,
                Node = Node,
                Distance = globalT + offset,
                GlobalHitPoint = globalPoint,
                LocalHitPoint = localPoint,
                IntTag = intTag,
                StrTag = strTag
            });
        }
Exemplo n.º 4
0
        public bool TryHandleInputEvent(IInputEvent eventArgs)
        {
            if (!(eventArgs is MouseEvent args))
            {
                return(false);
            }
            if (!args.IsLeftClickEvent())
            {
                return(false);
            }
            var clickInfo = new RayCastInfo(args.Viewport, args.Viewport.View.Layers.Single(), args.State.Position);
            var hitResult = rayHitIndex.CastRay(clickInfo).FirstOrNull() ?? RayHitResult.Failure();

            if (hitResult.Successful)
            {
                int?to = null;
                var gizmoComponentTo = hitResult.Node.GetComponent <StoryFlowchartNodeGizmoComponent>();
                if (gizmoComponentTo != null && gizmoComponentTo.ReferencedNode.Id != from)
                {
                    to = gizmoComponentTo.ReferencedNode.Id;
                }

                if (storyService.GlobalGraph.NodeIds.Contains(hitResult.Node.Id))
                {
                    to = hitResult.Node.Id;
                }

                if (to.HasValue)
                {
                    var child = StoryOperations.AddChild(storyService, commonNodeFactory, to.Value, this);
                    storyService.AddEdge(from, child.Id);
                    toolService.CurrentTool = null;
                    return(true);
                }
            }
            toolService.CurrentTool = null;
            return(false);
        }
Exemplo n.º 5
0
        public RayHitResult HitWithClick(RayCastInfo clickInfo)
        {
            var globalRay = clickInfo.GlobalRay;
            var globalTransformInverse = (planeTransform * Node.GlobalTransform).Invert();
            var localRay = globalRay * globalTransformInverse;

            var t = -localRay.Point.Z / localRay.Direction.Z;

            if (t <= 0f)
            {
                return(RayHitResult.Failure());
            }

            var point  = localRay.Point + localRay.Direction * t;
            var cirlce = getLocalCirlce(master);

            if (!cirlce.Contains(point.Xy))
            {
                return(RayHitResult.Failure());
            }

            var globalT     = t / globalTransformInverse.Scale;
            var offset      = getHitDistanceOffset(master);
            var globalPoint = globalRay.Point + globalRay.Direction * globalT;
            // todo: consider rotation
            var localPoint = new Vector3(point.Xy - cirlce.Center, 0) / cirlce.Radius;

            return(new RayHitResult
            {
                Successful = true,
                Node = Node,
                Distance = globalT + offset,
                GlobalHitPoint = globalPoint,
                LocalHitPoint = localPoint,
                IntTag = intTag,
                StrTag = strTag
            });
        }
Exemplo n.º 6
0
        public RayHitResult HitWithClick(RayCastInfo clickInfo)
        {
            // todo: calculate in screen space?
            var line      = getLine(master);
            var mouseLine = clickInfo.GlobalRay.ToLine();

            Line3.GetClosestPoints(line, mouseLine, out var p1, out var p2);
            var grad      = clickInfo.GetAbsPixelGradientAt((p1 + p2) / 2);
            var diff      = (p2 - p1).Abs();
            var pixelDiff = Vector3.Dot(diff, grad);

            return(pixelDiff <= lineWidth
                ? new RayHitResult
            {
                Successful = true,
                Node = Node,
                Distance = (clickInfo.GlobalRay.Point - p1).Length(),
                GlobalHitPoint = p1,
                LocalHitPoint = p1,
                IntTag = intTag,
                StrTag = strTag
            }
                : RayHitResult.Failure());
        }
Exemplo n.º 7
0
        public override void Update(GameUpdateEventArgs e)
        {
            // don't move stationary objects
            if (this.velocity == Vector2.Zero)
            {
                return;
            }

            var step = this.velocity * e.ElapsedTimeF;

            Vector2 lastStepDir = Vector2.Zero;

            // update position (tests collision recursively)
            for (int i = 0; i < 5; i++)
            {
                var rayResult = new RayHitResult(false, 1, this.position - this.tileCenter + step, Vector2.Zero);
                while (true)
                {
                    var result = this.tile.Info.ShootRay(new Ray(this.position - this.tileCenter, step));

                    if (result.RayFactor < rayResult.RayFactor)
                    {
                        rayResult = result;
                    }

                    var point = rayResult.Point + this.tileCenter;

                    var switchedTile = this.updateTile(rayResult.Point);

                    if (!switchedTile)
                    {
                        break;
                    }

                    rayResult = rayResult.WithNewPoint(point - this.tileCenter);
                }

                lastStepDir = this.tileCenter + rayResult.Point - this.position;

                this.position = this.tileCenter + rayResult.Point;

                if (!rayResult.Hit)
                {
                    break;
                }


                this.position += rayResult.Normal * 0.01f;

                var projected = rayResult.Normal * Vector2.Dot(rayResult.Normal, step);

                step -= projected;
            }

            this.velocity = lastStepDir.LengthSquared > 0 ?
                            this.velocity.Length * lastStepDir.Normalized() : Vector2.Zero;

            float slowDownFactor = 1 - this.frictionCoefficient * e.ElapsedTimeF;

            this.velocity *= slowDownFactor < 0 ? 0 : slowDownFactor;
        }
Exemplo n.º 8
0
 public RayHitResult HitWithClick(RayCastInfo clickInfo) =>
 RayHitResult.Failure();
Exemplo n.º 9
0
 // Hittable
 public RayHitResult HitWithClick(RayCastInfo clickInfo) => hittable?.HitWithClick(clickInfo) ?? RayHitResult.Failure();