Author Topic: NGUI Bounds to Screen Space  (Read 3401 times)

OmegaQueen

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
NGUI Bounds to Screen Space
« on: November 13, 2014, 01:31:40 AM »
Hi, first time posting

I'm trying to get an NGUI object (and it's combined children 's widgets) bounds from world (or local) space to screen space, so that I can then test if an NGUI object is overlapping with another on the screen, using something like:

  1. if(screenSpaceBounds[i].bounds.Intersects(screenSpaceBounds[j].bounds)

So to get the bounds I'm using:

  1. Bounds labelBounds = NGUIMath.CalculateRelativeWidgetBounds(nextLabel.transform, false);

Then passing those bounds to a custom function to create screen space bounds:

  1. Bounds WorldBoundsToSceenBounds(Bounds worldBounds)
  2.         {
  3.                 //Calculate the forward facing rectangle points of the label
  4.                 Vector3 bottomLeft = worldBounds.center + new Vector3(-worldBounds.extents.x, -worldBounds.extents.y, worldBounds.extents.z);
  5.                 Vector3 topLeft = worldBounds.center + new Vector3(-worldBounds.extents.x, worldBounds.extents.y, worldBounds.extents.z);
  6.                 Vector3 bottomRight = worldBounds.center + new Vector3(worldBounds.extents.x, -worldBounds.extents.y, worldBounds.extents.z);
  7.                 Vector3 topRight = worldBounds.center + new Vector3(worldBounds.extents.x, worldBounds.extents.y, worldBounds.extents.z);
  8.  
  9.                 //Convert each point to screen space
  10.                 bottomLeft = player.MainCamera.camera.WorldToScreenPoint(bottomLeft);
  11.                 topLeft = player.MainCamera.camera.WorldToScreenPoint(topLeft);
  12.                 bottomRight = player.MainCamera.camera.WorldToScreenPoint(bottomRight);
  13.                 topRight = player.MainCamera.camera.WorldToScreenPoint(topRight);
  14.                 Vector3 center = player.MainCamera.camera.WorldToScreenPoint(worldBounds.center);
  15.  
  16.                 //Calculate the extends
  17.                 float extendsX = Vector3.Distance(bottomLeft, bottomRight) / 2.0f;
  18.                 float extendsY = Vector3.Distance(bottomLeft, topLeft) / 2.0f;
  19.                 float extendsZ = 0.0001f;
  20.  
  21.                 Vector3 extends = new Vector3(extendsX, extendsY, extendsZ);
  22.  
  23.                 //Return the screen bounds
  24.                 return new Bounds(center, extends);
  25.         }

The problem is that there is never a collision detected between the screen space bounds I'm generating. I'm not sure if this is the correct way to do it or if NGUI has this implemented already.

Is the screen space bounds idea/code alright? Is there something else I should do?

Any help would be greatly appreciated.

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI Bounds to Screen Space
« Reply #1 on: November 14, 2014, 04:47:22 AM »
Widgets are 2D. Bounds are 3D. You should be using rectangles (UnityEngine.Rect) instead of bounds.

Also... widget.worldCorners gives you the 4 corners of a widget. You can then transform those to screen space.