Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Help with CopperCube
Weird things happening with Javascripted Rotation and Movement Controls

en929
Registered User
Quote
2017-07-10 18:04:20

I don't use the default controls that comes prepackaged with Coppercube. I try to set up my own controls using the:

"When key pressed do something" ---->Choosing a key----> to Action (not set) ----> to click the + button ------> to "Special" -----> to "Execute JavaScript" -----> to Clicking on the "|...|" box -----> to Inserting my JavaScript code in the box.

I was fairly successful with it and I use the following code below to add up, down, left, and right movement to my character (with a matter of changing the "pos3d.x" part to fit with where I want to go and the key that I press):


  


ccbRegisterKeyDownEvent("keyPressedDown");

function keyPressedDown(keyCode)
{
var cube = ccbGetSceneNodeFromName("cubeMesh1");
var pos3d = ccbGetSceneNodeProperty(cube, "Position");

pos3d.x += 2;

ccbSetSceneNodeProperty(cube, "Position", pos3d);



}




And I use this code below for my rotation as for when the "A" button is press (using the same procedures above) :




ccbRegisterKeyDownEvent("keyPressedDown");

function keyPressedDown(keyCode)
{
var cube = ccbGetSceneNodeFromName("Henry");
var pos3d = ccbGetSceneNodeProperty(cube, "Rotation");

pos3d.y += 4;

ccbSetSceneNodeProperty(cube, "Rotation", pos3d);



}




Oh and by the way, I got the above two codes from some of the tutorials. I'm no real expert and I didn't write the above codes myself. I actually got the above code from here:
http://www.ambiera.com/coppercub...

However, the problem that I have seems to be with rotation. When I press the arrow keys BEFORE rotating, my character moves normally. But, I when I press the rotation button my movement keys tend to shift. That is, originally when I press up button, my character goes forward, and when I press down my character goes backwards. But, when I my character is ROTATED, when I press the up button suddenly my character moves right and when I press the down button suddenly my character moves left, etc. The same thing happens when I try to add rotation by not coding, but using the preset commands. This only happens when I try to rotate my character. It's weird. Thus, how do I fix this? Maybe I'm using the wrong code to get to what I want or maybe something needs to be added to my code to get the buttons to work properly. But, I don't know what to put or how to do it. Thus, what do I do. Thanks!


3dblendsphinx
Registered User
Quote
2017-07-10 19:17:36

What you copied from doesn't take in consideration vectorial changes.
It was meant for one direction. This is why you have the weird result.

🔎︎

🔎︎


So to get this
🔎︎



you need to include calculation like this-


/////////////////////////////////
curPos=ccbGetSceneNodeProperty(this.AttachMode,"Position");
curRot=ccbGetSceneNodeProperty(this.AttachMode,"Rotation");
playerDir=curRot.y;


moveX=(Math.sin(playerDir*Math.PI/180))*0.6;
moveZ=(Math.cos(playerDir*Math.PI/180))*0.6;
vecMove=new vector3d(moveX,curPos.Y,moveZ);




I still working to have it move at a good pace but you can play around with this.

**Also just a side note if you are doing your own models to make sure that they are facing the +Z direction (In Blender -Y). So when zero it would face the +Z direction of the CopperCube world.


en929
Registered User
Quote
2017-07-15 17:05:37

3dblendSphinx, I tried to include the code that you wrote above, but I couldn't get it to work correctly. My character's controls still alters when I try to rotate it using the A button. This is what I wrote:





ccbRegisterKeyDownEvent("keyPressedDown");

function keyPressedDown(keyCode)
{
var cube = ccbGetSceneNodeFromName("Henry");
var pos3d = ccbGetSceneNodeProperty(cube, "Rotation");

pos3d.y += 4;

ccbSetSceneNodeProperty(cube, "Rotation", pos3d);
curRot=ccbGetSceneNodeProperty(cube,"Rotation");
playerDir=curRot;


moveX=(Math.sin(playerDir*Math.PI/180))*0.6;
moveZ=(Math.cos(playerDir*Math.PI/180))*0.6;
vecMove=new vector3d(moveX,curRot.y,moveZ);


}


Maybe I didn't write the above code correctly.


3dblendsphinx
Registered User
Quote
2017-07-15 17:58:45

Why do you have this line?
pos3d.y += 4;

But I forgot the other part. I test to see what direction the character is going if they are going forward or backward. if you want to go by keyCode you will for example going to have to replace my this.MoveFoward with keyCode==87 code 87 is for the 'w' key.

To get keycodes you can use this site http://keycode.info/


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

}
if(this.MoveBack)
{
//variables for player controlling
newPos=curPos.substract(vecMove);
ccbSetSceneNodeProperty(this.AttachMode,"Position",newPos);

}



