Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: SquigglyFrog on January 05, 2016, 11:31:53 PM

Title: Using 2d polygon colliders on a 2D UI, how to determine if they intersect?
Post by: SquigglyFrog on January 05, 2016, 11:31:53 PM
Ok, working on creating one of those lovely spinning bonus wheels. So I have created a simple wheel, dividing into 8 sections. Each section has a ui2dsprite set up with a 2d polygon collider on it. I have another ui2dsprite that sits on top, the ticker if you will, also set up with a collider. My problem is for the last several hours I've been trying and failing to figure out how to determine if one of those sections intersects with the ticker. All of the normal bounds.intersects and all seem to return nothing. I tried multiple ways of checking, some are below. The raycast actually indicates a hit, but then always says hit is null..

  1.  
  2. Ray ray = Camera.main.ScreenPointToRay(mousePosition);
  3. RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
  4. if (hit == null)
  5. {
  6.        Debug.LogError("No hit");
  7. }
  8. else
  9. {
  10.        Debug.LogError("Hit something");
  11.        Debug.LogError(hit.transform.name);
  12. }
  13.  
  14. // *****************
  15.  
  16. if (pointyThing.GetComponent<PolygonCollider2D>().IsTouching(wedges[x].GetComponent<PolygonCollider2D>()))
  17. {
  18.         Debug.LogError("collided");
  19. }
  20.  
  21. // *****************
  22.  
  23. if (pointyThing.GetComponent<Renderer>().bounds.Intersects(wedges[x].GetComponent<Renderer>().bounds))
  24. {
  25.         Debug.LogError("collided");
  26. }
  27.  

If this wont work, how would you go about setting this up?

Sample image if it helps:

(http://imagedump.squigglyfrog.com/bonusSpin.png)


Help me while I still have hair!!
Title: Re: Using 2d polygon colliders on a 2D UI, how to determine if they intersect?
Post by: ArenMook on January 06, 2016, 11:48:58 PM
You are greatly overcomplicating this.

There is no need for polygon collision checks. Knowing your ticker's position (top of the wheel), and the rotation of the wedges, determine the one with the closest dot product. That's the one it intersects with.

To elaborate: think of wedges as arrows pointing outwards from the center of the wheel. An arrow's direction is a vector. So if a straight-up vector. Vector3.Dot will give you a value between -1 and 1, where -1 is 180 degrees and 1 is 0 degrees between the vectors, 0 is 90 degrees, etc.