C
C#17mo ago
Velcer

❔ 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 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);
}
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);
}
12 Replies
amio
amio17mo ago
Do you want flipping or rotation, because they're not the same
Velcer
Velcer17mo ago
I want flipping. I figured the easiest way was rotation, then offset. The level bounds are demonstrated here
Velcer
Velcer17mo ago
amio
amio17mo ago
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
Velcer17mo ago
oh, interesting.
Velcer
Velcer17mo ago
Thank you @amio
Velcer
Velcer17mo ago
I did not realize the solution was so simple.
var pos = boardObject.Position;
var distanceToEdge = _levelBounds.max.y - pos.y;

pos.y = (int)distanceToEdge;
var pos = boardObject.Position;
var distanceToEdge = _levelBounds.max.y - pos.y;

pos.y = (int)distanceToEdge;
Thank you.
amio
amio17mo ago
Nice! 🙂 You're welcome
Velcer
Velcer17mo ago
@amio 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. so what I do need is rotation, and not mirroring.
amio
amio17mo ago
Sounds like 180, so you can just flip it twice, once along each axis
Velcer
Velcer17mo ago
ooh Thank you! Got it.
Accord
Accord17mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.