Jump to content

lookAt doesn't change rotation param


getzel
 Share

Recommended Posts

Edit : problem solved, look below ;)

 

______

 

lookAt works well on the view.

But the parameter rotation doesn't change staying at 0,0,0.

And I need it after to calculate the vector forward in local space of a mesh to moveWithCollision :

var forwards = new BABYLON.Vector3(parseFloat(Math.sin(character.rotation.y)) / speedCharacter, gravity, parseFloat(Math.cos(character.rotation.y)) / speedCharacter);
forwards.negate();
character.moveWithCollisions(forwards);

 

In the playground, I don't know how to display text to debug and Jsfiddle doesn't work so I paste my code here :

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style>


#renderCanvas {
    width: 100%;
    height: 95%;
    min-height: 95%;
}

</style>

</head>

<body>

<div id="info">
aaa
</div>

<canvas id="renderCanvas"></canvas>

<script src="http://cdn.babylonjs.com/2-4/babylon.js"></script>
<script>

var info = document.getElementById('info');


 var canvas = document.getElementById("renderCanvas");
 var engine = new BABYLON.Engine(canvas, true);
 var scene = new BABYLON.Scene(engine);
 var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 10, BABYLON.Vector3.Zero(), scene);
camera.attachControl(canvas, false);
 var light = new BABYLON.PointLight("Omni0", new BABYLON.Vector3(0, 100, 100), scene);
  var cube = BABYLON.Mesh.CreateBox("Box", 1, scene);
  var cube2 = BABYLON.Mesh.CreateBox("Box2", 1, scene);
   cube.position.x = -5;
 
 var material = new BABYLON.StandardMaterial("default", scene);
 material.emissiveColor = new BABYLON.Color3(0.3, 0.3, 0.5);
 cube.material = material;
    
 var material2 = new BABYLON.StandardMaterial("default2", scene);
 material2.emissiveColor = new BABYLON.Color3(0.5, 0.3, 0.3);
 cube2.material = material2;


scene.beforeRender = function() {

      
   };
 
    
    // Render loop
    var renderLoop = function () {
        
	cube.lookAt(cube2.position);
     cube2.position.z += 0.01;
	info.innerHTML = cube.rotation;    //                               !!!       display 0,0,0 everytime      !!!
		
	scene.render();
		
    };
    
    // Launch render loop
    engine.runRenderLoop(renderLoop);

</script>

</body>

</html>

 

Thank you

Link to comment
Share on other sites

There is a topic about that :

 

lookAt changes the rotation to quaternion mode.

I try the code and then change my mesh.rotation.x = mesh.rotation.z = 0;

He looks to the good direction but only 180°.

 

This function seems to work to retrieve rotation euler :

mesh.rotationQuaternion.toEulerAngles();

 

Link to comment
Share on other sites

Finally I found a solution without quaternion but trigonometry.

This code is to rotate a character on y axis :



 

function mousemovef(){

   var pickResult = scene.pick(scene.pointerX, scene.pointerY);

    if (pickResult.hit) {

  
                
             var   pickResultPos.x = pickResult.pickedPoint.x;
              var  pickResultPos.z = pickResult.pickedPoint.z;
                
                var diffX = pickResultPos.x - man.position.x;
                var diffZ = pickResultPos.z - man.position.z;
                man.rotation.y = Math.atan2(-diffX,-diffZ);
                
       
    }// if result
    
 
    
}//mousemovef()


 window.addEventListener("mousemove", function() {

    mousemovef();
 
});

 

 

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...