Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Help with CopperCube
How do I rotate an enemy toward my character? I searched this forum and I've been researching JavaScript for the past few hours.

wild-master
Registered User
Quote
2017-09-10 21:02:55

I want an enemy to rotate toward my character with all three dimensions (X, Y, Z), so it will look toward me if I move to a side, and it will rotate upward if my vertical position changes.

I found an idea in this thread:
http://ambiera.com/forum.php?t=3...

I tried to implement it, but I didn't know how to do it correctly, and it's only a 2D method.

After that, I found this thread that mentions the "Target" property of camera nodes:
http://ambiera.com/forum.php?t=5...

It says I might be able to rotate enemies by attaching them to cameras and rotating the cameras by using the "Target" property. I already have a camera on my main character with the "First-Person Shooter" movement, and my character turns when the camera turns (with the mouse).

Before I attach a camera to every enemy, I want to know if a better method exists.

I've been searching the internet for the past few hours for a method of doing this.

I thought of using the Cube Root of my main character and forcing the enemy to rotate toward it, but the "Rotate" feature of this engine requires three inputs (Vector3D) instead of one number, so I cannot force the enemy to rotate toward the Cube Root.

Please share a method of rotating an enemy toward a character!

It must be a method that can use all three dimensions.

Imagine turning left if a person walks toward that direction, but refusing to look upward if he is above you! You should turn your head with all three dimensions! Haha.

My enemy is a not a person. It's a simple object, and I want the "front" of it to face my character's position (X, and Y, and Z).

If that cannot be done, I will attach a camera to every enemy and use the "Target" property to rotate them!

Having hundreds of cameras in my game will be a stressful long-term maintenance problem, so I'll be thankful if a better method is shared!


niko
Moderator
Quote
2017-09-11 16:15:27

The three values you have in this engine are euler angles. I bet you will find a method on the web which could easily create a quaternion from the direction you need to have the wanted rotation. If you have that, convert the quaternion to an euler angle, and then set it. That's how it is very easy to do, usually.


wild-master
Registered User
Quote
2017-09-11 19:13:15

When you say "create a quaternion from the direction you need", what do you mean by "the direction you need"? How do I obtain the number that portrays the direction I need?

I tried two methods:

This is the first method I tried:

As a test, my enemy starts with this rotation: 0, 0, 0, and I put a character directly behind it. (I aligned the character with the enemy perfectly with the "Top", "Front", and "Left" views of CopperCube.)

To force the enemy to look at the character behind it, its rotation must be changed to this: -180, 0, 0

The character's position is -29.984121, 27.086069, 663.727173

I typed that position into the lowest box on the bottom-left side of this calculator, with the "XYZ" parameter:
http://www.andre-gaschler.com/ro...

That calculator converted that position to this quaternion: 0.4177714, -0.5927261, 0.1601791, -0.6696906

That calculation also shows a euler angle as one of the results, and it appears like this:
Euler angles (degrees) XYZ: -97.9635856, 68.0825621, 48.7657594


I also entered that quaternion into this calculator:
http://quat.zachbennett.com/

That calculator converted the quaternion to this euler angle: -155, -86, -49

But the correct answer is -180, 0 , 0!

This is the second method I tried:

You said I should convert the direction I need, so, since you might be referring to "-180, 0, 0" as the direction I need, I typed that into the lowest box on the bottom-left side of this calculator, with the XYZ parameter:
http://www.andre-gaschler.com/ro...

That calculator converted that rotation to this quaternion: -0.8939967, 0, 0, -0.4480736

That calculation also shows a euler angle as one of the results, and it appears like this:
Euler angles (degrees) XYZ: 126.7596877, 0, 0


I also entered that quaternion into this calculator:
http://quat.zachbennett.com/

That calculator converted the quaternion to this euler angle: 0, 0, 53

That method also doesn't display the correct answer!

The quaternion should be able to discover this rotation: -180, 0, 0

Did I not follow your instructions correctly?


niko
Moderator
Quote
2017-09-12 08:21:35

Hm, not entirely sure. I should probably implement it myself. But I just found an old extension action written by Jaime: If you paste this into a text file and save it as action_LookToObject.js in YourDocuments\CopperCube\extensions\ (then restart CopperCube), you have an action which causes the node this action is attached to to look to a target node. Just checked, it seems to work.


/*  <action jsname="action_LookToObject" description="Look in direction of target object 3D">
<property name="targetNode" type="scenenode" />
</action>
*/

action_LookToObject = function()
{
};

action_LookToObject.prototype.execute = function(currentNode)
{
var position_target = ccbGetSceneNodeProperty(this.targetNode, "Position");
var position = ccbGetSceneNodeProperty(currentNode, "Position");
var rotation = ccbGetSceneNodeProperty(currentNode, "Rotation");

pos_o_x=position_target.x-position.x;
pos_o_z=position_target.z-position.z;
pos_o_y=position_target.y-position.y;
var dist=Math.sqrt( Math.pow( (position.x-position_target.x) ,2) + Math.pow( (position.y-position_target.y) ,2) + Math.pow( (position.z-position_target.z) ,2) );
var pi=Math.PI;
var look_angle_horz=( (Math.atan2(pos_o_x,pos_o_z) )*180/pi )-180;

var look_angle_vert=( (Math.atan2(pos_o_y,dist) )*180/pi );

ccbSetSceneNodeProperty(currentNode,"Rotation",look_angle_vert,look_angle_horz,rotation.z);
}



