# Calculate relative rotation from global rotation

On 09/08/2013 at 11:11, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   R13-R14
Platform:   Windows  ;
Language(s) :     C++  ;

---------
Here I am again with my need for rotation algorithms.

I need to find the relative rotation between pink cube and aqua cube.
This rotation will be the same when the gray disc moves in space, but will change as the aqua cube's parent sphere roll on the disc.
The challenge is to find the relative rotation between the two cubes.
I want to do it using the two cubes'  global  position / rotation  only. And not involve the sphere and/or the disc in the calculation. If the latter is impossible, then I would still be interested..

I am almost sure such calculations take place inside C4D, and that a C4D programmer would tackle this task in a minute.. or at least I assume so.
I think I once saw an algorithm on how to do this, multiplication of matrices, inverting matrices, or something in that direction.
Any help will be very much appreciated, I am stuck on this task.
BTW, if you wonder why I need this, it is for a totally different task, but I found that the image above gives a good illustration on what I am after.

Here is a Python script that might be interesting (?)

``````    def _GlobalToLocal(self, obj, global_pos) :
#"""     Returns a point in global coordinate in local space.     """
obj_mg = obj.GetMg()
return ~obj_mg * global_pos
``````

I do not know exactly what it does, and to what extent it can be used in C++ and in my case.

On 09/08/2013 at 12:15, xxxxxxxx wrote:

Hi there ingvarai,

I'm by no means an expert at this so don't know how much I can help but why not try getting the global matrix of each cube and then subtract one from the other? Example:

``````
Matrix mat = (cube1->GetMg() - cube2->GetMg()));
``````

Others may of course come in and override me here But I use this on vectors (as I find them a little easier to understand and work with while learning) but it might translate across to matrices as well? Give it a go and see how it comes out!

Re - the python code, best I don't say anything there as I don't know python coding!

Hope that helps,

WP.

On 09/08/2013 at 13:14, xxxxxxxx wrote:

Hi Wp
Your code It works, until the disc starts to move in 3D space.

I admit that I myself, may be the problem here..
And that there might be a flaw in my way of thinking. But there should be a way to solve this.. maybe I need to add to the calculation all parents, all the way up, for it to work. Or at least add parents until I find a common ancestor..

On 09/08/2013 at 20:43, xxxxxxxx wrote:

Multiply the inverted global matrix of the aqua cube with the global matrix of the pink cube. MatrixToHPB() will give you the pink cube's rotation in the parent space of the aqua cube.

You can use this code for testing in a Python tag:
-------------------------
m = ~aqua.GetMg() * pink.GetMg()
rot = utils.MatrixToHPB(m)
-------------------------

On 10/08/2013 at 02:48, xxxxxxxx wrote:

Ingvar, you would probably be better served by showing us an illustration of the relative rotation relationship that you want to calculate.  Is it the rotational difference between the two objects in world space (which is what I think you are attempting) or some relation of one to the other (a disjoint spatial system not belonging to either but related to one)?

I know that you've been struggling with this and an illustrative example would possibly make it easier for us to consider and solve your problem.

On 10/08/2013 at 04:21, xxxxxxxx wrote:

Originally posted by xxxxxxxx

Multiply the inverted global matrix of the aqua cube with the global matrix of the pink cube. MatrixToHPB() will give you the pink cube's rotation in the parent space of the aqua cube.

You can use this code for testing in a Python tag:
-------------------------
m = ~aqua.GetMg() * pink.GetMg()
rot = utils.MatrixToHPB(m)
-------------------------

BINGO
I did something similar, but I was inverting the wrong side of the equation.

On 10/08/2013 at 04:35, xxxxxxxx wrote:

Originally posted by xxxxxxxx

Is it the rotational difference between the two objects in world space (which is what I think you are attempting) or some relation of one to the other (a disjoint spatial system not belonging to either but related to one)?

Robert, it is the relative rotation of two objects, one sits on its parent, the other sits on a "in between object", exactly as my illustrations shows.
What I am making is several plugins for character animation in C4D. I think C4D is an excellent character animation tool, despite what folks say, even in C4D user forums. Well, it will be excellent, with my plugins
It has eventually become so complex that I chose to use a simple illustration. It is all about charatcers, bones, controllers etc., and objects having their rotation axis everywhere, in all directions, except the default XYZ orientation in C4D. So I am using helper objects to circumvent this.

This has been a strange start of the day. I read the forum, found David's solution, tried it and it worked! Then I had a look at my old code, the much more complex code that somehow and sometimes work, sometimes not. Then I found a flaw in the code, fixed it, and now that one works too!
So I have my old, fixed code which now finally works, and David's elegant solution too boot..
It is like a stone fell off my shoulders. I never give up, that's my strength, and often mu curse..

But all in all - it stems from my lack of understanding of the C4D 3D space. I can get things to work, but often I really do not know why it works. Reminds me of the famous quote from quantum physicist Richard Feynman: "If you think you understand quantum mechanics, you don't understand quantum mechanics".

On 10/08/2013 at 07:22, xxxxxxxx wrote:

