Jump to content

rotation Freecamera on pivot


Dad72
 Share

Recommended Posts

Hi,

I would like to be able to rotate a FreeCamera around a model select on my scene.
I do not want to use ArcRotateCamera. I want to keep the camera position after rotation. The action of pivot is when I click on 'Alt' and when i released 'Alt' the freecamera returns to the normal while retaining its position.

 

Here's the start of what I have written to manage the 'Alt' key on the keyboard.

function onKeyDown(event) {    switch (event.keyCode) {               case 18:            // Pivoter autour d'un objet sélectionner                    break;    }    return false;}function onKeyUp(event) {    switch (event.keyCode) {               case 18:            // revenir a la normale                    break;    }    return false;}

Can you help me. Thank you in advance.

Link to comment
Share on other sites

I suggest you to use a cache and to temporarely parent the camera to your model, to set camera.position, and camera.rotation to BABYLON.Vector3.Zero() (camera.scaling to (1,1,1)) and use a pivot matrix to position, rotate and scale your camera. Thus, the pivot of your camera is the center of your model. Once you release 'Alt', you unparent the camera and use the cache to reset the camera transform.

 

Just a thought. Let me know if it works.

 

Something to help you: http://www.html5gamedevs.com/topic/3083-spaces-world-parent-pivot-local/?p=19938

Link to comment
Share on other sites

http://jsfiddle.net/gwenaelhagenmuller/FkxJ3/

 

Is it what you want?

 

Click (several times if you want) on the first button to rotate the camera on itself, then click on the second button to simulate your 'Alt is pressed' and click again on the first button (several times if you want). You should see the difference :) Finally click on the last button to simulate your 'Alt is released'.

Link to comment
Share on other sites

No, it is not that. Your example, rotates the object axis Y and not the camera. and your example uses a ArcRotateCamera. Me i would like to use a FreeCamera that this moves freely when alt is not press and which rotates around a selected object when one presses on alt.

 

Thank you for your help

Link to comment
Share on other sites

Sorry I was not enough clear. I used an ArcRotateCamera to see what happens with 'customCamera' (this mesh is to simulate your camera. I can display it whereas you can't display a camera; so in my example if I use a camera you won't understand if the model is rotating or if the camera is rotating). You can notice that 'customCamera' is rotating around 'model' (along Z, not Y like you wrote it)

 

Replace 'customCamera' by your camera and set it as the activeCamera and you'll get what you want. My jsfiddle is only the idea how to get what you want. Good luck for the rest :)

Link to comment
Share on other sites

Your script does not work properly. To me, the object rotates on the axis Y and not Z. In addition I have several error. Context is unefined (i put context2D replace). I,'ve adding my camera, I get mesh._localScaling is undefined, customcamera. Scaling is undefined, customcamera. SetPivotMatrix is not a function ...

 

I do not understand context2D also. I am sorry if I seem negative, but I must say that your 300 lines of code gives me evil has the heads for just a camera rotation.

Have you a simpler example with less than code?

 

[edite] I had not seen your 2 others previous answer. I am going to look.

 

Thanks

Link to comment
Share on other sites

http://jsfiddle.net/gwenaelhagenmuller/FkxJ3/13/

 

It's without the debugging code.

 

I still have to use a dumb parent for customCamera. It would be easier if there were Transform nodes such suggested here http://www.html5gamedevs.com/topic/2796-discussion-about-nodes-meshes-instances/?p=18165. The dumb parent here simulates this kind of Transform node.

Link to comment
Share on other sites

Yes I would like to turn around the object as the fact arcRotateCamera but with one FreeCamera. But can be that it would be simpler to change of camera when I click on Alt and to come back has FreeCamera when I unloose Alt. But I am not on to know how to keep the positions xyz between every change of camera.

Link to comment
Share on other sites

I have done that which operates almost correctly, but the positions are not yet accurate. That is what i done wrong?

function onKeyDown(event) {    switch (event.keyCode) {               case 18: // ALT            // Pivoter autour d'un objet avec la ArcRotateCamera            if(_mesh && objetSelected && CameraChanged == false) {                                scene.activeCamera.detachControl(canvas);                scene.activeCamera = cameraFree[1];                scene.activeCamera.attachControl(canvas);                cameraFree[1].attachControl(canvas);                                    cameraFree[1].target.x = _mesh.position.x;                cameraFree[1].target.y = _mesh.position.y + cameraFree[0].position.y - 3;                cameraFree[1].target.z = _mesh.position.z - 3;                cameraFree[1].radius = cameraFree[0].position.z - _mesh.position.z;                                    CameraChanged = true;            }        break;    }    return false;}function onKeyUp(event) {    switch (event.keyCode) {                case 18: // ALT            // revenir a la normale avec la freeCamera                        if(_mesh && objetSelected == null && CameraChanged) {                scene.activeCamera.detachControl(canvas);                scene.activeCamera = cameraFree[0];                scene.activeCamera.attachControl(canvas);                cameraFree[0].attachControl(canvas);                                cameraFree[0].position.x = cameraFree[1].position.x;                cameraFree[0].position.y = cameraFree[1].position.y;                cameraFree[0].position.z = cameraFree[1].position.z;                cameraFree[0].setTarget(_mesh.position);                CameraChanged = false;            }                                  break;    }    return false;}
Link to comment
Share on other sites

I finally find a solution for the radius, and I adjust the rest of the errors:

 

I have put a radius of 20 by default when you click on ALT and with the mouse wheel you can zoom unzoom by increasing or decreasing the radius. And I also add 5 by report has the position of the selected object to be at below.

It works correctly for me. Here's the full code:

function zoomIntOut(event){        if(cameraFree[1]) {        if(event.delta > 0) {            cameraFree[1].radius += 2;        }        else if(event.delta < 0) {                 cameraFree[1].radius -= 2;        }            }}function onKeyDown(event) {    switch (event.keyCode) {               case 18: // ALT            // Pivoter autour d'un objet avec la freeCamera                        if(_mesh && objetSelected && CameraChanged == false) {                               scene.activeCamera.detachControl(canvas); // Detach FreeCamera                scene.activeCamera = cameraFree[1]; // active Camera ArcRotate                scene.activeCamera.attachControl(canvas); // attache camera ArcRotate                                                    cameraFree[1].target.x = _mesh.position.x;                cameraFree[1].target.y = _mesh.position.y + 5;                cameraFree[1].target.z = _mesh.position.z;                cameraFree[1].radius = 20;                                    CameraChanged = true;            }                    break;    }    return false;}function onKeyUp(event) {    switch (event.keyCode) {                case 18: // ALT            // revenir a la normale avec la freeCamera            if(_mesh && objetSelected == null && CameraChanged) {                scene.activeCamera.detachControl(canvas); // detach ArcRotateCamera                scene.activeCamera = cameraFree[0]; // Active FreeCamera                scene.activeCamera.attachControl(canvas); // Attache FreeCamera                                                 cameraFree[0].position.x = cameraFree[1].position.x;                cameraFree[0].position.y = cameraFree[1].position.y;                cameraFree[0].position.z = cameraFree[1].position.z;                cameraFree[0].setTarget(_mesh.position);                CameraChanged = false;            }        break;    }    return false;}$(document).ready(function(){        $(this).wheel(function(e) {        e.preventDefault();                    zoomIntOut(e);    });});
Link to comment
Share on other sites

I guess it's what you wanted but for me you shouldn't hardcode 5 and 20. Where are they come from? They should be computed from the boundingInfo for example.

 

Be careful, you attachControl twice on the same camera:

scene.activeCamera.attachControl(canvas); // attache camera ArcRotatecameraFree[1].attachControl(canvas);

since scene.activeCamera equals cameraFree[1]

 

Same thing here:

scene.activeCamera.attachControl(canvas); // Attache FreeCameracameraFree[0].attachControl(canvas);

Pay attention also that it won't work if your cameras don't have the same parent or if your model has a parent.

Link to comment
Share on other sites

You shouldn't hardcode them. They are empirical values for your tests. They must be computed. That's something discussed here : http://www.html5gamedevs.com/topic/2912-invalid-mesh-pick/

 

(Tu ne devrais pas les hardcoder. Ce sont des valeurs empiriques pour tes tests. Ces valeurs doivent provenir d'un calcul. Ca aussi été discuté ici : http://www.html5gamedevs.com/topic/2912-invalid-mesh-pick/)

Link to comment
Share on other sites

You may want to say that according to the size of the model, the default value of 50 may be wrong. I might get into the object and not on the outside.
What would you suggest as calculate to have always 50 of radius of the object about his dimensions?
 

En plus claire.

(Vous voulez peut être dire que suivant la taille du modèle, la valeur par défaut de 50 peut être fausse. je pourrais me retrouver dans l'objet et non à l’extérieur.(je vient d'y pensez garce a vous) Que me suggéreriez vous comme calcule de façons à toujours être à 50 de rayon de l'objet quelque soit ses dimensions?)

Link to comment
Share on other sites

That will be maths. You need to frame the object. For this, you need to determine the radius of the bounding sphere for example and use this value to compute a ratio (value / radius of the bounding sphere of your test mesh) that you multiply by 50. Thus, it will be proportional.

Link to comment
Share on other sites

J'ai fait ça :

var modele = _mesh.getBoundingInfo();var valeurSphereRadius = modele.boundingSphere.radius;cameraFree[1].radius =  valeurSphereRadius + 20;

Je récupère le rayon de la sphère englobante  et je lui ajoute 20 pour avoir le radius entre la camera et l'objet sélectionner.

 

J'ai aussi fait avec une boite en récupérant la valeur la plus grande entre x et z et j'ajoute les 20 a la plus grande valeur. (ça me parait mieux avec une boite)

var modele = _mesh.getBoundingInfo();var valeurBoiteMax;if(modele.boundingBox.maximum.x > modele.boundingSphere.maximum.z) valeurBoiteMax = modele.boundingBox.maximum.x;else if(modele.boundingSphere.maximum.z > modele.boundingBox.maximum.x) valeurBoiteMax = modele.boundingBox.maximum.z;   cameraFree[1].radius =  valeurBoiteMax + 20;

Cela vous parait bien ou il y a une autre solution.

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