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.

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.

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


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

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

edit

Added the project as webplayer to my public dropbox folder