wild-master
Registered User
Quote
2017-09-12 19:30:44

It seems to work!

Now I want to know how to modify my 3D object so the engine knows which part I want to be the "front".

Example:
I can rotate it to "90, 0, 0", but Jaime's extension seems to still use the normal rotation of "0, 0, 0" to decide how to rotate the object when it "looks" at my character.

How can I let the engine know that I want the rotation "90, 0, 0" to become the new position of my object that gets labeled as "0, 0, 0"?


wild-master
Registered User
Quote
2017-09-12 23:01:13

I used a free program from Microsoft called "3D Builder" to change the default rotation of my object, and Jaime's extension works well with it!

I assumed that I could choose the default rotation for "0, 0, 0" from within CopperCube because I was using one of the simple 3D shapes that are included in the engine.

You should put that extension on your official page of Actions & Behaviors because people have wanted this ability for years!

It's a masterpiece!


wild-master
Registered User
Quote
2017-09-13 05:51:21

The extension is almost perfect, but I just noticed that the rotation is instant, and I want the enemy to rotate with a realistic speed!

If you want to see a person who is behind you, your body won't instantly be turned within one millisecond! You'll require at least one second to turn around!

CopperCube has a built-in action called "Change rotation of a scene node" that has a checkbox that says "Rotate Animated", and it allows me to decide how long the character will need to turn, so it won't be instant.

But your JavaScript manual just shows how to change the rotation of a character like this:
ccbSetSceneNodeProperty(currentNode,"Rotation", x, y, z)

The manual doesn't show a way to use the "Rotate Animated" feature with JavaScript.

I thought of storing the value from Jaime's extension in a variable, and typing the name of the variable in the "Vector" box of the "Change rotation of a scene node" Action, but that box just shows "0.0, 0.0, 0.0" and doesn't seem to be capable of calling a variable.

Do you know how I can use the "Rotate Animated" feature of your built-in Action with the XYZ rotation that is discovered with Jaime's extension?

Would it require you to extend your JavaScript feature with a software update?

This is an example of a possible future capability:
ccbSetSceneNodeProperty(currentNode,"Rotation", x, y, z, EnableTheRotateAnimatedFeature, 2000 Milliseconds)



Zoo
Guest
Quote
2017-09-13 07:57:21

Extension added to MTM https://marshtownmadness.com


wild-master
Registered User
Quote
2017-09-14 02:08:01

Niko, I found another problem.

I want some characters, like vehicles, to always move toward the direction they're looking at, so I want to use the "Move by vector into facing direction" feature, but the side of the object that CopperCube chooses as the "facing direction" isn't the same as the side of the object chosen by Jaime's extension as the "front".

So, even though my enemy looks toward my character with the extension, it moves toward its left when I use the "Move by vector into facing direction" feature! It looks at me while moving left!

I want Jaime's extension and the "Move by vector into facing direction" feature to both use the same side as the front!


To summarize, these are my remaining issues:

1. I want to be able to use the "RotateAnimated" feature with Jaime's extension, so the rotation of my characters won't be instant.

2. I want the side of my 3D object that is chosen as the "front" by Jaime's extension to also be the same side that's chosen as the "front" by the feature called "Move by vector into facing direction".


wild-master
Registered User
Quote
2017-09-15 23:55:14

I found the solution! I feel like an imbecile for wasting Niko's time!

I attached this behavior to my enemy: Game Actor with Health.

I also added this behavior: Every few seconds do something, with these settings: interval MS: 30 milliseconds, Action: Execute JavaScript.

I added this JavaScript code:
var Player = ccbGetSceneNodeFromName("MainCharacter");

var Enemy = ccbGetSceneNodeFromName("enemy");

var PositionOfPlayer = ccbGetSceneNodeProperty(Player, "Position");

ccbAICommand(Enemy, "moveto", PositionOfPlayer);

That causes the enemy to always follow me. It also always rotates to look toward my character. The speed of its rotation is realistic, instead of magically instant. A setting for "Rotation Speed" should be added to the "Game Actor with Health" Behavior because people might want to create vehicles that can move very fast, but can only turn slowly.

Example: I set the enemy's speed to 50, and then I set it to 1, and its rotation speed was the same for both. It could move slower than a snail, but it will still rotate quickly if I move behind it. Haha!

Game Actor with Health also has a setting called "Addtional Rotation for Looking". It allows us to decide which part of a character is its front, so we won't have modify our 3D characters in external programs to reorient them.

I love CopperCube!


niko
Moderator
Quote
2017-09-16 06:21:09

Ah, nice :)


help
Guest
Quote
2018-07-21 14:57:03

How about clones?


help
Guest
Quote
2018-07-21 15:08:44

Could you use a string like this.gameobject?


faith_254
Registered User
Quote
2018-08-20 09:10:05

after doing the revised code
i cant kill my enemy now


faith_254
Registered User
Quote
2018-08-20 09:10:41

the revised code as
var Player = ccbGetSceneNodeFromName("MainCharacter");

var Enemy = ccbGetSceneNodeFromName("enemy");

var PositionOfPlayer = ccbGetSceneNodeProperty(Player, "Position");

ccbAICommand(Enemy, "moveto", PositionOfPlayer);



Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internation?l" (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