C
C#2y 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
amio2y ago
Do you want flipping or rotation, because they're not the same
Velcer
VelcerOP2y ago
I want flipping. I figured the easiest way was rotation, then offset. The level bounds are demonstrated here
Velcer
VelcerOP2y ago
amio
amio2y 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
VelcerOP2y ago
oh, interesting.
Velcer
VelcerOP2y ago
Thank you @amio
Velcer
VelcerOP2y 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
amio2y ago
Nice! 🙂 You're welcome
Velcer
VelcerOP2y 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
amio2y ago
Sounds like 180, so you can just flip it twice, once along each axis
Velcer
VelcerOP2y ago
ooh Thank you! Got it.
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server