Forum

Rotate object by javascript

23 September 2016 01:15
I'm trying to rotate and object by pushing a key in the keyboard and it rotates (in an ugly form really as it doesn't interpolate the rotation) but my problem is when I release the key the object gets back to the original position.
The code is:
m_trans.get_rotation(obj, _vec4_tmp);
m_util.quat_to_euler(_vec4_tmp, _vec3_tmp);
_vec3_tmp[1] += 0.1;
m_util.euler_to_quat(_vec3_tmp, _vec4_tmp);
m_trans.set_rotation_v(obj, _vec4_tmp);

Can anyone help me with that?
26 September 2016 13:08
Hmm, the code is correct. This issue is probably related to the object itself. Does it have a parent or a constraint or something like this? Did you try to rotate a simple cube instead (it should be dynamic of course)?
26 September 2016 20:18
Hi, Ivan, thank you for your attention.
The project is a bit complex because I'm trying to put the camera inside a space ship, and control it from the keyboard. I attach the file if you want to take a look. It is a little messed up because I'm making changes to fix it. It is a FPS, controls are wsad and enter to enter the ship. Inside the ship I'm trying to make it rotate with a and d keys. If you start up the engine with Mays+I you can accelerate with Mays+Q and decelerate with Mays+E and apply power left and right with Q and E. Maybe I'm going a bit too far to my knowledge, hehe… I think in the engine there is some problem with the key sensors, you have to press the key twice to make it work…
26 September 2016 20:26
Sorry, the first file don't have the click method activated. This is the good one….
26 September 2016 22:16
Sorry, I had a problem with the file. Trying to fix it….
26 September 2016 22:27
Well, I think I got it at last, this is the file: parsec_project.zip
28 September 2016 11:44
but my problem is when I release the key the object gets back to the original position
I found the reason of this strange behavior. This happens because you have the "Dynamic" physics type on the object ScoutCollision.
For this physics type you should use special methods from the physics.js module, for example: set_transform.
Alternatively, you can set the type to "Static" and use those methods that you use now in your app.

Unfortunately we don't have a good explanation in our doc about the specifics and use cases of the different physics types. However this page can be helpful (if you didn't see it yet): b4w physics.

I suppose that you want the ScoutCollision to be a real physical object with collisions, rebounds, etc…. In this case I'd recommend to use the "Rigid Body" physics type along with the methods like apply_force and apply_torque to setup the ship's movement.
28 September 2016 13:24
Aaaah, all right, I'll try those hints. I thought it had something to do with the environment of the variables, so I was lost, this opens other way, thank you.
 
Please register or log in to leave a reply.