Topic: Rotation offset

In the other thread I was trying to change the mesh origin. Now I need to rotate it too but since it looks hard I thought I could just add an offset rotation to my object.

I have a 3d point P and a 3d point Q.
I want to get the rotation between these points and use it as an offset for my object.
This is what I tried.

//find the angle between the two 3d points
    MVector3 AxisYPoint = P;
    AxisYPoint.y = 1000;
    MVector3 A = AxisYPoint - P;  //vector A
    MVector3 B = Q - P;               //vector B
    A.normalize();
    B.normalize();

//find the angle
    float dp = A.dotProduct(B);
    float angle = acos(dp) / float(M_PI) * 360;

//find the sign of the angle
    MVector3 cross = B.crossProduct(A);
    if(B.dotProduct(cross) < 0)
        angle = -angle;

//find the axis
    axis = cross;

//transform and rotate the object
...

//and finally add the rotation offset
object->addAxisAngleRotation(axis,angle);

Useless to say, it doesn't work. The offset is not correct and so the final rotation is bad.

Re: Rotation offset

Bump :3
Just a hint would be ok.

Re: Rotation offset

255 wrote:

Bump :3
Just a hint would be ok.

I am not sure I understand the logic behind wanting to change the origin of your model via code, if you use blender just move your model in edit mode and you change its origin, its as simple as that.

Example: Your origin will most likely always be the center point of your models bounding box, in the case of a door just go into edit mode select all faces and move your model on its "x" axis until your origin point is at the edge of your door. Go back in in object mode and export to maratis. Now your door will rotate on the z axis as if it was on hinges.

Even is cases where the origin of your model might change to different locations you would ether load a new model or just animate this behavior.

But doing this in code if not done right can mess all kinds of things up "Motion, Physics, Collision Detection"


I don't have a code specific solution for you as this isn't something I tend to see people wanting to do on purpose, but maybe the above suggestion might help?

Last edited by zester (2013-10-14 23:51:56)

Re: Rotation offset

Well, I just dealt with origin points myself. And I think one reason it would help to know if it can be done in code is the fact that after a maratis mesh is made, there is no way to edit it otherwise (no blender importer)

I would have to have the original .blend file or something to adjust anything about it.

Re: Rotation offset

Tutorial Doctor wrote:

Well, I just dealt with origin points myself. And I think one reason it would help to know if it can be done in code is the fact that after a maratis mesh is made, there is no way to edit it otherwise (no blender importer)

I would have to have the original .blend file or something to adjust anything about it.

A blender (Mesh) importer wouldn't be that hard to write, provided you had experience writing blender plugins.  The (mesh) format is just simple xml.

I couldn't find any reference or detected changes to my two (mesh) files when I made changes to my cubes origin. You might want to put in a feature request for an addition to ...

<BoundingBox minx="-1.000000" miny="-1.000001" minz="-1.000000" maxx="1.000000" maxy="1.000000" maxz="1.000000" />

Such as ...

<BoundingBox ...>
    <Origin x="" y="" z=""/>
</BoundingBox>

That reminds me we need an xml binding for lua.

Re: Rotation offset

Hmm, I keep hearing of xml. I guess I need to look into that. I have been going from programming language to programming language, hahaha.

I have been gaining a lot of experience though. I think that all programmers should learn any language they can, because that makes you more flexible. I see now why companies want you to know so many of them, because usually each serves a unique purpose.

You are way above me when it comes to programming though (what's a binding) haha.

As for Blender plugins, I just simply don't like Blender's API. Then again I am picky about how API's should be presented. I like Maratis's API though.

Re: Rotation offset

I appreciate your help attempts, but please now don't go off-topic. ^ ^

I know my stuff, I need to edit the mesh via code because I'm trying to do complex things and so the mesh must be manipulated by my game and not manually using Blender or other tools.
If you can find what's wrong in my above code, then post.
Thanks.

Re: Rotation offset

255 wrote:

I appreciate your help attempts, but please now don't go off-topic. ^ ^

I know my stuff, I need to edit the mesh via code because I'm trying to do complex things and so the mesh must be manipulated by my game and not manually using Blender or other tools.
If you can find what's wrong in my above code, then post.
Thanks.

The problem with your code is your trying to rotate an object from a pivot point that doesn't exist. I know you think you created it but you didn't. And it cant be done that way anyways. You do have some of the needed math (convert Degree's to Radiant and Divide Pi by 180 Degrees and Translate your Objects X, Y and Z Position in a loop)  you need to calculate the path(X,Y and Z) and move your object along that path. You cant create an "offset" and try to rotate your object using it, because it doesn't actually exist. An Origin is actually two objects parented to each other. You then rotate the parent(origin) object.

You need a Tweening Library.

Other than that your code isn't going to work.

Ohhh and Mr I know my stuff your question, and even similar code examples have been posted in the past here on these vary forums. Thy knew there stuff two apparently, we need "Rotation Offset" in the freaking FAQ, with Diagrams on Why, Why Not and Alternative Methods. wink

Last edited by zester (2013-10-15 13:12:24)

Re: Rotation offset

zester wrote:

Ohhh and Mr I know my stuff

With "I know my stuff" I just meant that I know that you can change the origin in Blender but I really needed to change it via code. I just meant that, sorry if it sounded snob. Since you said "I am not sure I understand the logic behind..." I was just trying to say "yeah just trust me" lol.

zester wrote:

The problem with your code is your trying to rotate an object from a pivot point that doesn't exist. I know you think you created it but you didn't.

Thanks for the input.
But I think that the pivot does exist.
I'll try to give more information, I just didn't want the first post to be a wall of text, but I guess it's necessary.
What I'm trying to do is to create a complex ragdoll which loads a mesh as the bounding box for every part.
What I'm doing is this:

say you have a forearm and two bones, elbow and wrist
create a ragdoll part
set its origin the same as the wrist bone
set the part position in the position the same as the wrist bone
set the pivot constraint = position of elbow bone
update the skeleton to match the rotation of the ragdoll part
start the physics context and enjoy how everything is working fine and the forearm swings

but problem: if your ragdoll part mesh which is the forearm is a bit rotated (in the mesh file), then your skeleton will rotate wrong. This is why I'm trying to add an offset rotation to its rotation.
If you now look at the code in the first post, you will see that it makes more sense, given that point P is the elbow bone position and point Q is the wrist bone position.

An alternative is rotating the mesh itself, but this seems even harder because you have to change normals too, etc., but I may look into that too.

Last edited by 255 (2013-10-15 16:40:10)

Re: Rotation offset

A drawn diagram of what you want to do would help me. I'm sorta okay at calculus, but personally I like to find the easiest route. I don't get what you are trying to do.

When you say "offset rotation" what do you mean? And when you say you want to use the rotation "between" two objects, what do you mean?

Are you doing some sort of pendulum motion that has one object following the same rotation of a other, just offset by a certain angle?

For example, objec1 swings and then object2 swings just like object1, but the timing is offset?

Hmm, reminds me of that joints project that used behaviors to make a rag-doll looking effect.

Also, if the rotation is local, to the joint, why would it rotate the whole mesh?

Last edited by Tutorial Doctor (2013-10-15 16:58:02)

Re: Rotation offset

255 wrote:
zester wrote:

Ohhh and Mr I know my stuff

With "I know my stuff" I just meant that I know that you can change the origin in Blender but I really needed to change it via code. I just meant that, sorry if it sounded snob. It was a way to say "yep I know you can change it Blender but I really need to do it via code".

zester wrote:

The problem with your code is your trying to rotate an object from a pivot point that doesn't exist. I know you think you created it but you didn't.

Thanks for the input.
But I think that the pivot does exist.
I'll try to give more information.
What I'm trying to do is to create a complex ragdoll which loads a mesh as the bounding box for every part.
What I'm doing is this:

say you have a lower arm and two bones, elbow and wrist
create a ragdoll part
set its origin the same as the wrist bone
set the part position in the position the same as the wrist bone minus the elbow bone
set the pivot constraint = position of elbow bone
update the skeleton to match the rotation of the ragdoll part
start the physics context and enjoy how everything is working fine and the arm swings

but problem: if your ragdoll part mesh which is the lower arm is a bit rotated (in the mesh file), then your skeleton will rotate wrong. This is why I'm trying to add to its rotation an offset rotation.
Or I could rotate the mesh itself, but this seems way harder.

I mean your pivot exist just not ... never-mind doesn't matter.

You should have just said "Ragdoll, Code, Not Working, HELP" wink lol

I now see what your saying. Both solutions are incorrect thou, you wouldn't want to do ether. You do know that when you start messing with the pivot point your bounding box for each object modified is going to change its scale and transformation right?

Thats why I was say no no no no don't do that. Cause it will really mess with your physics. And then your ragdoll will really look bad.

Normally you wouldn't even use a ragdoll in a game, they exist for the purpose of recording a keyframed animation in your 3d package(blender) allowing you to simulate the physics of beating the crap out of a character to play back at a later time.

What you want doesn't have lua bindings yet "MArmature" and "MSkinData" I think "MSkinPoint * m_points;" is the  Bone's Transformation Matrice. If so thats what you would be wanting to tweek I do believe.

How is your C++? I would say put in a request.

I am half tempted to just re-implement the lua bindings using luabind would only take a couple of hours to wrap maratis entire codebase.

Last edited by zester (2013-10-15 17:22:23)

Re: Rotation offset

Tutorial Doctor wrote:

A drawn diagram of what you want to do would help me. I'm sorta okay at calculus, but personally I like to find the easiest route. I don't get what you are trying to do.

When you say "offset rotation" what do you mean? And when you say you want to use the rotation "between" two objects, what do you mean?

Are you doing some sort of pendulum motion that has one object following the same rotation of a other, just offset by a certain angle?

For example, objec1 swings and then object2 swings just like object1, but the timing is offset?

Hmm, reminds me of that joints project that used behaviors to make a rag-doll looking effect.

Also, if the rotation is local, to the joint, why would it rotate the whole mesh?

You are where I was two post ago TD lol, he wants to modify the rotation of the bones for his ragdoll cause its not looking right when it jiggles around.

13

Re: Rotation offset

zester wrote:

You do know that when you start messing with the pivot point your bounding box for each object modified is going to change its scale and transformation right?

Not when you use the collision shape convex-hull.

You should more carefully read my posts BTW. You missed this important part:

start the physics context and enjoy how everything is working fine and the forearm swings

Which, in other words, means that my ragdoll code WORKS.
The problem is only when the mesh is a bit rotated (not facing the axis perfectly).
If I use a forearm mesh correctly rotated then my code does work. But I want my game to be able to accept and adapt to any meshes.

Normally you wouldn't even use a ragdoll in a game, they exist for...

You are again forcing me to say "I know my stuff". I can't write yet another wall of text just to explain all my project goals. This topic is just to ask specific help on a very specific task.

What you want doesn't have lua bindings yet "MArmature" and "MSkinData" I think "MSkinPoint * m_points;" is the  Bone's Transformation Matrice. If so thats what you would be wanting to tweek I do believe.
How is your C++?

Again, you have not read my posts carefully, since it's obvious that I'm using C++ and nothing else, with MArmature, MBones, etc.
This time you missed another line:

update the skeleton to match the rotation of the ragdoll part

Last edited by 255 (2013-10-15 18:06:12)

Re: Rotation offset

255 wrote:
zester wrote:

You do know that when you start messing with the pivot point your bounding box for each object modified is going to change its scale and transformation right?

Not when you use the collision shape convex-hull.

You should more carefully read my posts BTW. You missed this important part:

start the physics context and enjoy how everything is working fine and the forearm swings

Which, in other words, means that my ragdoll code WORKS.
The problem is only when the mesh is a bit rotated (not facing the axis perfectly).
If I use a forearm mesh correctly rotated then my code does work. But I want my game to be able to accept and adapt to any meshes.

Normally you wouldn't even use a ragdoll in a game, they exist for...

You are again forcing me to say "I know my stuff". I can't write yet another wall of text just to explain all my project goals. This topic is just to ask specific help on a very specific task.

What you want doesn't have lua bindings yet "MArmature" and "MSkinData" I think "MSkinPoint * m_points;" is the  Bone's Transformation Matrice. If so thats what you would be wanting to tweek I do believe.
How is your C++?

Again, you have not read my posts carefully, since it's obvious that I'm using C++ and nothing else, with MArmature, MBones, etc.
This time you missed another line:

update the skeleton to match the rotation of the ragdoll part

This isn't a pissing contest, don't take anything I am posting to heart, I post the information regardless if you know it or not, for myself and others to look back on. Someone might come along who's messing with ragdolls has a problem but doesn't particularly want to solve it the same way you are.

The code you post above was lua, that doesn't translate into "I am only using C++ and nothing" else that translates into "My lua code isn't working" if you already know about "MArmature" and "MSkinData" and are using C++ then you should know how to solve your problem. I posted every last bit of info I had on the topic.

Anael is the only one here who might be able to help you.

Last edited by zester (2013-10-15 18:21:12)

15

Re: Rotation offset

The code I've posted is not LUA, it's C++.

Re: Rotation offset

255 wrote:

The code I've posted is not LUA.

My bad your correct its C, It looked like Lua at first glance. I just read it to see what you were trying to do. Didn't notice you declaring types and the C++ "//" comments sorry. But regardless the info posted is still relevant.

Last edited by zester (2013-10-15 18:27:44)

17

Re: Rotation offset

zester wrote:

But regardless the info posted is still relevant.

Sure, I didn't get mad, I was just pointing out what you were not understanding. The lua-code-misinterpretation makes everything clear now. It's just hard to show our mood via forum text, but you can stay assured that my mood is always quiet and calm. big_smile

Re: Rotation offset

I think AxisYPoint or P and Q might be wrong,
they all need to be in the same coordinate, meaning global or local.

so, or you make AxisYPoint global, or you make Q and P local.

you can look at the code of the look-at behavior : https://maratis.googlecode.com/svn/trun … LookAt.cpp

19

Re: Rotation offset

I've tried using local points but no luck.
What kind of values does setAxisAngleRotation() expects for the angle? e.g. float from 0 to 359
Just to know if I have the angle right.

Re: Rotation offset

yes the angle is in degree,
the base of your code is ok, you do a cross-product to find the rotation axis
and a dot product to find the angle.

by the way there is some macro for degree/radians conversion in MCore :
DEG_TO_RAD and RAD_TO_DEG (ex : degree = radian*RAD_TO_DEG;)

after, I'm not sure to understand what you want to do with an offset
so it's possible that you have a problem on this part.

21

Re: Rotation offset

Yes, reading the source file that you've posted I noticed RAD_TO_DEG.

the base of your code is ok

Nice to know. What I'm trying to do is explained here: http://forum.maratis3d.com/viewtopic.php?pid=5545#p5545
Since the mesh file may have a mesh a bit rotated (not perfectly looking towards one of the axis), I need to adjust that.
How can I do it?
1) rotating the mesh itself editing the vertices
or
2) adding a rotation offset to the bone associated with the ragdoll part (what I'm currently trying to do).

My code for finding the offset is now the following. It only runs one time, at the ragdoll creation, and saves the offset data in a class which manages the ragdoll part (the forearm in this case).

//find the angle between the elbow and the wrist bones (I've tried both local and relative coordinates)
    MVector3 AxisYPoint = elbowBonePos;
    AxisYPoint.y = 1000;
    MVector3 A = AxisYPoint - elbowBonePos;  //vector A
    MVector3 B = wristBonePos - elbowBonePos;               //vector B
    A.normalize();
    B.normalize();

//find the angle
    float dp = A.dotProduct(B);
    float angle = acos(dp);

//find the sign of the angle
    MVector3 cross = B.crossProduct(A);
    if(B.dotProduct(cross) < 0)
        angle = -angle;

    ragdollPart->angle = float(angle * RAD_TO_DEG);
//find the axis
    ragdollPart->axis = cross;

The code for moving the bone is the following one (the first part is basically the code you gave me in another topic):

            //rotate the bone
            MMatrix4x4 myMatrix = character_using_the_ragdoll->getMatrix()->getInverse() * (*ragdollPart->entity->getMatrix());
            MObject3d * parentBone = bone->getParent();
            if(parentBone)
            {    //calculate object matrix in parentBone's space coordinate
                myMatrix = parentBone->getMatrix()->getInverse() * myMatrix;
            }
            //get rotation coords from myMatrix
            bone->setEulerRotation(myMatrix.getEulerAngles());


            //add offset (that I calculated before)
            bone->addAxisAngleRotation(ragdollPart->offsetAxis,ragdollPart->offsetAngle);

There is a lot of code of course that I'm not showing, where I set the physics properties and do other things, but everything is working. If I don't do this "offset thing" all the ragdoll is working fine and the arm is swinging around the pivot and the skeleton is correctly updated, but the skeleton forearm is slightly off in respect to the ragdoll part forearm, and this is what I was trying to solve.

Last edited by 255 (2013-10-16 13:35:01)

22

Re: Rotation offset

I did it!

        MVector3 cross = A.crossProduct(B);
        if(A.dotProduct(cross) < 0)
            angle = -angle;
        offsetAxis = cross;
        offsetAxis.normalize();
        offsetAngle = float(angle * RAD_TO_DEG);

I did

if(B.dotProduct(cross) < 0)

That's wrong. To find the sign you have to do the dot product of Vn and the cross product, where Vn is the vector normal, which is basically the axis that you first used for reference.

I also did the cross product wrong. It is not commutative so you have to be careful if you do AXB or BXA.

Last edited by 255 (2013-10-16 16:20:08)