How to control rotation by a percentage



  • On 04/11/2013 at 03:34, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:    
    Platform:      
    Language(s) :

    ---------
    I want to be able to control (programatically) the amount of rotation, to what extent an Object A shall follow the rotation of Object B.
    When the percentage is 0%, ObjectA sits still, no rotation at all.   
    When the percentage is 100%, ObjectA's rotation is equal to Object B's rotation.

    Getting hold of ObjectA's rotation Vector and multiplying this by 0.0 to 1.0 doesn't always work, it depends on how ObjectB is rotated.

    The effect I am after is the same as you see when using Animation Clips in C4D, there you have a Percentage value for each animation layer. If an Object is rotated from 0º to 800º during an animation, setting the Clip Layer to 50% results in a rotation of 0º to 400º. Exactly what I am after.
    I just need a formula where I can enter a percentage value (0.0 .. 1.0) and use that.

    I can also use a third reference object if necessary, and set its rotation to 0º 0º 0º. Or a zeroed out Matrix or Vector as reference.
    I hope that someone reading this has more knowledge of math, matrices and vectors that I have, and knows how to do this!

    -Ingvar



  • On 04/11/2013 at 15:23, xxxxxxxx wrote:

    Because C4D uses a specialized rotation system which is not linear, it would probably be best to convert rotations from one orientation to another into quaternions and apply the percentage to the quaternion then convert it back into degrees (actually, radians is the internal unit for rotations as stored in vectors and matrices).  Quaternions encode the rotation as a direct spherical arc between two rotational orientations.  There are methods in the SDK to accomplish this.



  • On 04/11/2013 at 20:45, xxxxxxxx wrote:

    Thanks Robert,
    if you happen to know about a code sample on how to use Quaternions, I would be glad. I have read looked into the SDK on countless occasions, but when it comes to Quaternions, I have so far thrown in the towel.
    A very simple example would get me going, like a round trip C4D matrix rotation --> Quaternions -- C4D matrix rotation. The code doesn't have to do anything in particular, it is the basic principle I need right now.

    -Ingvar



  • On 05/11/2013 at 17:28, xxxxxxxx wrote:

    In the SDK docs, there is a class Quaternion that has methods to convert from a matrix to quaternion and vice versa.  Then you can use the general QSlerp() method to interpolate between two quaternions (say, an initial rotation, maybe 0/0/0, and the current rotation) by supplying that percentage value between 0.0 and 1.0. Very simple to do.  Just declare your two Quaternion class instances and get the rotation matrices as input, do your interpolation, and then extract the new rotation matrix from the result.



  • On 05/11/2013 at 21:38, xxxxxxxx wrote:

    Thanks Robert! The way you explain it, it sounds plain straight forward. I will look at it, and come back!



  • On 06/11/2013 at 00:53, xxxxxxxx wrote:

    Ok, I have tested it. Very interesting!! Unfortunately, I must be missing something.

    void TestQuaternion(BaseDocument* doc)
    {
        BaseObject* Cube1 = doc->SearchObject("cube1");
        BaseObject* Cube2 = doc->SearchObject("cube2");
        BaseObject* Cube3 = doc->SearchObject("cube3");
        if(!Cube1 || !Cube2 || !Cube3)
            return;
      
        Quaternion qt1;
        Matrix mCube1 = Cube1->GetMg();
        qt1.SetMatrix(mCube1);
        
        Quaternion qt2;
        Matrix mCube2 = Cube2->GetMg();
        qt2.SetMatrix(mCube2);
        
        
        LReal factor = 0.1;
        Quaternion qResult = QSlerp(qt1, qt2, factor);
        Matrix mResult = qResult.GetMatrix();
        
        Matrix mCube3 = Cube3->GetMg();
        mResult.off = mCube3.off;
        Cube3->SetMg(mResult);
    }
    

    In my test, I make Cube2 rotate H from 0º to 800º. 
    If I set the factor to 1.0, all is fine.
    But if I set the factor to 0.1, Cube 3 behaves strange and will "flicker" back and forth, it does not show a steady rotation 1/10 of Cube2's rotation, as I had hoped.
    What do I misunderstand / do wrong here?
     
    -Ingvar



  • On 06/11/2013 at 11:53, xxxxxxxx wrote:

    Later today, I will make some tests to see if I can see why you are getting sporadic rotation results.  It may be that you are using two different object systems and some modifications to the matrices may need to be made.  Will post my results when done.



  • On 06/11/2013 at 12:54, xxxxxxxx wrote:

    Hi Robert,
    just put 3 cubes in a C4D scene, name them cube1, cube2 and cube3, and find a means to execute the code I posted in my previous post.
    When the rotation comes to exactly 180º, cube3 will flip/flap.
    What I wanted is precisely to avoid just this effect..
    Any help is very much appreciated! I just need to understand why this happens. A factor above 0.5 seems to work fine, worth to mention.



  • On 08/11/2013 at 05:52, xxxxxxxx wrote:

    Here is the latest update:
    While my tests did not work as expected, when applying the Quaternions to my real project, it functions real good :)
    So I want to thank you Robert for pointing me in the right direction, it was a big leap forward for me! Thanks a lot!


Log in to reply