Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
I used a script that fixes a problem related to horizontal rotations. Please modify it so it also works for vertical rotations!

wild-master
Registered User
Quote
2017-09-18 21:45:05

I found an answer to a previous problem here:
http://ambiera.com/forum.php?t=5...

A high-class person named "3dblendsphinx" shared a script that fixes it. (I used his second CCB file from that thread.)

This is the previous problem:
1. The Left and Right buttons rotate my object.
2. When it rotated past 180 degrees, the directions of the Left and Right buttons were switched.

The script from 3dblendsphinx fixes that problem. I want to modify it so it also works for vertical rotations, but my attempts have proven that I'm too inept to do that!

This is the script:
if(ccbGetCopperCubeVariable("MovingForward")==1)
{
/////////////////////////////////
//Let's get the player reference and position and rotation
player=ccbGetSceneNodeFromName("Player");
curPos=ccbGetSceneNodeProperty(player,"Position");
curRot=ccbGetSceneNodeProperty(player,"Rotation");

//The Y axis will tell us what direction the player is facing
playerDir=curRot.y;


moveX=(Math.sin(playerDir*Math.PI/180))*2.6;
moveZ=(Math.cos(playerDir*Math.PI/180))*2.6;
vecMove=new vector3d(moveX,0,moveZ);

newPos=curPos.substract(vecMove);
//let's move the player
ccbSetSceneNodeProperty(player,"Position",newPos);

}else if(ccbGetCopperCubeVariable("MovingBack")==1)
{
/////////////////////////////////
//Let's get the player reference and position and rotation
player=ccbGetSceneNodeFromName("Player");
curPos=ccbGetSceneNodeProperty(player,"Position");
curRot=ccbGetSceneNodeProperty(player,"Rotation");

//The Y axis will tell us what direction the player is facing
playerDir=curRot.y;


moveX=(Math.sin(playerDir*Math.PI/180))*2.6;
moveZ=(Math.cos(playerDir*Math.PI/180))*2.6;
vecMove=new vector3d(moveX,0,moveZ);

newPos=curPos.add(vecMove);
//let's move the player
ccbSetSceneNodeProperty(player,"Position",newPos);

}

if(ccbGetCopperCubeVariable("RotateRight")==1)
{
//Let's get the player reference and position and rotation
player=ccbGetSceneNodeFromName("Player");
curRot=ccbGetSceneNodeProperty(player,"Rotation");

vecTurn=new vector3d(0,1,0);
////Let's turn Right
newPos=curRot.substract(vecTurn);

ccbSetSceneNodeProperty(player,"Rotation", newPos);
}else if (ccbGetCopperCubeVariable("RotateLeft")==1)
{
//Let's get the player reference and position and rotation
player=ccbGetSceneNodeFromName("Player");
curRot=ccbGetSceneNodeProperty(player,"Rotation");

vecTurn=new vector3d(0,1,0);
////Let's turn Left
newPos=curRot.add(vecTurn);

ccbSetSceneNodeProperty(player,"Rotation", newPos);
}


And this is the part I added to enable vertical rotations:

if(ccbGetCopperCubeVariable("TiltUp")==1)
{
//Let's get the player reference and position and rotation
player=ccbGetSceneNodeFromName("Player");
curRot=ccbGetSceneNodeProperty(player,"Rotation");

vecTilt=new vector3d(0,0,1);
////The Player will be tilted upward
NewRotation=curRot.substract(vecTilt);

ccbSetSceneNodeProperty(player,"Rotation", NewRotation);
}else if (ccbGetCopperCubeVariable("TiltDown")==1)
{
//Let's get the player reference and position and rotation
player=ccbGetSceneNodeFromName("Player");
curRot=ccbGetSceneNodeProperty(player,"Rotation");

vecTilt=new vector3d(0,0,1);
////The Player will be tilted downward
NewRotation=curRot.add(vecTilt);

ccbSetSceneNodeProperty(player,"Rotation", NewRotation);
}


(I must split this post because this forum claims to have a limit of 4,096 characters for each post, but I posted this message with 4,096 characters and only the first 3,700 appeared, so the real limit is 3,700.)


wild-master
Registered User
Quote
2017-09-18 21:51:48

When the horizontal rotation is set to "0, 0, 0" or "0, 180, 0", it will rotate vertically correctly, but if its horizontal rotation is set to a number that's NOT divisible by 180, attempting to rotate it vertically will not produce the intended behavior.

Also, the horizontal movement that was fixed by the script from 3dblendsphinx WON'T work correctly when the character's vertical rotation is set to a number that's NOT 0, or 360, or divisible by 360!

