- Home /
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);
}
}
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);
}
}
Answer by rakkarage · Apr 19, 2018 at 03:27 PM
BoundsInt.Contains always starts at 0 no matter the position, so -1 is not contained either
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);
}
}
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".
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)
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
Follow this Question
Related Questions
Rotate a point around another point? 2 Answers
Get closest point on 2D perimeter using Bounds 2 Answers
Finding the Height of the Platform the Player is Standing On. 0 Answers
CircleCollider2D bounds.center, bounds.extents and radius severely innacurate 0 Answers
Keep player inside a non-square/irregular shape area 1 Answer