en929
Registered User
Quote
2017-07-15 20:44:11

wrote:
Why do you have this line?
pos3d.y += 4;

But I forgot the other part. I test to see what direction the character is going if they are going forward or backward. if you want to go by keyCode you will for example going to have to replace my this.MoveFoward with keyCode==87 code 87 is for the 'w' key.

To get keycodes you can use this site http://keycode.info/


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

}
if(this.MoveBack)
{
//variables for player controlling
newPos=curPos.substract(vecMove);
ccbSetSceneNodeProperty(this.AttachMode,"Position",newPos);

}




Gosh, I probably got it all messed up now. It seems like it was trying to rotate when I pressed the A, but it was doing so in the up and down position. I was trying to do a spin effect using the procedure such as:

1. When a key is pressed do something
2. I selected the A button
3. I pasted in this code below using the Execute JavaScript function. (maybe I got it messed up now as I'm not the best coder,but here it is):



ccbRegisterKeyDownEvent("keyPressedDown");

function keyPressedDown(keyCode)
{
var cube = ccbGetSceneNodeFromName("Henry");
curPos=ccbGetSceneNodeProperty(cube,"Position");
curRot=ccbGetSceneNodeProperty(cube,"Rotation");


playerDir=curRot.z;


moveX=(Math.sin(playerDir*Math.PI/180))*0.6;
moveZ=(Math.cos(playerDir*Math.PI/180))*0.6;
vecMove=new vector3d(moveX,curPos.y,moveZ);



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


ccbSetSceneNodeProperty(cube, "Position", newPos);




}



I did it this way because I kept getting messages saying that something wasn't defined.


3dblendsphinx
Registered User
Quote
2017-07-15 22:14:16

Give me your full code and once I get back to my computer I will integrate into my code.


en929
Registered User
Quote
2017-07-16 00:15:01

wrote:
Give me your full code and once I get back to my computer I will integrate into my code.


Above was the full code that I used. There wasn't much coding involved. Thus, for reference, here's what I did:

1. I clicked on the node that I wanted to rotate. I called it "Henry"
2. I went to behaviour ----> When a key is pressed do something (I specifically chose the "A" button to rotate my character).
3. I found the "Action" bar and clicked on the box with the 3 dots.
4. I clicked on the +
5. I clicked on Special ---------> Execute JavaScript
6. I clicked on the box with the 3 dots that appears
7. I pasted the short code snippet into the box. Here it is again. It's very short:



ccbRegisterKeyDownEvent("keyPressedDown");

function keyPressedDown(keyCode)
{
var cube = ccbGetSceneNodeFromName("Henry");
var pos3d = ccbGetSceneNodeProperty(cube, "Rotation");

pos3d.y += 4;

ccbSetSceneNodeProperty(cube, "Rotation", pos3d);
curRot=ccbGetSceneNodeProperty(cube,"Rotation");
playerDir=curRot;


moveX=(Math.sin(playerDir*Math.PI/180))*0.6;
moveZ=(Math.cos(playerDir*Math.PI/180))*0.6;
vecMove=new vector3d(moveX,curRot.y,moveZ);


}






8. Finally after pasting that, I ran the game and got the controls and rotations, but my other movement controls became skewed when I rotated my character using this code as was mentioned above..


3dblendsphinx
Registered User
Quote
2017-07-16 08:51:13

Sorry for the late response the day was family time. But anyway the first thing I notice an error is -

playerDir=curRot; <-this will mess up your math processing the PlayerDir is using the Y rotation to find out what angle degree the player is facing.

if was suppose to be playerDir=curRot.y;

But here is a ccb file to look at and play around with.

https://app.box.com/s/3fqdchs1kt...


3dblendsphinx
Registered User
Quote
2017-07-16 19:03:15

Here is a better one. Being that the way you had it initially setup on key pressed you would have to keep pressing the keydown to activate. So did it a better way.

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


en929
Registered User
Quote
2017-07-16 20:27:40

wrote:
Here is a better one. Being that the way you had it initially setup on key pressed you would have to keep pressing the keydown to activate. So did it a better way.

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






Hey 3dblendsphinx, it worked! It's working exactly the way that I wanted it to. Thanks a bunch. I adapted the file to my character. This is huge! Thanks again!


3dblendsphinx
Registered User
Quote
2017-07-16 22:16:40

Hey, I fixed the second link cause the file wasn't updated.


en929
Registered User
Quote
2017-07-18 16:26:27

wrote:
Hey, I fixed the second link cause the file wasn't updated.


Oh ok, thanks for that one too. I see that this second version has become more node based and that's great. Thanks for the help!


Create reply:


Posted by: (you are not logged in)


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