In my inventory, I add and rotate items. But when I rotate and then add, things go wrong since the pivot hasn't been adjusted.
Adding - (in Item.cs):
public void Notify_HasBeenAdded(Slot toSlot)
{
// stuff
cachedTransform.localPosition = toSlot.cachedTransform.localPosition;
// stuff
}
Rotating:
public void RotateItem(RotationDirection rotDir)
{
// stuff
var tween = TweenRotation.Begin(parent.gameObject, rotAnimTime, parent.localRotation * Quaternion.Euler(0, 0, (rotDir == RotationDirection.Clockwise) ? -90 : 90));
// stuff
}
When I rotate and then add, here's how it looks - See attachment - It was originally topleft, I rotated 90 dgs clkwise, if you look at it, you wouldn't think the pivot is topleft, it would look like as if it was topright, right? but in fact, it's still topleft but rotated.
Using localCorners wouldn't work here cause it relies on getting the offset from the pivot, which is topleft, but looks like topright.
I solved this problem by writing a piece of code that adjusts the pivot according to the rotation.
But I don't wanna use that - it's just not very reusable. So I thought, why not write a method that sets a REAL corner in a widget to a certain position? Like: widget.SetCornerTo(Corners.TopLeft, pos); - That way I could always set the REAL top left corner, without having to worry about rotation, pivots, etc.
I tried, and came up with a formula that says:
newPos = reqPos + (realPivotOffset X realDimensions)
Where:
newPos
: is the position
where the widget will reside
by its real pivot
.reqPos: is the position required to
set a certain corner at
. realPivotOffset: is the dist
from the corner we
're trying to set and the real pivot. realDimensions: is the real dimensions of the widget - taking rotation into consideration.
I'm having trouble getting it to work since I can't seem to find a way to get the realPivotOffset.
realDimensions is easy to find. Originally, the sprite was 150x100 - when I rotated 90 dgs the real dims are 100x150, rotate again back to 150x100, etc. So:
if (widget.localRotation.x % 180 == 90) {
reaDim.width = widget.height;
reaDim.height = widget.width;
} else {
reaDim.width = widget.width;
reaDim.height = widget.height;
}
But just to illustrate, in my attachment example (2nd picture) - (let's set the topleft corner) it would be:
newPos = reqPos + ((1, 0) X (100, 150)) // the gun is actually 150x100, but since it's rotated, it's now 100x150
// the (1, 0) is the dist from the topleft corner, to the real pivot at the topright
newPos = reqPos + (100, 0)
newPos = reqPos + (width, 0)
If it wasn't for rotation, it would be as simple as:
0- cache the original pivot.
1- set the current pivot to the required corner.
2- set the widget's position to the required position.
3- rollback to the original pivot.
I also thought of multiplying the localCorners by a transformation (rotation) matrix - that rotates the corners by the rotation of the item - But no idea how to do that... Not even sure how it would help.
To recap, how can I set a widget's position by its corners regardless of its rotation or original pivot? - Something like widget.SetTopLeft(pos);
Thanks a lot!
PS: Had to write the question twice [very frustrating] since the site logged me out the first time I tried to submit.