Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Help with CopperCube
Make a character look at player (me)

ryandavanzo
Registered User
Quote
2023-09-09 21:01:39

I need a character to look into the player's soul while talking to him and never look away. How do I do that?


Dieter
Guest
Quote
2023-09-10 15:25:01

I think there as a AI function (actor behavior) that does that.

However, I would suggest you learn the underlaying Math, so you can do this in any engine, by your own

first determine the angle between the two, like if the player is the center, at which angle stands the npc, if it were the finger of a clock.

xyz1=ccbGetSceneNodeProperty(player,"Position");
xyz2=ccbGetSceneNodeProperty(npc,"Position");

// distances on x and z axis
xd=xyz1.x - xyz2.x;
zd=xyz1.z - xyz2.z;

a=Math.atan2(zd,xd);
// Notice, atan2 returns the wrong angle system for CC, so multiply the result by a factor of 57.29577951
a*=57.29577951;

// now you can simply rotate the player Y axis using a, and rotate the npc using (a+180).

ccbSetSceneNodeProperty(player,"Rotation",0, a, 0);
ccbSetSceneNodeProperty(npc,"Rotation", 0,a + 180, 0);

Maybe you have to switch some variables here, this is pseudo code, untested, but should work after some tweaking.


ryandavanzo
Registered User
Quote
2023-09-10 15:36:25

I mean, the NPC is an imported animated mesh. Will it work with that too?


Dieter
Guest
Quote
2023-09-10 15:44:53

It's actually some months ago that I worked with animated NPCs in CC. As far as I remember I was using them as a child of a "folder node" anyway, that's basically just a pivot that the NPC is stuck to, and you move and rotate that folder node, not the mesh.
Yes, should work.
Also maybe compare the amount of rotation between two frames, and when significant, start a "step on place" animation, so it looks kind of natural.


Dieter
Guest
Quote
2023-09-10 16:08:19

Oh btw. if the player is the camera (as in fps), as far as I remember you can't simply rotate the camera. Instead one has to position the "Target" somewhere around the camera, so the latter will look in that direction. I must admit this is a bit of a hassle.

However, you can just inherit the current Y position of the target, and then set X and Z according to the previously obtained angle a (as in prev. post). Herein also lies an important lesson in regards to sinus, cosinus, atan2 and angles generally, fundamental game design skills.

xyz=ccbGetSceneNodeProperty(ccbGetActiveCamera(),"Target");
ccbSetSceneNodeProperty(ccbGetActiveCamera(),"Target",Math.sin(a)*5, xyz.y, Math.cos(a)*5 );

Positions the camera target in the direction of the NPC, by a radius of 5.


Dieter
Guest
Quote
2023-09-10 16:15:01

Well, right now I'm not even sure whether the property "Target" can be used to position the target directly, or whether it just returns a node handle that has its own "Position" etc. properties.
But I guess it's for positioning directly. Sure somebody will correct me here if I'm wrong, thanks.


ryandavanzo
Registered User
Quote
2023-09-11 01:20:42

so basically, put a object behind the camera and make the npc look at it?


just_in_case
Moderator
Quote
2023-09-11 06:53:29

@Dieter, well you can rotate the camera. There is an option in CC, if you go to irredit/irrlicht properties while having the camera selected you will find an option called Binding you can simply turn that ON , and now your camera target is binded with the rotation of the camera.

As you work mostly with webGL, I keep myself away from answering to your post because webGL is less explored by me, so I am not sure if things will work there or not. Like the TOP-View camera never works in webGL, it will not render anything if you have a camera facing directly down at a perpendicular.

@ryandavanzo, no you don't need to put an object behind the camera and make the npc look towards the object. You can directly use the camera node position and make the NPC look to the camera position always.

as @Dieter already suggested there are actions available to do this.

You can download the extension from my website here. https://vazahat.github.io/ccb_ac...

hope this helps


Dieter
Guest
Quote
2023-09-11 16:19:35

Thanks, just_in_case, I didn't know about the option to bind the target to the camera. Actually never checked the irrEdit settings of the camera, as I considered it a material editor.

@Ryan, you can try the suggestion of just_in_case.

Either way, you would be right if you do it the way I first suggested, by positioning the target to force the camera to look that way. The radius or distance doesn't matter, it's just about the angle. And the target is invisible, part of every camera.

More important tho is the Math, on how to obtain a 0 to 360 degree angle from two XZ positions, so they can be pointed at eachother.
You can use CC internal prefab functions for that, but I think every games designer should understand the atan2 stuff.


Dieter
Guest
Quote
2023-09-11 16:23:43

Correction, rather than:

so basically, put a object behind the camera and make the npc look at it?


it would be:
put a object behind the npc and make the camera look at it. Then take the angle of the camera, add 180 and rotate the npc accordingly. It will now have the mirror angle of the camera, resulting in looking directly at it.


Dieter
Guest
Quote
2023-09-11 16:27:22

Oh btw, you could also simply position the target at the NPCs location. Preferably between his eyes.

Then you'd still have to rotate the NPC Y axis to the one of the camera, plus 180.


Dieter
Guest
Quote
2023-09-11 16:39:25

Ok, sorry for these tons of short messages, I should log in and edit, I know...
Anyway, so far I assumed you want to force the camera too, to look at the NPC. But that may not be the case.

If so, then you would just use the atan2 thing in reverse. Consider the NPC to be the center, determine at which angle it stands to the player, and rotate it so, but leave the camera / player untouched.


ryandavanzo
Registered User
Quote
2023-09-11 16:55:33

it's ok about the messages. but the thing is...the player WILL keep moving, so I need the npc to constantly keep changing it's rotation to face the player (FPS btw)


just_in_case
Moderator
Quote
2023-09-11 17:27:47

@ryandavanzo, you can use every few seconds do something behavior and attach the action to that behavior and then simply select your NPC for the option Scenenode and the camera or player in Target , there is also an additional rotation property if your NPC is looking in the wrong direction then you can adjust it with that option.

@Dieter, the actions that I mentioned uses the same math of atan2 for rotating the object towards another object. But you are right everybody should be aware about using those maths functions.


Dieter
Guest
Quote
2023-09-11 17:37:37

Just decide whether to try the action addons just_in_case has linked to, or try the atan2 thing yourself.

Now the application point is where people often struggle. I would suggest you attach a proximity behavior to the npc, when the player enters his area, a ccb variable is set to 1. If he leaves the area, the variable is set to 0.

ccbSetCopperCubeVariable("npc_near", 0);


Then have a behavior in the root scene node, to do something every few seconds. Choose something like every 100ms.
Make it execute a javascript (special > execute javascript), that would do something like:

npc_near=ccbGetCopperCubeVariable("npc_near");

if(npc_near==1)
{
player=ccbGetActiveCamera();
npc=ccbGetSceneNodeFromName("name_of_your_npc_in_the_editor");
xyz1=ccbGetSceneNodeProperty(player,"Position");
xyz2=ccbGetSceneNodeProperty(npc,"Position");

// distances on x and z axis
xd=xyz1.x - xyz2.x;
zd=xyz1.z - xyz2.z;

a=Math.atan2(xd,zd);
// Notice, atan2 returns the wrong angle system for CC, so multiply the result by a factor of 57.29577951
a*=57.29577951;

// now you can simply rotate the npcr Y axis using a

ccbSetSceneNodeProperty(npc,"Rotation", 0,a, 0);

}



Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internati?nal" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact