• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rakkarage · Apr 18, 2018 at 09:47 PM · 2dmathbounds

BoundsInt.Contains

 Bounds:
   m_Position: {x: -6, y: -4, z: 0}
   m_Size: {x: 12, y: 8, z: 1}

given this BoundsInt

 Vector0: {x: -6, y: 0, z: 0}

why is -6 contained

 Vector0: {x: 6, y: 0, z: 0}

but not 6?

 using UnityEngine;
 public class TestBoundsInt : MonoBehaviour
 {
     public BoundsInt Bounds = new BoundsInt(new Vector3Int(-6, -4, 0), size: new Vector3Int(12, 8, 1));
     public Vector3Int Vector = new Vector3Int(6, -3, 0);
     void OnDrawGizmosSelected()
     {
         Gizmos.color = Color.blue;
         Gizmos.DrawWireCube(Bounds.center, Bounds.size);
         Gizmos.color = Bounds.Contains(Vector) ? Color.green : Color.red;
         Gizmos.DrawWireCube(Vector, Vector3Int.one);
     }
 }

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rakkarage · Apr 18, 2018 at 09:54 PM 0
Share

or idk I guess bounds is the same... hrm

avatar image rakkarage · Apr 19, 2018 at 02:42 PM 0
Share

sorry I guess I was confused

offsetting the vector so it lines up with grid clears it up

 using UnityEngine;
 using UnityEngine.Tilemaps;
 public class TestBoundsInt : $$anonymous$$onoBehaviour
 {
     public BoundsInt Bounds;
     public Vector3Int Vector;
     void OnDrawGizmosSelected()
     {
         var offset = new Vector3(.5f, .5f, 0f);
         Gizmos.color = Color.blue;
         Gizmos.DrawWireCube(Bounds.center, Bounds.size);
         Gizmos.color = Color.white;
         Gizmos.DrawWireCube(Bounds.$$anonymous$$ + offset, Vector3Int.one);
         Gizmos.color = Color.white;
         Gizmos.DrawWireCube(Bounds.max + offset, Vector3Int.one);
         Gizmos.color = Bounds.Contains(Bounds.position + Vector) ? Color.green : Color.red;
         Gizmos.DrawWireCube(Bounds.position + Vector + offset, Vector3Int.one);
     }
 }
 

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by rakkarage · Apr 19, 2018 at 03:27 PM

  1. BoundsInt.Contains always starts at 0 no matter the position, so -1 is not contained either

  2. The 'selection' vector is not on the same grid and needs to be offset by .5 to line up

|||

 using UnityEngine;
  using UnityEngine.Tilemaps;
  public class TestBoundsInt : MonoBehaviour
  {
      public BoundsInt Bounds;
      public Vector3Int Vector;
      void OnDrawGizmosSelected()
      {
          var offset = new Vector3(.5f, .5f, 0f);
          Gizmos.color = Color.blue;
          Gizmos.DrawWireCube(Bounds.center, Bounds.size);
          Gizmos.color = Color.white;
          Gizmos.DrawWireCube(Bounds.min + offset, Vector3Int.one);
          Gizmos.color = Color.white;
          Gizmos.DrawWireCube(Bounds.max + offset, Vector3Int.one);
          Gizmos.color = Bounds.Contains(Bounds.position + Vector) ? Color.green : Color.red;
          Gizmos.DrawWireCube(Bounds.position + Vector + offset, Vector3Int.one);
      }
  }
  


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 19, 2018 at 05:00 PM 1
Share

Your answer doesn't make much sense. First of all it doesn't always start at "0". The bounds starts at the set position. Just look up the actual implementation. Second you're currently mixing floating point vectors with integer bounds. I'm note sure what's the point of your code here, but i think you completely missed the point of BoundsInt. If you're interested in a general AABB just use the normal "Bounds".

avatar image
2

Answer by Eno-Khaon · Apr 18, 2018 at 10:19 PM

Looking at the behavior, it seems the maximum bounds are not inclusive in BoundsInt.Contains().

 BoundsInt testBounds = new BoundsInt(-6, -4, 0, 12, 8, 1);
 
 // Prints "true"
 Debug.Log(testBounds.Contains(testBounds.min));
 
 // Prints "false"
 Debug.Log(testBounds.Contains(testBounds.max));


According to the documentation, it should have, but does not appear to *actually* provide the option to choose whether the test will be inclusive or not.

(This information accurate as of version 2018.1b)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 19, 2018 at 04:50 PM 1
Share

Actually according to the documentation the Contains method should have a second, optional parameter called "inclusive" which should control the max behaviour. Though the method doesn't have that parameter ^^. So maybe they have a version of that method that has this extra parameter but it's not shipped yet / got removed. This is the actual implementation. So max is clearly not included. This actually makes sense since the "size" specifies how many coordinates are in there. So if you start at -6 and have a count of 12 that would be:

  1   2   3   4   5   6   7   8   9  10  11  12
 -6, -5, -4, -3, -2, -1,  0,  1,  2,  3,  4,  5  // <-- 12 elements

If you start at 0 with a range of 12 it would be:

  1   2   3   4   5   6   7   8   9  10  11  12
  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11  // <-- 12 elements


Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

162 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get the bounds of an actual sprite, not it's bounding box. 1 Answer

Starting point or library for freehand drawing 2D shapes, calculating intersection areas and basic translations? 0 Answers

My object is going through boundaries Problem! 1 Answer

Can't calculate the bounds.min precisely. 0 Answers

AI reflect shooting 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges