C#
C#

help

Root Question Message

Velcer
Velcer3/11/2023
❔ Flip Board not working properly

Hello, I'm working on a level editor for my game. My board is not flipping properly.
I've attached a video.
Here is my code:
        public void FlipBoard() 
        {
            foreach (var boardObject in _placedObjects)
            {
                if (boardObject.buildable.Category == LevelEditorCategory.BoardTile || boardObject.buildable.Category == LevelEditorCategory.Piece || boardObject.buildable.Category == LevelEditorCategory.ActionTile)
                {
                    var pos = boardObject.Position;
                    var newRotation = new Vector2(pos.x, pos.y);


                    newRotation = newRotation.Rotate(180);
                    newRotation.x += _levelBounds.max.x;
                    newRotation.y += _levelBounds.max.y;
                    



                    boardObject.transform.position = newRotation;
                    boardObject.Position = new Vector2Int((int)newRotation.x, (int)newRotation.y);
                }

            }


            isFlipped = !isFlipped;
        }



    public static Vector2 Rotate(this Vector2 v, float degrees)
    {
        return v.RotateRadians(degrees * Mathf.Deg2Rad);
    }

    public static Vector2 RotateRadians(this Vector2 v, float radians)
    {
        var ca = Mathf.Cos(radians);
        var sa = Mathf.Sin(radians);
        return new Vector2(ca * v.x - sa * v.y, sa * v.x + ca * v.y);
    }
amio
amio3/11/2023
Do you want flipping or rotation, because they're not the same
Velcer
Velcer3/11/2023
I want flipping.
Velcer
Velcer3/11/2023
I figured the easiest way was rotation, then offset.
Velcer
Velcer3/11/2023
The level bounds are demonstrated here
Velcer
Velcer3/11/2023
amio
amio3/11/2023
Yes, but rotating something is just different. Flipping something along an axis means it's distance from one "edge" is now its distance from the opposite one
Velcer
Velcer3/11/2023
oh, interesting.
Velcer
Velcer3/11/2023
Thank you @160854339135602689
Velcer
Velcer3/11/2023
I did not realize the solution was so simple.
                    var pos = boardObject.Position;
                    var distanceToEdge = _levelBounds.max.y - pos.y;

                    pos.y = (int)distanceToEdge;

Thank you.
amio
amio3/11/2023
Nice! 🙂 You're welcome
Velcer
Velcer3/11/2023
@160854339135602689 I realize just now, that I am trying to make the effect of two people on two sides of the board, and they decide to rotate it.
Velcer
Velcer3/11/2023
so what I do need is rotation, and not mirroring.
amio
amio3/11/2023
Sounds like 180, so you can just flip it twice, once along each axis
Velcer
Velcer3/11/2023
ooh
Velcer
Velcer3/11/2023
Thank you! Got it.
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy