# Making character look at point

Hi, all!

Still fighting this for a long time now, but got zero success.

I want my character to turn his head, neck, chest and eyes to the point in 3D space.
This character can have any pose at that time and play animation.
Lets consider poses where head is relatively vertical (more vertical than horizontal, or can be made so
without much effects).

So I need:

1. turn head to vertical position
2. and then turn in direction of target.
3. turn chest half-way
4. turn neck half-way

Problems:

1. Rotations are relative and accumulative, each one creates its new coordinate system.
When quaternion multiplication is done, it is done in order from left to right,
so if full transform of head is q1 * q2 * q3 *q4 , only q1 rotates in global space, all the rest
rotate in previous quaternion space. With lots of them it becomes very hard to track target rotation.
Also the transform is non-commutative, which means order is important. So if I want to convert my rotation from global space to local space, I will need to multiply it by inverse rotations in reversed order.
2. To rotate something in global space I will add my rotation at beginning of sequence, it will rotate the whole thing globally. so if q is current rotation of head and qdelta is additional rotation I need to apply, I just do qdelta * q
to get q rotated in global space. But general target is usually a bit different - we need to do local rotation
so that it comply to global goals. So we need to calculate local delta for each rotating bone, and that is where
biggest problem comes.
Initially we have some animation running, with head facing front. We have some global point in space, to which we want to look. We have some global node master which represents global character trasform in space.
Each bone contributes to that transform using its own quaternion each, having head as final.
So having our initial delta rotation, we somehow need to convert it to local rotation of head bone.
The simple approach of multiplying by parentās world transform doesnāt work. That is where I stopped.
Tricks like Rotate(x, TS_WORLD) do not help either, so I think the problem is deeper.

This kind of look-at āIKā works quite well in other engines, like Unity, UE4, Godot. But not in Urho.
Probably there is some technical detail about quaternions in Urho, which make it different,
so Iād like to know them. Please note that I look for generic solution, not something which assumes vertical position
or Z-facing. I assume work in any random initial pose (which makes sense for look-at). In my case I think the problem
is solved when character can look-at from head-on pose in addition to standing and sitting.
The challenge looks quite tough.

Your question is too long. Donāt know if anyone will read it.
You donāt criticize an engine when you donāt know how to use it. These stuff takes time and need patience. Learning takes time.
If you understand the theory correctly, then all engines are the same, including this.

You problem is about inverse kinematic and are dealing with a chain of maybe 3 bones. By the way, why would a person rotate his spine when looking somewhere? This is unnatural pose. We donāt do this in practice. Unless in extreme combat condition.

Hereās the simplest solution for you.
Just attach another node to the head. Then translate in z a little.
Then use inverse kinematic code from Urho.

I donāt know why you say so strange things when we look at something we use whole body.

Inverse kinematics in Urho is not yet ready to be used yet. That is why I invent my own wheel.

As for criticizing - I think I have full right to, also, I did not criticize it in this thread at all, just said Urho is different
from other engines regarding this, as parent 's world transform inverse do not produce local coordinate space.

I know about long questions and tl;dr;, Iām just going to continue speculation about transformations here in this thread.
The conditions and requirements are quite long.
My current head pose is almost default, i.e. facing -Z. but up -s +Y. This looks like this particular heading
is really a key to my problems, as it looks like the rotation have problems exclusively for head, (and neck as it have the same orientation). Chest doesnāt have this problem as it is Z-front and rotate properly.

Iād say it is first engine I see which is so much depends on everything being Z-front.

It is absolute standard for any engine Iāve ever worked with that all objects (especially animated ones) have the same facing or problems will ensue. And it is quite logical, since otherwise youād have to manually rotate everything before usage - not even speaking of more complicated things like IK.

Besides, I just find it a good standard to export every asset facing the same way. More professional than just have things with a random facing

No, that is not.

1. in Start() I check head facing - if I do GetWorldDirection() it faces Vector3::FORWARD.
No animation is played, everything is default.
All quaternions are identity (1 0 0 0)

2. In HandleSceneDrawableUpdateFinished
Sitting animation is played.
Rotating options do not work correctly.
If compensated by quat.FromRotationTo(Vector3(0, 0, 1), Vector3(0, 0, -1));
look start to work.

There is no such behavior in Unity, and UE4 with the same assets. Look works straightforward.

So you tell me complete bullshit without any digging into the problem. Please stop doing that.
Unless you see where the problem might be, from now on I will consider such posts direct aggression
and reserve all right to react in any way I wish.
You did not even care to read my previous posts, you did not care at all to understand the nature of problem
and after that you tell me I did everything wrong. I really hope your karma will hit you in very bad way.

The engine behavior is inconsistent and unpredictable, so there is no way to manually compensate
bone rotation without hardcoding values. I canāt do all animation retargeting and all bone changes to face Z
as that would be enormous waste of time. I try to compensate from code. And that doesnāt really work,
while all information should be available to compensate either automatically or from application code.
Looks like everything is fine unless animation is played, which turns nodes in weird ways. But I wonder why
the actual node weird rotations do not affect model? why node world space suddenly change?

it looks like changing bones sometimes lead to mess when animation is played.
Anyway I found universal solution to fix this for general skeleton case.

1. Requirements - character faces Z, skeleton is irrelevant, bone facing is irrelevant.

2. Creating child note for head, but setting world position to node_->GetWorldPosition() + node_->GetWorldDirection() * 10.0f;

+ node_->GetWorldDirection() * 10.0f);

3. calculate 2 directions - facing and look

4. Calculate rotational difference
Quaternion quat;
quat.FromRotationTo(facing_dir, target_dir);

5. Rotate to difference:
`head->Rotate(quat, TS_WORLD);`

This canāt be considered a full solution, as it is mere workaround for a bug.
But it might be useful to somebody.

So, you mean in Unity and UE4, you can just import any asset, no matter how itās facing was done when exported (positive/negative Z, or even positive/negative Y), attach it to any amount of bones and yet they all magically work, facing the same/correct direction without needing any kind of manual adjustment?

Quite honestly, I just donāt believe that. Meshes are just 3D vector positions, even the idea of āforwardā is foreign to them (if not some kind of new format came up that explicitly stores a āthis is forwardā information with the mesh). An engine has no way of knowing what āforwardā means for any 3D asset. It just has a default assumption (which differs for each engine, though most go with forward or negative Z).

Or are you talking about something completely different?

If you regard a golden rule in 3D development sinceā¦ wellā¦ foreverā¦ as complete bullshit, just go ahead and see where it takes you

[quote=āslapin, post:6, topic:3227, full:trueā]Unless you see where the problem might be, from now on I will consider such posts direct aggression
and reserve all right to react in any way I wish.
You did not even care to read my previous posts, you did not care at all to understand the nature of problem
and after that you tell me I did everything wrong. [/quote]
Oh, I did read this thread, but it is hard to tell what exactly is your question or problem. From what I got, everything just points to the same thing: Something does not face the direction you expect it to after a number of manipulations. There can only be three reasons for this:

1. You got your math wrong - since I didnāt get what exactly you are even trying to do, I did not comment on this.
2. If your math is right, your assets are wrong. If your assets face different directions, the situation is requiring a fix of the assets or a fix in code (which I think you ended up doing?). From what you said, it doesnāt sound like all assets you are using use the same direction of āforwardā. From experience, this can easily explain your problems.
3. Thereās actually a bug in Urho. Perfectly possible, of course, but wouldnāt that have been noticed already, in such an important system as animation?

If that happens, I will just hit it right back. With a sledge hammer
Nobody is allowed to hit me in very bad ways, especially not cosmic forces!

3 Likes

To test your code, you could use a trick: switch your asset for something you know for sure it works. You could use the jack or ninja that comes with the examplesā¦
If your code doesnāt work with them, you can be reasonably sure the flaw is in your math.
Contrariwise, if they work, you can be reasonably sure the flaw is in your model.
This left with an option out: the flaw is in the engine. Perfectly possible. But that need you to share a replicable procedure for someone else to testā¦

(offtop)

I can start a list of āinconsistent and unpredictableā things in the Urho that finally ended up as misuse or misunderstood

1. Degenerate trimeshes cause assert in physics
2. Quaternion math is surprising
3. (this topic subject, TL;DR)
4. (reserved for the future)

Yes, I speak about different thing. My assets are in general Z-facing (front facing) when exported,
but some bones are not, and that seems normal for other engines.

I have no problem manually rotating bones in any other engines, with animation or not.

I have really have trouble isolating this, as Iām pretty sure there is bug somewhere, but I canāt
really find where, otherwise I would just report it to a bug tracker.

Iām really open for discussion of what Iām doing wrong, but that should be quite specific thing,
not speculation.
I do bone rotations in same place IK does them (as my code is kind of IK). I calculate delta difference
between target and current bone rotation and apply that difference to a bone. That is simple.
The proble mis that bone looks at completely wrong place. Research shows that somehow bone facing direction
changes during course of action, as in Start() function the bone is Z-facing, at the event handler the bone becomes
anti-z facing. I calculate this using quat.FromRotationTo(Vector3(0, 0, 1), node_->GetWorldRotation() * head->GetWorldDirection()); in 2 places. At Start() I get identity quat, at second place I get inverted quat.
So this looks actually like a bug, but I donāt know where it lives.
The positioning of node does not change, the only difference is if animation is playing or not, visually head is facing the same spot.

if I change my IK code to use relative-only positioning (like in one of previous posts) everything works like a charm.
That is full story up to current moment. The rush is now lowered, as I can now have look implemented, but I need to
add child node to each involved bone to indicate front side, in this case everything works great. I donāt like it but
I could not find better way yet.

1. A bullet bug. The PR is in queue.
2. Not the math, but some engine approach to it.

Why do you even bother writing?
If you feel sad that I do not respect peopleās work, donāt be.
If you consider software as being sacred that nobody can speak ill of,
I think you should not be in industry. And yes I do respect work of
others and expect the same of mine. And know that errare humanum est,
so I should never trust software neither mine nor someone elseās.
And there are bugs. Everywhere. Lurking.
So if you are not helping, please donāt bother to interfere at all,
Iām sure you can find much better and interesting things to do
than frustrating me. If you can help, Iām would be really thankful,
if you do.

Actually, Iād like to help, but this thread is too big and I donāt have enough inspiration to read it. Iāll do it later.

That is really fine by me, donāt rush. I have workaround, so I can manage. The hobby should be fun, no schedules,
lots of beer!

1 Like

Try to walk with your shoulder, arm and spine rotate like that. It is not natural pose.you will get injury.

Simple video showing unconstrained one bone head look at from code I pasted responded to one of your other post.
You can see that Urho works just fine. Just use the Fabrik inverse kinematic code, or you could implement it in less than 100 lines of code for the ik code base itself.

1 Like

Hey George, do you have the source of that sample?

Hi,
I just edit the SkeletalAnimation sample.

Urho3D::Node *man [100];
Urho3D::Node *box; //Create this box somewhere.

Inside generating 100 Jack models I do these.

``````man[i] = modelNode;
``````

Inside update loop I do this.

``````for (int i = 0; i < 100; ++i)
{
Vector3 dir1 = man[i]->GetWorldDirection();
Vector3 dir2 = box->GetWorldPosition() - head[i]->GetWorldPosition();
Quaternion delta(dir1.Normalized(), dir2.Normalized());