Author Topic: "simple" coordinate question  (Read 2379 times)

slumtrimpet

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 3
  • Posts: 13
    • View Profile
"simple" coordinate question
« on: July 23, 2014, 09:15:32 AM »
So I think this question is probably a result of my own basic lack of understanding of the various coordinate systems in 3D programming... but I'll pose it here as my specific application is related to moving around some NGUI widgets.

So, lets say I've got two UISprite rectangles on the screen.  One is on the left hand side, the other is on the right.  I simply want to programatically move the right hand rectangle so it is horizontally centered on the right edge of the first triangle.

Before:
xxxx     
x  x      zzz
x  x      zzz
xxxx


After:
xxxx
x zzz
x zzz
xxxx


So... it seems from various posts I've found that part of the key here is to use NGUIMath.CalculateAbsoluteWidgetBounds to get my widget rectangle coordinates.  Then I think I should be able to derive the target screen point to pass to TweenPosition.Begin on my right hand rectangle.

On button press I'm doing the following:
  1. Bounds lBounds = NGUIMath.CalculateAbsoluteWidgetBounds(leftWidget.transform);
  2. print(lBounds);

This generates the following log output:
  1. Center: (-0.8, 0.0, 0.0), Extents: (0.4, 1.0, 0.0)

So, I'm deducing from this output that we are in some kind of -1:1 coordinate system. (Does this have a name one could Google to get educated?  It's sort of normalized but not, because < 0 so I'm not sure what I'm even looking at here.)  If my assumption is correct, it seems I should be able to figure out the right edge of my left box by taking:


  1. xPos = (center + (width / 2)) * Screen.width / 2
  2. [aka]
  3. xPos = (-0.8 + (0.4 / 2)) * Screen.width / 2

... but I think this is the basic spot where I'm getting off track.

When I:
  1. TweenPosition.Begin(rightWidget.gameObject, 1f, new Vector3(xPos, 0f))
The box moves a bit too far to the left and overshoots it's target.  Any ideas?  Is there any easier way to translate the result of CalculateAbsoluteWidgetBounds into something usable by TweenPosition?

slumtrimpet

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 3
  • Posts: 13
    • View Profile
Re: "simple" coordinate question
« Reply #1 on: July 23, 2014, 11:09:54 AM »
Been banging on this a bit more... I think I had some math wrong in the last code where I was trying to derive the -1:1 coordinate given via NGUI into some form of 'viewport' (which I believe go from [0,0] in bottom left hand corner to [1,1] in top right) or 'screen' coordinates (which I believe go from [0,0] in bottom left corner to [width px,height px] in top right).

I believe this works to 'normalize' the NGUI coordinate, then convert that value into a screen position and a world position. 

  1. Bounds lBounds = NGUIMath.CalculateAbsoluteWidgetBounds(leftPanel.transform);
  2. print(lBounds);
  3.  
  4. float xPoint = (lBounds.center.x + (lBounds.extents.x / 2));
  5. float normalX = (1 + xPoint) / 2;
  6.  
  7. //center vertically:
  8. float yPoint = lBounds.center.y;
  9. float normalY = (1 + yPoint) / 2;
  10.  
  11. Vector3 normalPos = new Vector3(normalX, normalY);
  12. print(normalPos);
  13. Vector3 screenPos = this.core.root.UICamera.ViewportToScreenPoint(normalPos);
  14. print(screenPos);
  15. Vector3 worldPos = this.core.root.UICamera.ScreenToWorldPoint(screenPos);
  16. print(worldPos);
  17.  
  18. TweenPosition.Begin(rightPanel.gameObject, 2f, worldPos)

Unfortunately, throwing normalPos, screenPos, or worldPos at the TweenPosition.Begin method still results in incorrect positioning (although printing the various coordinates I'm deriving seems to result in plausible values... if I'm interpreting everything correctly)... I think at this point I just don't get which of the many various coordinate systems and values TweenPosition is expecting.

slumtrimpet

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 3
  • Posts: 13
    • View Profile
Re: "simple" coordinate question
« Reply #2 on: July 23, 2014, 11:18:54 AM »
Ok... the below actually does work to position my widgets as I had originally described above. 

I have no idea why what I was doing above didn't work though.  (and I'd really like some advice on what I was doing wrong if anyone has a minute to digest all that I wrote... although the below code makes sense to me on a very basic level, it seems all that coordinate nonsense is something I need to understand at some point and obviously don't at all currently...)

  1. Vector3 crap = new Vector3(leftPanel.transform.localPosition.x + leftPanel.width / 2, leftPanel.transform.localPosition.y);
  2. TweenPosition.Begin(rightPanel.gameObject, 2f, crap);

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: "simple" coordinate question
« Reply #3 on: July 23, 2014, 08:04:07 PM »
There was no need to convert coordinates from viewport to screen, etc. Tweens work with local positions by default. You were always trying to pass a world position. That's what you were doing wrong.

Also... I suggest using the layout system's anchoring for something like this. Anchoring one widget to the edge of another is quite trivial to achieve and requires no code.