Author Topic: Screen coordinates of a box collider  (Read 6463 times)

StridingDragon

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 53
    • View Profile
Screen coordinates of a box collider
« on: June 11, 2013, 05:35:14 PM »
I am still struggling with this and can't seem to get this right. I need the on-screen coordinates of a box collider attached to a gameObject.
Here is the code I'm currently using, but for some reason it seems to be constantly off by 20 pixels or so.

  1.         Vector3 bPos = gameObject.transform.localPosition;                                                                      // Box position
  2.                
  3.         float bcCenterX = gameObject.GetComponent<BoxCollider>().center.x * gameObject.transform.localScale.x;          // Calculate adjusted center offset
  4.         float bcCenterY = gameObject.GetComponent<BoxCollider>().center.y * gameObject.transform.localScale.y;
  5.                
  6.         float bcWidth = gameObject.GetComponent<BoxCollider>().size.x * gameObject.transform.localScale.x;                      // Calculate adjusted dimensions
  7.         float bcHeight = gameObject.GetComponent<BoxCollider>().size.y * gameObject.transform.localScale.y;
  8.  
  9.         float colliderX = bPos.x + bcCenterX - ( bcWidth / 2 );
  10.         float colliderY = bPos.y + bcCenterY - ( bcHeight / 2 );
  11.  
  12.         Rect collider = new Rect( colliderX, colliderY, bcWidth, bcHeight );
  13.  

Could anyone tell me what I am doing wrong here? It seems so straight-forward but for some reason my resulting rectangle is just not correct.

zenmaster

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 20
    • View Profile
Re: Screen coordinates of a box collider
« Reply #1 on: June 11, 2013, 10:22:18 PM »
Hi.

I have circular gauges where you can click on their faces to set values.
I still have problems translating in some gauges, but this is a code snipet for converting xy to polar coordinates.
  1.                 print( "GaugeFaceGO.transform.localPosition: " + GaugeFaceGO.transform.localPosition );
  2.                 //Vector3 screenCoordinateCenter = camera.WorldToScreenPoint( DepthGaugeGO.transform.localPosition );
  3.                 Vector3 screenCoordinateCenter = uiCamera.WorldToScreenPoint( UICamera.lastHit.collider.transform.position );
  4.                 print ( "screenCoordinateCenter: " + screenCoordinateCenter );
  5.                
  6.                 float xRelative = UICamera.lastTouchPosition.x - screenCoordinateCenter.x;
  7.                 float yRelative = UICamera.lastTouchPosition.y - screenCoordinateCenter.y;
  8.                 print ( "onClick() " +  "xRelative: " + xRelative  + "yRelative: " + yRelative );
  9.                
  10.                 float myDepth = convertXYtoDepth( xRelative, yRelative );
  11.  
  12.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Screen coordinates of a box collider
« Reply #2 on: June 12, 2013, 03:57:48 AM »
You can make your code significantly faster by simply caching the result of your property queries like so:
  1. BoxCollider col = collider as BoxCollider;
  2. Vector3 center = col.center;
  3. Vector3 localScale = transform.localScale;
  4. float bcCenterX = center.x * localScale.x;
  5. float bcCenterY = center.y * localScale.y;
  6. ...etc
...not to mention cleaner, shorter, and easier to read.

Aside from that, as zenmaster mentioned, you can't simply use local coordinates and expect them to be in screen space. You need to convert from local to world, then from world to screen space.

StridingDragon

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 53
    • View Profile
Re: Screen coordinates of a box collider
« Reply #3 on: June 13, 2013, 08:32:25 PM »
The "big" code was there because I kept working on this, rewriting it a million times, trying to get it to work. in the end, yes, of course, caching the look-ups will be part of the code. But first I need to get it to work - and I can't.

I tried this world to screen etc, but all I'm getting are fantasy values. So, perhaps it is time for me to rephrase my original question.…

How can I get the on-screen coordinates of a scaled object's collider as a rectangle?
« Last Edit: June 14, 2013, 07:35:34 PM by StridingDragon »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Screen coordinates of a box collider
« Reply #4 on: June 14, 2013, 08:39:26 PM »
NGUIMath.CalculateAbsoluteWidgetBounds does it to calculate the final world-space bounds of a group of widgets underneath the specified transform. You can use it as a starting point for your own function if you need it to work for colliders.

StridingDragon

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 53
    • View Profile
Re: Screen coordinates of a box collider
« Reply #5 on: June 15, 2013, 02:10:03 AM »
Thanks for trying to help but in all honesty, I have absolutely no idea what NGUIMath.CalculateAbsoluteWidgetBounds does. I look at the code and I have no clue what is happening there, let away which of these lines of code would relate to transforming a position to screen coordinates.

Isn't there some standard API routine or a few lines of code that take an object, get its collider box and translate the position to screen coordinates? With all this code and all the tests I've been running trying to get this to work, I feel like I am completely in No-Man's land here, because nothing I do yields any results that even remotely resemble the correct screen coordinates.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Screen coordinates of a box collider
« Reply #6 on: June 15, 2013, 06:51:51 AM »
No, there are no built-in functions, which is why I wrote NGUIMath.CalculateAbsoluteWidgetBounds. :P

Unfortunately I can't really explain it better than that. It's just math.

StridingDragon

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 53
    • View Profile
Re: Screen coordinates of a box collider
« Reply #7 on: June 15, 2013, 05:37:08 PM »
Finally got it to work. :-)  Thanks for the input, Michael.