Question

I have seen that piece of code, but I don't get it what's really doing, in mathematical way. Source is a 3D Model of a bike. The normal comes from a ray casting to the ground, and mFrontWheelNormal, is coming from another ray casting.

Quaternion rotFront =
        Quaternion.AngleAxis(Source.rotation.eulerAngles.y, normal) *
                Quaternion.FromToRotation(new Vector3(0, 1, 0),
                                          mFrontWheelNormal.GetVector3());
Was it helpful?

Solution

Thinking of this multiplication as two separate nodes may help. You have two rotations here

  1. the one that is returned by Quaternion.AngleAxis (xz orientation of the bike on the ground)
  2. the one that is returned by Quaternion.FromToRotation (the tilt of the front wheel)

Imagine that 1 is the parent of 2, and 1's rotation is "outer" to 2's rotation. I imagine that this rotFront is applied to an object that is not the bike itself. In that case that object is effectively 2's child. This rotation probably makes that object to have a similar rotation to the bike's front wheel.

So the motion that this creates is identical to adding a parent to the object that has 2's rotation, and adding a parent to 2 which has 1's rotation.

2 is the rotation that when applied to the +y vector, it would make it become like the front wheel's normal vector (I assume this is the normal on the ground). Its purpose is to tilt the object like the front wheel.

1 is a rotation around "normal" with the bike's y rotation. Its purpose is to orient the object on the ground to the same xz direction that the bike is facing.

By applying these two, an object with a zero rotation becomes oriented like the bike's front wheel. I'm not exactly sure why they don't just use the front wheel's rotation. Maybe with 1 they want to make sure they get the bike's xz orientation and not the front wheel's. It may make sense to use this for a camera, you want the camera to follow the bike's body, not the front wheel. Maybe using the tilt of the front wheel makes the camera motion look cooler. To me it sounds like they did some trial and error before they arrived at this formula.

I hope this helps. If you tell me precisely what normal and mFrontWheelNormal are (ok ray casting, but from where and along what vector?) and where exactly rotFront is used, I can be of more help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top