Please tell me how to modify the script so it will work for all possible rotation combinations, instead of only working for characters that will only turn horizontally! I tried to modify the math section of the script multiple times, but the caliber of intelligence that's required to solve this problem seems to be far beyond what my frail mind can achieve.


erik
Registered User
Quote
2017-09-21 12:04:02

Huh, that's a lot of code to go into. I'll play around it and see if I can help you.


wild-master
Registered User
Quote
2017-09-21 18:48:53

New information:

I was using all of the code from the CCB file shared by 3dblendsphinx in my game, but I just realized that only this part is needed to fix the problem for Left and Right rotations:

if(ccbGetCopperCubeVariable("RotateRight")==1)
{
player=ccbGetSceneNodeFromName("Player");
curRot=ccbGetSceneNodeProperty(player,"Rotation");

vecTurn=new vector3d(0,1,0);
newPos=curRot.substract(vecTurn);

ccbSetSceneNodeProperty(player,"Rotation", newPos);
}else if (ccbGetCopperCubeVariable("RotateLeft")==1)
{
player=ccbGetSceneNodeFromName("Player");
curRot=ccbGetSceneNodeProperty(player,"Rotation");

vecTurn=new vector3d(0,1,0);
newPos=curRot.add(vecTurn);

ccbSetSceneNodeProperty(player,"Rotation", newPos);
}


My game wasn't even using the math part that I included in my first post. Please ignore the code for now and read the part below.

I just need to know why each of the three parameters for rotations don't consistently affect the same axis!

Please try this so you can understand what my problem is:
1. Open CopperCube and press "File", then press "New". Then press "New 3D App" and press "Ok".
2. Delete the sample object called "cubeMesh1"
3. In the "Scene Editing" tab, click the shape of a Cone.
4. A box will open that says "Create a cone". Press "Ok".
5. In the SceneGraph Explorer, click the object called "coneMesh1".
6. In its attribute box, change the rotation to 0, 0, -90.
7. That will be the rotation of my character during its normal movement.
8. If you want to know how I want it to turn when it looks upward, change the rotation to 0, 0, -45, and then to 0, 0, 0.
9. Now, please set the rotation back to 0, 0, -90.
10. Now that you are at the default rotation of my character, change the rotation to -90, 0, -90.
11. Imagine that position as showing how my character looks when it turns to look toward its right.

12. Now, imagine that my character rotates toward its right, and then imagine that I also want it to look upward again. Do you remember what we did to make my character look upward? We changed the Z rotation. We changed it from 0, 0, -90 to 0, 0, 0! The Z rotation changed from a -90 to a 0! So, now, my character is looking toward its right, and I want it to look upward again, so my normal mind believes I should change the Z rotation from -90 to 0 again, because that's what I did last time! Since the current rotation is -90, 0, -90, please change it to -90, 0, 0.

13. Look at the result! The character doesn't look upward! The Z rotation parameter previously had the power to control the vertical rotation of my character, but it lost that ability! After it rotated to look toward its right, it lost the power to look upward by changing its Z parameter! Do you want to know how to make the character look upward now? I'll tell you, because it's disgusting! To force the character to look upward, we must change the Y parameter of its rotation! Its Y paramter must change from 0 to 90, so the new rotation becomes -90, 90, -90!

Do you understand what my problem is now? Why did the parameter that can change the vertical rotation of my character switch from Z to Y? What should I do to be able to change the vertical rotation of my character at any time, regardless of its horizontal rotation? How do I disconnect them? I need a stable value that can be used to rotate my character upward or downward without being affected by its horizontal rotation!


pmax
Registered User
Quote
2017-09-21 19:49:18

I have not done the test but it seems to me that I have understood the problem of inverting the axes of rotation.
Search for information about the problem "gimbal lock".


wild-master
Registered User
Quote
2017-09-22 00:42:40

I searched the forum for "gimbal lock" and I found this thread:
http://ambiera.com/forum.php?t=3...

Niko tells the person to download the SDK for "CopperLicht" and use it to convert "quaternions" to "euler angles".

I downloaded the source code for CopperLicht, but I don't understand it because I haven't been educated as a programmer.

I found these online converters, but I don't know how to achieve my goal with them:
1. http://www.andre-gaschler.com/ro...
2. http://quat.zachbennett.com/

Even if I learn how to use the conversion ability of the source code of CopperLicht, I still won't know how to add a parameter in my game that can be used to rotate a character vertically at all times.

Is my goal impossible to achieve with this engine?