Well, if this all works the way you want, very great.  Make sure that it works in various general situations and you have your answer. Note that resolving this at the global level resolves any hierachical issues.  Seems to be the best approach.  You will eventually understand enough to play with it even if you do not understand it completely.

On 10/08/2013 at 08:16, xxxxxxxx wrote:

Originally posted by xxxxxxxx

What I am making is several plugins for character animation in C4D. I think C4D is an excellent character animation tool, despite what folks say, even in C4D user forums. Well, it will be excellent, with my plugins
It has eventually become so complex that I chose to use a simple illustration. It is all about charatcers, bones, controllers etc., and objects having their rotation axis everywhere, in all directions, except the default XYZ orientation in C4D. So I am using helper objects to circumvent this.

But there's really no need to make any plugins that deal with rotations and axis stuff for C4D.
It already has editable axis rotation orders and lots of constraint tags.

Everything you are doing is already there. And is fully controllable through the sdk.
-The rotation orders can be changed via code
-The various constraint tags can be added, deleted, changed via code
-All of the parameters(links, strength, executions button, ect..) can all be changed via code

Everything you're attempting to do (and more) is already there. And editable through the sdk.
I can understand writing your own custom tools that take advantage of these built-in things.
But you're trying to re-write what's already been written, working, and built into the program.
Every problem you've posted over these past weeks is easily solved by adding the correct constraint tag. With the proper settings.

The only reason I'm personally interested in rotations type code is because I write code for other programs. And sometimes they don't have enough hierarchy code in them. So I have to write it myself.
But in C4D. There's no reason to re-write these things from scratch because they are already there. And already fully editable through the sdk.

-ScottA

On 10/08/2013 at 09:50, xxxxxxxx wrote:

Originally posted by xxxxxxxx

Everything you are doing is already there. And is fully controllable through the sdk.
[..]
Everything you're attempting to do (and more) is already there.
But you're trying to re-write what's already been written, working, and built into the program.
Every problem you've posted over these past weeks is easily solved by adding the correct constraint tag.

Ahem.. allow me to disagree! With "everything" you write here!
First and foremost I write this stuff for me, myself. I am engaged in a non-profit task (environmental issues, preserve nature etc.) So I need to illustrate certain scenarios, sometimes involving people. In this case I (we) will use Cinema 4D, because it turned out it is the allround _ _ professional 3D app that can do most. Yes, I am a professional C# writer. But having hunted high and low and not found what I am after, freeware, or stuff that cost money, I rolled up the sleeves and started to write in C++. I have made some Python plugins, yes, but I hope I do not have to write a single line of Python anymore.

When my plugins are ready, I will start using C4D, and work with character animation the way I want it to function, fast, easy. Not the way it is to day, slow, tedious and cumbersome. I want to take advantage of the vast number of characters and objects out there, most notably the daz 3D store, along with several thousand BVH animation files that recently have been released for free.
With my plugins, this stuff can be used in C4D, practically out of the box, which is not the case today.

Wait with your verdict, till you see my plugins! They will hopefully be released, once they are ready. If I have time to polish them enough to be published. And in the mean time, please bear with my sometimes silly questions. I really appreciate all the help I am getting here! This forum is invaluable!

Here:
Have a look at this thread. I started this thread 15 months ago, as an ultimate C4D beginner. Read what I write, and especially read the answers! Perhaps you will better understand what I am doing now.
http://www.c4dcafe.com/ipb/topic/68246-how-can-i-do-something-similar-to-genesis-in-c4d/?hl=ingvarai

On 10/08/2013 at 09:56, xxxxxxxx wrote:

Originally posted by xxxxxxxx

You will eventually understand enough to play with it even if you do not understand it completely.

Yes
If I could be satisfied with that! But I have to be now, I am running out of time..
As I said to Scott, I am dependent on the assistance and advice I get here in this forum!

On 10/08/2013 at 13:11, xxxxxxxx wrote:

Originally posted by xxxxxxxx

Wait with your verdict, till you see my plugins! They will hopefully be released, once they are ready. If I have time to polish them enough to be published. And in the mean time, please bear with my sometimes silly questions. I really appreciate all the help I am getting here! This forum is invaluable!

Fair enough.
All I'm saying is that you are writing more code than you have to. And making things much, much harder on yourself.
For things requiring parenting and rotations in complex hierarchies. You can save yourself a ton of troubles by using the built-in constraint tags. While writing less code.

-ScottA

On 11/08/2013 at 01:04, xxxxxxxx wrote:

Originally posted by xxxxxxxx

You can save yourself a ton of troubles by using the built-in constraint tags. While writing less code.

Well, this trouble is what drives my interest in programming. It is the forever repeating chain Problem-->Experimenting-->Solution that I am addicted to. Hadn't it been for this addiction, I would probably never had become a programmer. I am 100% autodidact.
I will stop here, I just want to say that when you (hopefully) see my plugins, you will realize the same as me, using constraint tags would require several dozens of them inside my own plugin, not a feasible solution. I have tried!
As it is now, I have at least opened the door a little, to he mysterious 3D world of Cinema 4D, instead of just using an unopened black magic box. And that is worth the trouble!