Jump to content

[solved] Wrong direction after rotation


George3D
 Share

Recommended Posts

I have this Code to move a player (3rd person) forward and backward, with the arrow keys left-right I can rotate the Player:

if (key == 37)  // left
    android.rotation.y -= Math.PI/2;
if (key == 38)  // foreward
    android.position.z += 0.5;
if (key == 39)  // right
    android.rotation.y += Math.PI/2;
if (key == 40)  // backward
    android.position.z -= 0.5;

But after a rotation the facing direction is wrong. How can I correct this?

Link to comment
Share on other sites

19 hours ago, George3D said:

But after a rotation the facing direction is wrong. How can I correct this?

This is because position is set using the WORLD axes. So even though your android will have turned from North (z) to East (x) position.z will still move the android north. You need to move the android using its LOCAL axes. This can be done using translate rather than position, however be warned that following a rotation the LOCAL axes will only rotate after the world matrix for the android has been computed.

In the following PG   http://www.babylonjs-playground.com/#1ZMJQV#38   see the difference when line 40 is commented out or not.

if (key == 37) { // left 
    android.rotation.y -= Math.PI/2;
    android.computeWorldMatrix(true);
} 
if (key == 38) // forward 
    android.translate(BABYLON.Axis.Z, 0.5, BABYLON.Space.LOCAL);; 
if (key == 39) { // right 
    android.rotation.y += Math.PI/2; 
    android.computeWorldMatrix(true);
}
if (key == 40) // backward 
    android.translate(BABYLON.Axis.Z, -0.5, BABYLON.Space.LOCAL);

A suggestion for future questions - even if your project is too complex for a playground and (as in this case) you have identified a small problem then use a playground to reproduce the problem. For example just use a cuboid for the android and put in the code to move it with the keys. You are then much more likely to get a faster response as people can see what is happening and check out that their suggestions actually work for you.

Good luck in your project.

More on rotations (in spite of page address) in the guide http://babylonjsguide.github.io/basics/Position and also follow further reading

 

Link to comment
Share on other sites

@JohnK: Thank you for the detailed information. I tried it and it works fine. But meanwhile I found another solution with sin and cos:
var xDiff = 0.5 * Math.sin(android.rotation.y);
var zDiff = 0.5 * Math.cos(android.rotation.y); 
if (key == 37)  // left around
   android.rotation.y -= Math.PI/4;
if (key == 38)  {  // forward
   android.position.x += xDiff;
   android.position.z += zDiff;
}
if (key == 39)  // right around
   android.rotation.y += Math.PI/4;
if (key == 40)  {  // backward
   android.position.x -= xDiff;
   android.position.z -= zDiff;
}

@adam: My model (= android) is facing in the right direction. That was not my problem but the rotation.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...