I've been trying alternate methods for the past four hours, like forcing the character to follow a higher object with the ccbAICommand(), but those attempts were unsuccessful.


tim12345
Guest
Quote
2017-09-22 19:44:38

hey that's my thread, haha. I was unable to figure out how to convert "quaternions" to "euler angles" because it was confusing and it wasn't a high priority. would be nice though.


3dblendsphinx
Registered User
Quote
2017-09-22 20:50:31

Hello, do you mean something like this? I fixed something I also notice with the other the Z was going the wrong direction. And cleaned the code up a bit.

https://app.box.com/s/i4du8gudsd...

It might be a long while that I respond back been quite busy.


wild-master
Registered User
Quote
2017-09-23 00:34:24

Hello! I tried your file. I want my main character to rotate like that, but your file still has the problem. If the guy is tilted forward or backward, his horizontal rotation is destroyed. Try rotating him all the way backward, and then try rotating him toward the right. His motion will be different! When you press the button for rotate him toward the right, he will rotate differently than how he rotates when his vertical rotation is not changed!


3dblendsphinx
Registered User
Quote
2017-09-23 00:59:15

I see what you mean. It would be easier if the object had it on local position versus just universal. Most engine have a local system for objects and then the world system. Coppercube everything is based on the world. If I have a chance I would look into it but can't make any promises.


wild-master
Registered User
Quote
2017-09-23 02:38:59

At this page, Niko said the engine uses quaternion rotations internally for the Artificial Intelligence code:
http://ambiera.com/forum.php?t=3...

When I read that, I was inspired and compelled to try to overcome our rotational problems:

1. I right-clicked the root node of my project, hovered over "Insert", and clicked "Create a Path". My path is called "Path1".
2. I added the Behavior called "Game Actor with Health" to my character. Inside of the properties section of that behavior, I chose the Mode called "Walk along a Path", and I chose "Path1" as the path it must follow.
3. Instead of moving my character with keyboard buttons, I use my keyboard buttons to move toward the Path1 node, and my character follows it!

With that method, my character follows the path and rotates toward it, but it follows EVERY movement of the path, instead of ONLY following its most current location.

After I was filled with sadness, I thought of this other method to force it to ONLY move toward the most current location of an object:

1. I created a sphere named "sphereMesh1" as an object.
2. Instead of moving my character with keyboard buttons, I move the sphere, and my character follows it.
3. I used a behavior called "Every few seconds, do something", and I put this JavaScript code inside of it:

var player = ccbGetSceneNodeFromName("Player");

var Sphere = ccbGetSceneNodeFromName("sphereMesh1");
var PositionOfSphere = ccbGetSceneNodeProperty(Sphere, "Position");

ccbAICommand(player, "moveto", PositionOfSphere);

With that JavaScript feature called ccbAICommand, my character follows the most current location of the sphere! The previous method that used the "Path" feature was worse because my character followed EVERY movement, like a trail, instead of only following the current location of the object.

But the method that uses the JavaScript code doesn't rotate my character! It moves toward the sphere in all vertical and horizontal locations, but it doesn't look toward my character! The method that used the "Path" method rotated my character!

I have a very important question here:
When I use the ccbAICommand code on my enemies, some of them actually rotate toward every direction perfectly! I use "moveto" to move them toward my main character, and some enemies actually rotate toward every vertical and horizontal position that my character moves to! But some enemies also just move toward my character without rotating!

Why does the "moveto" feature of the ccbAICommand feature occasionally make characters rotate while they're moving, and occasionally only moves them without rotating them so they'll face their targets? I'd share an example, but I probably don't still have a CCB file that shows some enemies that move WITHOUT rotating. I've tried debugging by setting the "Game Actor with Health" feature to "This is the Player", and then "Stand Still", and I've tried with characters that have cameras attached, and some that don't have cameras attached, and I haven't discovered why the "moveto" feature occasionally rotates objects while moving them and occasionally doesn't.

If we learn how to make the "moveto" option of the ccbAICommand rotate our objects every time, we could force characters to move toward tiny invisible objects that are very close to them, so we won't need to rotate them directly. People who play our games will still feel like they're controlling our characters! They'll never know that they're actually controlling tiny invisible objects and the main characters just walk toward them!

Tell me if my idea is idiotic or disgraceful!


just_in_case
Moderator
Quote
2017-09-23 18:55:43

It maybe possible that am wrong...... But i read the last post of this thread....

@Wildmaster.... You are saying that you want a chracter to rotate according to the another character in the game... Am i right?

If i gussed it right then.. let me tell u the method, i usee to play. Around few months ago... While i was busy in creating a behavior ... Path follow for my character..
But as the animated nodes doesn't have collision , so the calcularion to nearest path to reach enemy doesn't work out for me..


