Ejemplo n.º 1
0
        public void Rotate_90()
        {
            var testInput = new Vector2(12.345f, -67.89f);
            var expected  = new Vector2(67.89f, 12.345f);
            var result    = S2VXUtils.Rotate(testInput, 90);

            Assert.AreEqual(expected.X, result.X, FloatingPointTolerance);
            Assert.AreEqual(expected.Y, result.Y, FloatingPointTolerance);
        }
Ejemplo n.º 2
0
        private static Vector2 CalculateCameraOffset(Vector2 position, float rotation, float scale)
        {
            var closestCoordinate = new Vector2(
                (float)Math.Round(position.X),
                (float)Math.Round(position.Y)
                );
            var offset = S2VXUtils.Rotate(closestCoordinate - position, rotation) * scale;

            return(offset);
        }
Ejemplo n.º 3
0
        protected override void Update()
        {
            if (IsHidden())
            {
                if (InternalChildren.Count != 0)
                {
                    ClearInternal();
                }
                return;
            }

            var camera   = Story.Camera;
            var position = camera.Position;
            var rotation = camera.Rotation;
            var scale    = camera.Scale.X;

            var cameraOffset = CalculateCameraOffset(position, rotation, scale);
            var unitX        = S2VXUtils.Rotate(new Vector2(1, 0), rotation);
            var unitY        = S2VXUtils.Rotate(new Vector2(0, 1), rotation);

            var startDistance     = scale / 2;
            var endDistance       = LineLength / 2;
            var distanceIncrement = scale;
            var lineIndex         = 0;

            for (var distance = startDistance; distance <= endDistance; distance += distanceIncrement)
            {
                var up    = unitY * distance + cameraOffset;
                var down  = -unitY * distance + cameraOffset;
                var right = unitX * distance + cameraOffset;
                var left  = -unitX * distance + cameraOffset;

                UpdateLineProperties(lineIndex++, up, LineLength, Thickness, rotation);
                UpdateLineProperties(lineIndex++, down, LineLength, Thickness, rotation);
                UpdateLineProperties(lineIndex++, right, Thickness, LineLength, rotation);
                UpdateLineProperties(lineIndex++, left, Thickness, LineLength, rotation);
            }

            CheckAndRemoveLines(lineIndex);
        }
Ejemplo n.º 4
0
        private bool IsMouseOnNote(Vector2 mousePos, S2VXNote note)
        {
            mousePos = ToSpaceOfOtherDrawable(ToLocalSpace(mousePos), Editor);

            // DrawPosition is centered at (0,0). I convert it so (0,0) starts top left
            var convertedCenterPoint = new Vector2(note.DrawPosition.X + Editor.DrawWidth / 2, note.DrawPosition.Y + Editor.DrawHeight / 2);
            var topLeft    = new Vector2(convertedCenterPoint.X - note.DrawWidth / 2, convertedCenterPoint.Y - note.DrawHeight / 2);
            var topRight   = new Vector2(convertedCenterPoint.X + note.DrawWidth / 2, convertedCenterPoint.Y - note.DrawHeight / 2);
            var bottomLeft = new Vector2(convertedCenterPoint.X - note.DrawWidth / 2, convertedCenterPoint.Y + note.DrawHeight / 2);

            // https://gamedev.stackexchange.com/a/110233
            var pointA = S2VXUtils.Rotate(new Vector2(topLeft.X - convertedCenterPoint.X, topLeft.Y - convertedCenterPoint.Y), note.Rotation);
            var pointB = S2VXUtils.Rotate(new Vector2(topRight.X - convertedCenterPoint.X, topRight.Y - convertedCenterPoint.Y), note.Rotation);
            var pointC = S2VXUtils.Rotate(new Vector2(bottomLeft.X - convertedCenterPoint.X, bottomLeft.Y - convertedCenterPoint.Y), note.Rotation);

            // Shift origin back after rotating
            pointA = new Vector2(pointA.X + convertedCenterPoint.X, pointA.Y + convertedCenterPoint.Y);
            pointB = new Vector2(pointB.X + convertedCenterPoint.X, pointB.Y + convertedCenterPoint.Y);
            pointC = new Vector2(pointC.X + convertedCenterPoint.X, pointC.Y + convertedCenterPoint.Y);

            var v0 = pointC - pointA;
            var v1 = pointB - pointA;
            var v2 = mousePos - pointA;

            var dot00 = Vector2.Dot(v0, v0);
            var dot01 = Vector2.Dot(v0, v1);
            var dot02 = Vector2.Dot(v0, v2);
            var dot11 = Vector2.Dot(v1, v1);
            var dot12 = Vector2.Dot(v1, v2);

            var invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
            var u        = (dot11 * dot02 - dot01 * dot12) * invDenom;
            var v        = (dot00 * dot12 - dot01 * dot02) * invDenom;

            return(u >= 0 && v >= 0 && u <= 1 && v <= 1);
        }