So i’m back with another Aabb Extents question. Everything i coded works HOWEVER, i noticed the cylinder aabb looks funky when rotated. With the extents returned from the Aabb there is always an axis that is too long and i believe it’s this line of code.
Blue = Calclated Shape ← this works
Red = Extents Bounds ← uses aabb.CalculateAabb()
I tried removed the extra radius but then i get an axis always being too small. To be honest my program still works, it’s just not as precise as I want it to be with cylinders. Any ideas on how to remove the extra space?
more pictures (Note: the red bounds is NOT supposed to rotate
)
Don’t judge me on this if I am wrong because I am not looking at this for very long, but I think you need to multiply e by (1 - axis).
Unfourtunately, I tried this and it didn’t work, thanks though.
Since this is Unity’s code maybe my mesh creator is funky?
public void GenerateMeshEquivalent(ref Mesh m,float3 size = new float3())
{
// verify the collider is null
if (collider != ACollider.Null)
{
GameObject go = null;
Mesh tmp = new Mesh();
if (m == null) m = new Mesh();
float3 scale;
unsafe
{
if (collider.GetCollider_P() == null)
{
Debug.LogWarning("GenerateMeshE: Cannot generate a mesh with a null collider.");
return;
}
}
size = size.Equals(float3.zero) ? AColliderProperties.GetSize(collider.properties) : size;
// Debug.Log(collider.aabb.Min);
switch (collider.properties.type)
{
case ColliderType.Box:
go = GameObject.CreatePrimitive(PrimitiveType.Cube);
tmp = go.GetComponent<MeshFilter>().sharedMesh;
m = CopyMeshWithModification(tmp, size);
break;
case ColliderType.Capsule:
go = GameObject.CreatePrimitive(PrimitiveType.Capsule);
tmp = go.GetComponent<MeshFilter>().sharedMesh;
scale = size / tmp.bounds.size;
m = CopyMeshWithModification(tmp, scale );
break;
case ColliderType.Cylinder:
go = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
tmp = go.GetComponent<MeshFilter>().sharedMesh;
scale = size / tmp.bounds.size;
// Debug.Log("size = "+size+"::"+tmp.bounds.size+":"+scale);
m = CopyMeshWithModification(tmp, scale );
break;
case ColliderType.Sphere:
go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
tmp = go.GetComponent<MeshFilter>().sharedMesh;
m = CopyMeshWithModification(tmp, size);
break;
default:
Debug.LogWarning("Failed to get mesh for this type....");
break;
}
if (!Application.isPlaying)
GameObject.DestroyImmediate(go);
else
GameObject.Destroy(go);
}
else Debug.LogWarning("GenerateMeshE: cannot generate a mesh with an invalid ACollider.");
}
private Mesh CopyMeshWithModification(Mesh org,float3 scale)
{
Mesh m = new Mesh();
m.vertices = org.vertices;
m.uv = org.uv;
m.triangles = org.triangles;
m.normals = org.normals;
m.tangents = org.tangents;
Vector3[] verticies = m.vertices;
// string a = "";
for (int i = 0; i < verticies.Length; i++)
{
verticies[i] = new Vector3(verticies[i].x * scale.x, verticies[i].y * scale.y, verticies[i].z * scale.z);
// a += "\n" + verticies[i].ToString();
}
m.vertices = verticies;
// Debug.Log(a);
m.RecalculateBounds();
m.RecalculateNormals();
m.RecalculateTangents();
return m;
And the function that calls it:
cropSection.CropSectionBounds.GenerateMeshEquivalent(ref CropBoundsMesh, cropSection.CropSectionBounds.collider.aabb.Extents);
Gizmos.color = Color.red;
Gizmos.DrawWireMesh(CropBoundsMesh, cropSection.CropSection.CropBounds.transform.pos, quaternion.identity);
This is what ACollider holds:
[System.Serializable]
public struct ACollider : IComponentData
{
// The acutal Collider Data (BoxCollider,SphereCollider,etc) is actually lost in the internal Data so converting
// Collider->Collider*->BoxCollider gives curropted Data. This only way to get that data is to store the Collider*
private unsafe Unity.Physics.Collider* Bounds_p;
[HideInInspector]
// the Aabb of the collider. this is stored to prevent multiple calls to
// Bounds.calculate Aabb
public Aabb aabb;
[HideInInspector]
// store the colliders position and rotation
public RigidTransform transform;
[HideInInspector]
public AColliderProperties properties;
//... other fucntions