• 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
15
Question by aldonaletto · Jun 04, 2011 at 02:31 AM · normal

The (ab)normal mistery

When I was trying to answer a question about collision normal inaccuracies, I performed some tests with colliders and normals, and found a very strange behaviour (see movie here).
I basically put a simple cylinder to rotate along its axis, and cast a perpendicular ray to its center, then drew the resulting normal at the hit point. Ideally, the normal should return over the incident ray, but it began to swing like a pendulum!
The small project containing the tests can be download here . If someone wants to reproduce this experiment from scratch, just create a simple cylinder and attach to it the following script:

 var height:float = 0;
 var length:float = 5;
 
 function Update(){
 
     var org:Vector3 = Vector3(-length,height,0);
     var hit:RaycastHit;
     
     Debug.DrawRay(org,Vector3.right*length,Color.red);
     if (Physics.Raycast(org,Vector3.right,hit,length)){
         var w:float = (hit.point-org).magnitude;
         Debug.DrawRay(hit.point,hit.normal*w,Color.green);
     }
     transform.Rotate(0,30*Time.deltaTime,0);
 }

It's necessary to click the Gizmos button over the Game view in order to see the rays.
While the cylinder rotation is set to (0,0,0), the normal returns over the incident ray, as expected. If you set the X rotation to 90, however, the mistery appears: the normal begins to swing smoothly in a large angle. Things go even worse if you set the initial rotation to (0,0,90): not only the normal swings in a bigger angle, but also the hit point oscillates back and forth!
Can anyone explain this mistery? Am I doing something wrong? Or is it a bug?
NOTE: This strange behavior only affects the capsule collider. Box, sphere or mesh colliders behave as expected despite the rotation angle.

Comment
Add comment · Show 14
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 GesterX · Jun 04, 2011 at 06:05 PM 2
Share

Could this be Gimbal Lock or does Unity have a way of making sure that doesnn't happen?

avatar image aldonaletto · Jun 04, 2011 at 06:57 PM 1
Share

I don't think it's Gimbal Lock. It looks like the capsule collider were a peanut ins$$anonymous$$d of a capsule (but it still looks like a capsule in the game view). I suspect there's a bug in the routines that calculate normals and the hit point, so when the capsule is not in the "vertical" position they get fooled and generate wrong results.

avatar image Peter G · Jun 04, 2011 at 06:57 PM 2
Share

@GesterX, Unity stores rotations in Quaternions to prevent the rotation from entering the Gimbal lock.

avatar image Peter G · Jun 04, 2011 at 06:58 PM 6
Share
  • for the catchy title though :)

avatar image aldonaletto · Jun 04, 2011 at 07:17 PM 1
Share

Peter, the Debug.DrawRay uses a "kind of" ray: it takes a vector to indicate the direction, but not a normalized one - its magnitude sets the lenght of the ray. I think internally the second parameter is added to the first parameter to generate the end point of the ray, and a line is drawn between the two points. I suppose the code is correct: I changed the collider to mesh, sphere and box, and the rays and normals behave as expected - only the capsule behaved this weird.

avatar image Wolfram · Jun 04, 2011 at 07:21 PM 1
Share

@Peter_G, no, what you are describing is DrawLine. DrawRay should be fine with a starting point and a direction.

avatar image Peter G · Jun 04, 2011 at 07:23 PM 1
Share

@Wolfram, that's exactly what I was thinking. Thanks for catching that. I need to read a bit more carefully.

avatar image aldonaletto · Jun 04, 2011 at 07:49 PM 1
Share

NOTE: I edited the question and added a link to download the small project used to perform these tests.

avatar image Wolfram · Jun 04, 2011 at 08:03 PM 2
Share

Hm, my guess would be some kind of epsilon problem due to the 90deg X-rotation. Unity DOES have its problems with that, it is closely Gimbal lock related. Are we sure Unity really does all its transformations with Quaternions/$$anonymous$$atrices, and uses Euler angles only for conversion and Inspector display? Because things like that shouldn't happen if that were the case.

You can clearly see the Euler angle conversion problems in the Inspector: although you rotate around Y, the converted value shows X changing its value, because the conversion cannot handle the transform's up vector being perpendicular to world up.

The fact that the normal is looking fine in your video if you don't rotate around X or Z seems to support my assumption. Have you tried with the ray co$$anonymous$$g from below and going along +Y?

avatar image Wolfram · Jun 04, 2011 at 08:04 PM 2
Share

Hm, it is interesting, though, that you say the other colliders don't have a problem with your test script. I wonder how the capsule collider is implemented internally.

avatar image aldonaletto · Jun 04, 2011 at 08:16 PM 1
Share

I noticed that the abnormality starts to happen only when the Euler angles shown in Inspector start to show those crazy values. I suppose the values are not wrong themselves - I think they are just some of the possible values the conversion back to Euler angles can give. But it may give some clue to the cause of the mistery.

avatar image Jesse Anders · Jun 04, 2011 at 10:08 PM 2
Share

@Peter G: Quaternions don't prevent gimbal lock as such; rather, it's simply constructing and manipulating rotations in ways that don't lead to gimbal lock that prevents gimbal lock.

@aldonaletto: Agree with Peter G, nice thread title :)

avatar image Bunny83 · Jun 06, 2011 at 03:44 AM 2
Share

That's really a huge bug. I'm not sure if Unity have messed up something when they have implemented the capsule from Physx or if that is a bug that comes already with Physx. Anyway, i guess we should file a bug report :D.

I'm way too tired now.

avatar image aldonaletto · Jun 06, 2011 at 04:36 AM 1
Share

Good job! I'll report a bug and link all this stuff to easy things for the Unity's $$anonymous$$m.

1 Reply

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

Answer by Bunny83 · Jun 05, 2011 at 06:15 PM

I've done some research myself. It seems to be a problem in the physics system when entering or passing some kind of gimbal lock. I've checked the Quaternion's values and they are still valid.

Even resetting the rotation won't fix this problem once it's messed up. So i guessed it's a problem with the collider itself.

I just paused the game, removed the collider and added a new one. That has fixed the problem.

I guess it's a bug in Physx system.
The internal transformation matrix (at least for raycasts, the normal physics collision is not affected) seems to be messed up.

See this nice pic :D.
I've just created a raycast-field and show the hit normals. As you can see the "logical" representation is messed up. It's even streched a bit, not just rotated.

Capsule bug

If you rotate it manually a lot it can result in some really strange shape:

Capsule bug 2 Capsule bug 3

(I used 3 raycaster to get the full shape, 40x40x3 = 4800 raycasts @ 20fps :D )

I've re-uploaded the modified test project if someone wants to test this crazy stuff :D

edit

Added the project as webplayer to my public dropbox folder

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 Bravini · Jun 06, 2011 at 04:15 AM 6
Share

when I first started learning unity I had a lot of problems with capsule colliders. apart from the raycast messing up, with first person controllers and rigidbodies gave some weird bugs like the capsule floating ins$$anonymous$$d of walking and falling only when the walk button was released. Other kinds of colliders also didn't show that bug.

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

9 People are following this question.

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

Related Questions

Moss/Snow texture on arbitrarily rotated doodads. 1 Answer

Shader: actual view direction/normal? 2 Answers

Problem importing baked normal map from 3ds max 1 Answer

Why does my shader only shade objects based on their normals in the X axis? 1 Answer

Color depending on vertex normal 1 Answer


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