But the rotation and character follow works...

And i simply used ...


Var tiny_object = ccbgetscenenodefromname(tiny_object)
Var tiny_rotation = ccbgetscenenodeproperty(tiny_object,"Rotation")
Var player = ccbgetscenenodefromname (player)
Var player_rotation = ccbsetscenenodeproperty (player,"Rotation", tiny_rotation.x,tiny_rotation.y,tiny_rotation.z);



That way the player will rotate relatively with tiny invisible object in ur scene....

If possible i will tomarrow send u a ccb file too...

Just use the code with every fewseconds do something behaviour...

And if you wan to create your own Ai and then wants them to rotate with a fixed focua on another scenenode...

Then there are some more complicated ways to do it...

For example you have to calculate the position ...
If player position is negative and ai position is in positive or more than Player position... Then the rotation will be just negative of the player rotation...

Suppose enemy is standing in north... And palyer is also facing in the north direction ....
Then the enemy rotation must be negative ...so that it will follow the south direction.. amd face towards player...


Hope you undertsand what am trying to say...

Am not at all good in explaining...

Sorry for my bad english....



wild-master
Registered User
Quote
2017-09-24 04:49:02

Thanks for your attempt, just_in_case, but I had a different idea for the invisible object. I've been trying to find a solution for this all day.

I might be able to solve this problem if the "Parent" and "Child" system for Nodes would allow child objects to update their positions.

Example:
1. Imagine that I have an object that's located at this position: 200, 0, 400.
2. Now imagine that I set that object as a child of my character's node, so it always appears directly in front of it.
3. Even though my main character moves around, and even though the Child object always appears in front of my character, its position will always be reported as 200, 0, 400 when I use the ccbGetSceneNodeProperty() function.

The engine believes its position never changes, even though I see it moving around with my character!

I used a behavior called "Every few seconds, do something", then I set its interval to 1000 milliseconds, and a JavaScript code inside of that behavior uses the print() function to display the current position of the Child object.

And, every time the current position of the Child object is reported, it always says 200, 0, 400! It always reports the position that the child object
was located at from right before I set it as a child object!

I was so close to solving our rotational problem, but this shortcoming of the Child/Parent system stopped me! I was SO close! hahaha. I wanted to use the ccbAICommand() function to make the character move toward points that are always next to it, so, if i want it to rotate vertically, I would use the "moveto" feature of the ccbAICommand() function. It would rotate vertically forever because the Child object would always be above it, until I stop pressing the "Up" button, and the rotations would be perfect because the engine uses quaternions internally for rotations when the Artificial Intelligence features are used. The same situation would happen for all other directions. If I want my character to rotate left, I would use the "moveto" feature of the ccbAICommand() to move it toward the object called "LeftTurn". Since the object called "LeftTurn" is a Child object, my character would rotate left forever, because the object would always be toward its left, and it would only stop moving when I stop pressing the "Left" button. This method would be easy to achieve if Child objects would receive updated vector3D positions, but they don't.

Are any of you aware of a method that can attach an object to my character's position and rotation that will also allow the engine to capture its new position? Example: If I want an object to always appear at the left side of my character, it always WILL, exactly like how the Child/Parent system works, but using the ccbGetSceneNodeProperty() must also provide its new location.

I also tried the Action called Change position of a scene node, and I set its position type to Set relative to a scene node, and it doesn't do what I need because its X and Y and Z positions don't change along with my character's movements (they stay relative to the whole world's XYZ coordinates).

As a final contingency plan, if I must cease my attempts and admit to my demise by using the "First Person Shooter style controlled" Behavior, please tell me how to disable the WSAD keys. I want to create my own movements. Can the WSAD keys be disabled while using the First-Person Shooter behavior? Can I free the WSAD keys so I can use them for other tasks?

I appreciate the help from all of the prestigious people on this forum! I've been stressing your minds for weeks!


just_in_case
Moderator
Quote
2017-09-24 05:45:25

Maybe you got me wrong.... My method do the exact same thing you want to do just like the child and parent thing..but the problem is thart they are not really parent and child ....

But the object will behave as a child node ...

For example if you want it to follow the chracter from left...
It will always follow from the left...
And the print command will always print the new position too...

Let me send you my ccb file...


just_in_case
Moderator
Quote
2017-09-24 12:10:55

Here is the sample ccb file in which a cube follows another cube with respect to the rotation of the another cube...

https://drive.google.com/file/d/...


Create reply:


Posted by: (you are not logged in)


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