Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 BobDoleOwndU · Sep 28, 2017 at 12:07 PM · c#collidersmodelsbounds

Bounds vs BoxCollider producing different sizes when provided same values?

I've been working in Unity to build a tool that can read data from a natively unsupported model format.

One of the things this format does is have its bounding boxes connected to the bones. Unity does not support putting Bounds directly onto transforms, so I compromised by using BoxColliders instead. Now here's where things have gotten confusing for me.

I have an array of Bounds that the values are read into (this format lists the maximum and minimum values of the bounding boxes):

 for(int i = 0; i < bounds.Length; i++)
 {
  bounds[i].SetMinMax(new Vector3(fmdl.GetSection0BlockDEntries()[i].minZ, fmdl.GetSection0BlockDEntries()[i].minY, fmdl.GetSection0BlockDEntries()[i].minX), new Vector3(fmdl.GetSection0BlockDEntries()[i].maxZ, fmdl.GetSection0BlockDEntries()[i].maxY, fmdl.GetSection0BlockDEntries()[i].maxX));
 } //for

From there, I assign them to the BoxColliders using the center and size values:

 BoxCollider collider = bones[i].gameObject.AddComponent<BoxCollider>();
 collider.center = bounds[fmdl.GetSection0Block0Entries()[i].boundingBoxId].center;
 collider.size = bounds[fmdl.GetSection0Block0Entries()[i].boundingBoxId].size;


Everything seems to work well for the most part. The layout of the boxes is correct, but the BoxColliders are too big: alt text

I thought that maybe the bones were getting layed out incorrectly; But they are not. Here are the bones compared to the BoxColliders: alt text

And even more interesting, if the same Bounds that I've been using the sizes from to create the BoxColliders are applied as the meshes' bounds, their size is correct (not all of the bounds get applied because the bounding boxes are meant to be applied to the bones):

 mesh.bounds = bounds[i];

alt text

So my question is, what's going on with the BoxColliders? Why are they so big when the same values applied to the meshes' bounds are the correct size? Is there anything I can do to fix this? Thanks in advance.

Comment
Add comment
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

2 Replies

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

Answer by BobDoleOwndU · Oct 03, 2017 at 10:08 AM

Found the solution. It was a global vs local issue.

When the bounds are applied to mesh.bounds, they are applied in local space, which puts them at the correct size.

When they're applied to the BoxColliders, it applies them in global space, which is why they were over sized.

The solution was to set the position in local space to the bone by doing:

 collider.center = bounds[fmdl.GetSection0Block0Entries()[i].boundingBoxId].center - bones[i].position;

This solution will only work without differing scales, which in my case, there weren't any.

Edit:

A better solution is:

 collider.center = bones[i].InverseTransformPoint(bounds[fmdl.GetSection0Block0Entries()[i].boundingBoxId].center);

which handles scales as well.

Comment
Add comment · 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
1

Answer by Runemark · Sep 28, 2017 at 01:15 PM

The images doesn't really help to find the problem, but I think this happens becouse of scaling, more specifically relative scaling.

When you are assigning the bounds, you use relative positions (relative to the mesh), but the mesh has different scaling (maybe becouse it's exported from the modelling software with a scale different of 1 unit = 1 meter).

Solution should be either changing the imported mesh scale (native import has a scale value), or converting the bound positions to global (using the scale difference).

Hope it helps to figure out what's the problem here!

Comment
Add comment · Show 3 · 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 BobDoleOwndU · Sep 29, 2017 at 03:04 AM 0
Share

Hello! Thanks for the reply.

So just to clarify, you're saying to scale the mesh up to the same size as the bounding boxes, or to scale the bounds down to the size of the meshes, using the scale difference? Is there an easy way to go about finding the scale difference?

For reference, I haven't touched the scales anywhere in Unity. All meshes, bone transforms, etc... are created in Unity with the default scales Unity uses.

avatar image Runemark · Sep 29, 2017 at 09:26 AM 0
Share

I was thinking of scaling down the colliders.

Also I found something else, that might be a problem: Are you using mesh.bounds or renderer.bounds for getting the information out?

Collider.bounds is world space bounding volume.

$$anonymous$$esh.bounds is local bounding volume

Renderer.bounds is the same as mesh.bounds but in world space coords

You should assign Renderer.bounds to Collider.bounds if I understand this correctly.

avatar image BobDoleOwndU Runemark · Sep 29, 2017 at 10:29 AM 0
Share

In the bottom image I posted, where the sizes are correct, the bounds were applied as

 mesh.bounds = bounds[i];

You can't directly set a collider's bounds, as its bounds property is read only. So I set it by doing this:

 collider.center = bounds[fmdl.GetSection0Block0Entries()[i].boundingBoxId].center;
  collider.size = bounds[fmdl.GetSection0Block0Entries()[i].boundingBoxId].size;

For getting the information out, I'm just using the BoxColliders.

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.

Follow this Question

Answers Answers and Comments

395 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 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 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 Does OnTriggerEnter() Work? 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Procedural object generation, objects overlap each other 1 Answer

Remove objects from list when they are out of range. 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