How to programmatically undo positional translation to pivot point?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I think this is ultimately a pretty simple question, but it's hard to describe, thus, I provide a working example here (in the sample press 'z' to see rotation with unwanted translation and 'x' keys to rotate with a compensating re-position).



Basically, I am trying to rotate an object (a thumbstick) about the z-axis of a complex model loaded via gltf (a model of the oculus rift touch controller). It's easy to rotate about the x-axis because it's 90 deg. orthogonal to the x-axis. About the z-axis, it's harder because the plane the thumbstick is attached to is angled at 30 deg. I realize that if the thumbstick were using local coordinates, this wouldn't be a problem, but 'thumb.rotation.z' does not seem to be using local coordinates and is rotating about the model's (as a whole), or maybe even the scene's global y and z (?). Anyway, after a bunch of futzing around, I was able to get things to work by doing the following:



// occulus plane is angle at 30 deg, which corresponds to
// 5 units forward to 3 units down.
var axis = new THREE.Vector3(0, 5, -3).normalize();
factory.thumbstick.geometry.center();
var dir = (evt.key === 'x' ? 1 : -1);
thumb.rotateOnAxis(axis, factory.ONE_DEG * 5.0 * dir);


Basically, I'm rotating about a "tilted" axis, and then calling 'center' to make thumbstick centered on the pivot point, so it rotates about the pivot point, rather than around the pivot point (like the earth orbiting the sun).



Only problem is that when you call 'geometry.center()' and then call 'rotateOnAxis', it translates the thumbstick to the pivot point:



enter image description here



Note: the position on the thumbstick object is (0,0,0) before and after the calls.



I have empirically determined that if I alter the position of the thumbstick after the translation like so:



   // magic numbers compensating position
var zDisp = 0.0475;
var yDisp = zDisp / 6.0
thumb.position.x = 0.001;
thumb.position.y = -yDisp;
thumb.position.z = zDisp;


Then it (almost) returns back to it's original position:



enter image description here



Problem is these numbers were just determined by interactively and repeatedly trying to re-position the thumbstick i.e. empirically. I simply cannot find a programmatic, analytical, api kind of way to restore the original position. Note: saving the original position doesn't work, because it's zero before and after the translation. Some of the things I tried were taking the difference between the bounding spheres of the global object and the thumbstick object, trying to come up with some 'sin x- cos x' relation on one distance etc. but nothing works.



My question is, how can I progammatically reverse the offset due to calling 'geometry.center()' and rotateOnAxis (which translates to the pivot point), without having to resort to hacked, empircal "magic" numbers, that could conceivably change if the gltf model changes.



Of course, if someone can also come up with a better way to achieve this rotation, that would be great too.



What's throwing me is the (peceived?) complexity of the gltf model itself. It's confusing because I have a hard time interpreting it and it's various parts: I'm really not sure where the "center" is, and in certain cases, it appears with the 'THREE.AxesHelper' I'm attaching that what it shows as 'y' is actually 'z' and sometimes 'up' is really 'down' etc, and it gets confusing fast.



Any help would be appreciated.










share|improve this question

























  • I also think what you want to do is probably very simple. I just dont quite understand what it is you want to achieve. But if I understand one thing correctly, it is that you want the thumb to rotate similar to how it does when pressing "X" in your example? How is the thumb added to the scene? I think this would be easier to achieve if it is added as a child to the touch controller, and not the scene. Check this example of it makes any sense at all: codepen.io/micnil/pen/VqXeVR

    – micnil
    Jan 4 at 16:39













  • Yes, pressing "x" is the solution. It's not that I don't have something that doesn't work -- I could probably push this code right now (this is part of a-frame issue github.com/aframevr/aframe/issues/3928). I'm just trying to improve the implementation e.g not having to resort to a "hack" to re-position it. The thumb is part of a gltf created by oculus (facebook) -- it's not something I created. I don't really have any control over how oculus defines their gltf model -- I think it's a scene with various objects attached.

    – vt5491
    Jan 5 at 3:47











  • In other words, when you press "z" and the thumbstick gets displaced, how can I calculate what that displacement is at runtime (so I can subsequently subtract it) instead of using hard-coded, empirical constants that only approx. restore the original position. Calling THREE.center() moves the thumbstick, I want something like 'uncenter()' to move it back to it's original position. p.s the gltf model is a THREE.Group, not a scene.

    – vt5491
    Jan 5 at 7:36








  • 1





    It's looking like the answer from WestLangley on this thread stackoverflow.com/questions/28848863/… is giving me what I need. If it pans out, I will post an official answer.

    – vt5491
    Jan 5 at 10:58


















0















I think this is ultimately a pretty simple question, but it's hard to describe, thus, I provide a working example here (in the sample press 'z' to see rotation with unwanted translation and 'x' keys to rotate with a compensating re-position).



Basically, I am trying to rotate an object (a thumbstick) about the z-axis of a complex model loaded via gltf (a model of the oculus rift touch controller). It's easy to rotate about the x-axis because it's 90 deg. orthogonal to the x-axis. About the z-axis, it's harder because the plane the thumbstick is attached to is angled at 30 deg. I realize that if the thumbstick were using local coordinates, this wouldn't be a problem, but 'thumb.rotation.z' does not seem to be using local coordinates and is rotating about the model's (as a whole), or maybe even the scene's global y and z (?). Anyway, after a bunch of futzing around, I was able to get things to work by doing the following:



// occulus plane is angle at 30 deg, which corresponds to
// 5 units forward to 3 units down.
var axis = new THREE.Vector3(0, 5, -3).normalize();
factory.thumbstick.geometry.center();
var dir = (evt.key === 'x' ? 1 : -1);
thumb.rotateOnAxis(axis, factory.ONE_DEG * 5.0 * dir);


Basically, I'm rotating about a "tilted" axis, and then calling 'center' to make thumbstick centered on the pivot point, so it rotates about the pivot point, rather than around the pivot point (like the earth orbiting the sun).



Only problem is that when you call 'geometry.center()' and then call 'rotateOnAxis', it translates the thumbstick to the pivot point:



enter image description here



Note: the position on the thumbstick object is (0,0,0) before and after the calls.



I have empirically determined that if I alter the position of the thumbstick after the translation like so:



   // magic numbers compensating position
var zDisp = 0.0475;
var yDisp = zDisp / 6.0
thumb.position.x = 0.001;
thumb.position.y = -yDisp;
thumb.position.z = zDisp;


Then it (almost) returns back to it's original position:



enter image description here



Problem is these numbers were just determined by interactively and repeatedly trying to re-position the thumbstick i.e. empirically. I simply cannot find a programmatic, analytical, api kind of way to restore the original position. Note: saving the original position doesn't work, because it's zero before and after the translation. Some of the things I tried were taking the difference between the bounding spheres of the global object and the thumbstick object, trying to come up with some 'sin x- cos x' relation on one distance etc. but nothing works.



My question is, how can I progammatically reverse the offset due to calling 'geometry.center()' and rotateOnAxis (which translates to the pivot point), without having to resort to hacked, empircal "magic" numbers, that could conceivably change if the gltf model changes.



Of course, if someone can also come up with a better way to achieve this rotation, that would be great too.



What's throwing me is the (peceived?) complexity of the gltf model itself. It's confusing because I have a hard time interpreting it and it's various parts: I'm really not sure where the "center" is, and in certain cases, it appears with the 'THREE.AxesHelper' I'm attaching that what it shows as 'y' is actually 'z' and sometimes 'up' is really 'down' etc, and it gets confusing fast.



Any help would be appreciated.










share|improve this question

























  • I also think what you want to do is probably very simple. I just dont quite understand what it is you want to achieve. But if I understand one thing correctly, it is that you want the thumb to rotate similar to how it does when pressing "X" in your example? How is the thumb added to the scene? I think this would be easier to achieve if it is added as a child to the touch controller, and not the scene. Check this example of it makes any sense at all: codepen.io/micnil/pen/VqXeVR

    – micnil
    Jan 4 at 16:39













  • Yes, pressing "x" is the solution. It's not that I don't have something that doesn't work -- I could probably push this code right now (this is part of a-frame issue github.com/aframevr/aframe/issues/3928). I'm just trying to improve the implementation e.g not having to resort to a "hack" to re-position it. The thumb is part of a gltf created by oculus (facebook) -- it's not something I created. I don't really have any control over how oculus defines their gltf model -- I think it's a scene with various objects attached.

    – vt5491
    Jan 5 at 3:47











  • In other words, when you press "z" and the thumbstick gets displaced, how can I calculate what that displacement is at runtime (so I can subsequently subtract it) instead of using hard-coded, empirical constants that only approx. restore the original position. Calling THREE.center() moves the thumbstick, I want something like 'uncenter()' to move it back to it's original position. p.s the gltf model is a THREE.Group, not a scene.

    – vt5491
    Jan 5 at 7:36








  • 1





    It's looking like the answer from WestLangley on this thread stackoverflow.com/questions/28848863/… is giving me what I need. If it pans out, I will post an official answer.

    – vt5491
    Jan 5 at 10:58














0












0








0


1






I think this is ultimately a pretty simple question, but it's hard to describe, thus, I provide a working example here (in the sample press 'z' to see rotation with unwanted translation and 'x' keys to rotate with a compensating re-position).



Basically, I am trying to rotate an object (a thumbstick) about the z-axis of a complex model loaded via gltf (a model of the oculus rift touch controller). It's easy to rotate about the x-axis because it's 90 deg. orthogonal to the x-axis. About the z-axis, it's harder because the plane the thumbstick is attached to is angled at 30 deg. I realize that if the thumbstick were using local coordinates, this wouldn't be a problem, but 'thumb.rotation.z' does not seem to be using local coordinates and is rotating about the model's (as a whole), or maybe even the scene's global y and z (?). Anyway, after a bunch of futzing around, I was able to get things to work by doing the following:



// occulus plane is angle at 30 deg, which corresponds to
// 5 units forward to 3 units down.
var axis = new THREE.Vector3(0, 5, -3).normalize();
factory.thumbstick.geometry.center();
var dir = (evt.key === 'x' ? 1 : -1);
thumb.rotateOnAxis(axis, factory.ONE_DEG * 5.0 * dir);


Basically, I'm rotating about a "tilted" axis, and then calling 'center' to make thumbstick centered on the pivot point, so it rotates about the pivot point, rather than around the pivot point (like the earth orbiting the sun).



Only problem is that when you call 'geometry.center()' and then call 'rotateOnAxis', it translates the thumbstick to the pivot point:



enter image description here



Note: the position on the thumbstick object is (0,0,0) before and after the calls.



I have empirically determined that if I alter the position of the thumbstick after the translation like so:



   // magic numbers compensating position
var zDisp = 0.0475;
var yDisp = zDisp / 6.0
thumb.position.x = 0.001;
thumb.position.y = -yDisp;
thumb.position.z = zDisp;


Then it (almost) returns back to it's original position:



enter image description here



Problem is these numbers were just determined by interactively and repeatedly trying to re-position the thumbstick i.e. empirically. I simply cannot find a programmatic, analytical, api kind of way to restore the original position. Note: saving the original position doesn't work, because it's zero before and after the translation. Some of the things I tried were taking the difference between the bounding spheres of the global object and the thumbstick object, trying to come up with some 'sin x- cos x' relation on one distance etc. but nothing works.



My question is, how can I progammatically reverse the offset due to calling 'geometry.center()' and rotateOnAxis (which translates to the pivot point), without having to resort to hacked, empircal "magic" numbers, that could conceivably change if the gltf model changes.



Of course, if someone can also come up with a better way to achieve this rotation, that would be great too.



What's throwing me is the (peceived?) complexity of the gltf model itself. It's confusing because I have a hard time interpreting it and it's various parts: I'm really not sure where the "center" is, and in certain cases, it appears with the 'THREE.AxesHelper' I'm attaching that what it shows as 'y' is actually 'z' and sometimes 'up' is really 'down' etc, and it gets confusing fast.



Any help would be appreciated.










share|improve this question
















I think this is ultimately a pretty simple question, but it's hard to describe, thus, I provide a working example here (in the sample press 'z' to see rotation with unwanted translation and 'x' keys to rotate with a compensating re-position).



Basically, I am trying to rotate an object (a thumbstick) about the z-axis of a complex model loaded via gltf (a model of the oculus rift touch controller). It's easy to rotate about the x-axis because it's 90 deg. orthogonal to the x-axis. About the z-axis, it's harder because the plane the thumbstick is attached to is angled at 30 deg. I realize that if the thumbstick were using local coordinates, this wouldn't be a problem, but 'thumb.rotation.z' does not seem to be using local coordinates and is rotating about the model's (as a whole), or maybe even the scene's global y and z (?). Anyway, after a bunch of futzing around, I was able to get things to work by doing the following:



// occulus plane is angle at 30 deg, which corresponds to
// 5 units forward to 3 units down.
var axis = new THREE.Vector3(0, 5, -3).normalize();
factory.thumbstick.geometry.center();
var dir = (evt.key === 'x' ? 1 : -1);
thumb.rotateOnAxis(axis, factory.ONE_DEG * 5.0 * dir);


Basically, I'm rotating about a "tilted" axis, and then calling 'center' to make thumbstick centered on the pivot point, so it rotates about the pivot point, rather than around the pivot point (like the earth orbiting the sun).



Only problem is that when you call 'geometry.center()' and then call 'rotateOnAxis', it translates the thumbstick to the pivot point:



enter image description here



Note: the position on the thumbstick object is (0,0,0) before and after the calls.



I have empirically determined that if I alter the position of the thumbstick after the translation like so:



   // magic numbers compensating position
var zDisp = 0.0475;
var yDisp = zDisp / 6.0
thumb.position.x = 0.001;
thumb.position.y = -yDisp;
thumb.position.z = zDisp;


Then it (almost) returns back to it's original position:



enter image description here



Problem is these numbers were just determined by interactively and repeatedly trying to re-position the thumbstick i.e. empirically. I simply cannot find a programmatic, analytical, api kind of way to restore the original position. Note: saving the original position doesn't work, because it's zero before and after the translation. Some of the things I tried were taking the difference between the bounding spheres of the global object and the thumbstick object, trying to come up with some 'sin x- cos x' relation on one distance etc. but nothing works.



My question is, how can I progammatically reverse the offset due to calling 'geometry.center()' and rotateOnAxis (which translates to the pivot point), without having to resort to hacked, empircal "magic" numbers, that could conceivably change if the gltf model changes.



Of course, if someone can also come up with a better way to achieve this rotation, that would be great too.



What's throwing me is the (peceived?) complexity of the gltf model itself. It's confusing because I have a hard time interpreting it and it's various parts: I'm really not sure where the "center" is, and in certain cases, it appears with the 'THREE.AxesHelper' I'm attaching that what it shows as 'y' is actually 'z' and sometimes 'up' is really 'down' etc, and it gets confusing fast.



Any help would be appreciated.







three.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 6:04







vt5491

















asked Jan 3 at 5:58









vt5491vt5491

1,11611019




1,11611019













  • I also think what you want to do is probably very simple. I just dont quite understand what it is you want to achieve. But if I understand one thing correctly, it is that you want the thumb to rotate similar to how it does when pressing "X" in your example? How is the thumb added to the scene? I think this would be easier to achieve if it is added as a child to the touch controller, and not the scene. Check this example of it makes any sense at all: codepen.io/micnil/pen/VqXeVR

    – micnil
    Jan 4 at 16:39













  • Yes, pressing "x" is the solution. It's not that I don't have something that doesn't work -- I could probably push this code right now (this is part of a-frame issue github.com/aframevr/aframe/issues/3928). I'm just trying to improve the implementation e.g not having to resort to a "hack" to re-position it. The thumb is part of a gltf created by oculus (facebook) -- it's not something I created. I don't really have any control over how oculus defines their gltf model -- I think it's a scene with various objects attached.

    – vt5491
    Jan 5 at 3:47











  • In other words, when you press "z" and the thumbstick gets displaced, how can I calculate what that displacement is at runtime (so I can subsequently subtract it) instead of using hard-coded, empirical constants that only approx. restore the original position. Calling THREE.center() moves the thumbstick, I want something like 'uncenter()' to move it back to it's original position. p.s the gltf model is a THREE.Group, not a scene.

    – vt5491
    Jan 5 at 7:36








  • 1





    It's looking like the answer from WestLangley on this thread stackoverflow.com/questions/28848863/… is giving me what I need. If it pans out, I will post an official answer.

    – vt5491
    Jan 5 at 10:58



















  • I also think what you want to do is probably very simple. I just dont quite understand what it is you want to achieve. But if I understand one thing correctly, it is that you want the thumb to rotate similar to how it does when pressing "X" in your example? How is the thumb added to the scene? I think this would be easier to achieve if it is added as a child to the touch controller, and not the scene. Check this example of it makes any sense at all: codepen.io/micnil/pen/VqXeVR

    – micnil
    Jan 4 at 16:39













  • Yes, pressing "x" is the solution. It's not that I don't have something that doesn't work -- I could probably push this code right now (this is part of a-frame issue github.com/aframevr/aframe/issues/3928). I'm just trying to improve the implementation e.g not having to resort to a "hack" to re-position it. The thumb is part of a gltf created by oculus (facebook) -- it's not something I created. I don't really have any control over how oculus defines their gltf model -- I think it's a scene with various objects attached.

    – vt5491
    Jan 5 at 3:47











  • In other words, when you press "z" and the thumbstick gets displaced, how can I calculate what that displacement is at runtime (so I can subsequently subtract it) instead of using hard-coded, empirical constants that only approx. restore the original position. Calling THREE.center() moves the thumbstick, I want something like 'uncenter()' to move it back to it's original position. p.s the gltf model is a THREE.Group, not a scene.

    – vt5491
    Jan 5 at 7:36








  • 1





    It's looking like the answer from WestLangley on this thread stackoverflow.com/questions/28848863/… is giving me what I need. If it pans out, I will post an official answer.

    – vt5491
    Jan 5 at 10:58

















I also think what you want to do is probably very simple. I just dont quite understand what it is you want to achieve. But if I understand one thing correctly, it is that you want the thumb to rotate similar to how it does when pressing "X" in your example? How is the thumb added to the scene? I think this would be easier to achieve if it is added as a child to the touch controller, and not the scene. Check this example of it makes any sense at all: codepen.io/micnil/pen/VqXeVR

– micnil
Jan 4 at 16:39







I also think what you want to do is probably very simple. I just dont quite understand what it is you want to achieve. But if I understand one thing correctly, it is that you want the thumb to rotate similar to how it does when pressing "X" in your example? How is the thumb added to the scene? I think this would be easier to achieve if it is added as a child to the touch controller, and not the scene. Check this example of it makes any sense at all: codepen.io/micnil/pen/VqXeVR

– micnil
Jan 4 at 16:39















Yes, pressing "x" is the solution. It's not that I don't have something that doesn't work -- I could probably push this code right now (this is part of a-frame issue github.com/aframevr/aframe/issues/3928). I'm just trying to improve the implementation e.g not having to resort to a "hack" to re-position it. The thumb is part of a gltf created by oculus (facebook) -- it's not something I created. I don't really have any control over how oculus defines their gltf model -- I think it's a scene with various objects attached.

– vt5491
Jan 5 at 3:47





Yes, pressing "x" is the solution. It's not that I don't have something that doesn't work -- I could probably push this code right now (this is part of a-frame issue github.com/aframevr/aframe/issues/3928). I'm just trying to improve the implementation e.g not having to resort to a "hack" to re-position it. The thumb is part of a gltf created by oculus (facebook) -- it's not something I created. I don't really have any control over how oculus defines their gltf model -- I think it's a scene with various objects attached.

– vt5491
Jan 5 at 3:47













In other words, when you press "z" and the thumbstick gets displaced, how can I calculate what that displacement is at runtime (so I can subsequently subtract it) instead of using hard-coded, empirical constants that only approx. restore the original position. Calling THREE.center() moves the thumbstick, I want something like 'uncenter()' to move it back to it's original position. p.s the gltf model is a THREE.Group, not a scene.

– vt5491
Jan 5 at 7:36







In other words, when you press "z" and the thumbstick gets displaced, how can I calculate what that displacement is at runtime (so I can subsequently subtract it) instead of using hard-coded, empirical constants that only approx. restore the original position. Calling THREE.center() moves the thumbstick, I want something like 'uncenter()' to move it back to it's original position. p.s the gltf model is a THREE.Group, not a scene.

– vt5491
Jan 5 at 7:36






1




1





It's looking like the answer from WestLangley on this thread stackoverflow.com/questions/28848863/… is giving me what I need. If it pans out, I will post an official answer.

– vt5491
Jan 5 at 10:58





It's looking like the answer from WestLangley on this thread stackoverflow.com/questions/28848863/… is giving me what I need. If it pans out, I will post an official answer.

– vt5491
Jan 5 at 10:58












1 Answer
1






active

oldest

votes


















0














The breakthrough for me on this was to re-frame the problem as how do I change the pivot point for the thumbstick, rather than how do I move the thumbstick to the (default and pre-existing) pivot point. To paraphrase JFK, "ask not how you can move to the pivot, but ask how the pivot can move to you" :-)



After changing my angle of attack, I pretty quickly found the aforementioned link, which yielded my solution.



I posted an updated glitch here, so now pressing z works as I expected. Here is the relevant code portion:



  factory.onModelLoaded = function(evt) {
console.log(`onModelLoaded: entered`);

factory.thumbstick = this.scene.children[1].children[2]
let thumb = factory.thumbstick;
// make the thumb red so it's easier to see
thumb.material = (new THREE.MeshBasicMaterial({color: 0xFF7777}));

// use method from https://stackoverflow.com/questions/28848863/threejs-how-to-rotate-around-objects-own-center-instead-of-world-center/28860849#28860849
// to translate the pivot point of the thumbstick to the the thumbstick center
factory.thumbParent = thumb.parent;
let thumbParent = factory.thumbParent;
thumbParent.remove(thumb);

var box = new THREE.Box3().setFromObject( thumb );
box.getCenter( thumb.position ); // this basically yields my prev. "magic numbers"
// thumb.position.multiplyScalar( - 1 );
var pivot = new THREE.Group();
thumbParent.add( pivot );
pivot.add( thumb );

thumb.geometry.center();

// add axeshelp after centering, otherwise the axes help, as a child of thumb,
// will increase the bounding box of thumb, and positioning will be wrong.
axesHelper = new THREE.AxesHelper();
thumb.add(axesHelper);
}


Which allows my "z" handler to just rotate without having to do translation:



  case 'z':
case 'Z':
var axis = new THREE.Vector3(0, 5, -3).normalize();
var dir = (evt.key === 'z' ? 1 : -1);
thumb.rotateOnAxis(axis, factory.ONE_DEG * 5.0 * dir);
break;


Interestingly, it's the call to box.getCenter() that generates numbers very close to my "magic numbers":



box.getCenter()
Vector3 {x: 0.001487499801442027, y: -0.007357006114165027, z: 0.04779449797522323}


My empirical guess was {x: 0.001, y: -0.00791666666, z: 0.0475} which is %error {x: 32.7%, y: 7.6%, z: 0.61%}, so I was pretty close esp. on the z component, but still not the "perfect" numbers of box.getCenter().






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017039%2fhow-to-programmatically-undo-positional-translation-to-pivot-point%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The breakthrough for me on this was to re-frame the problem as how do I change the pivot point for the thumbstick, rather than how do I move the thumbstick to the (default and pre-existing) pivot point. To paraphrase JFK, "ask not how you can move to the pivot, but ask how the pivot can move to you" :-)



    After changing my angle of attack, I pretty quickly found the aforementioned link, which yielded my solution.



    I posted an updated glitch here, so now pressing z works as I expected. Here is the relevant code portion:



      factory.onModelLoaded = function(evt) {
    console.log(`onModelLoaded: entered`);

    factory.thumbstick = this.scene.children[1].children[2]
    let thumb = factory.thumbstick;
    // make the thumb red so it's easier to see
    thumb.material = (new THREE.MeshBasicMaterial({color: 0xFF7777}));

    // use method from https://stackoverflow.com/questions/28848863/threejs-how-to-rotate-around-objects-own-center-instead-of-world-center/28860849#28860849
    // to translate the pivot point of the thumbstick to the the thumbstick center
    factory.thumbParent = thumb.parent;
    let thumbParent = factory.thumbParent;
    thumbParent.remove(thumb);

    var box = new THREE.Box3().setFromObject( thumb );
    box.getCenter( thumb.position ); // this basically yields my prev. "magic numbers"
    // thumb.position.multiplyScalar( - 1 );
    var pivot = new THREE.Group();
    thumbParent.add( pivot );
    pivot.add( thumb );

    thumb.geometry.center();

    // add axeshelp after centering, otherwise the axes help, as a child of thumb,
    // will increase the bounding box of thumb, and positioning will be wrong.
    axesHelper = new THREE.AxesHelper();
    thumb.add(axesHelper);
    }


    Which allows my "z" handler to just rotate without having to do translation:



      case 'z':
    case 'Z':
    var axis = new THREE.Vector3(0, 5, -3).normalize();
    var dir = (evt.key === 'z' ? 1 : -1);
    thumb.rotateOnAxis(axis, factory.ONE_DEG * 5.0 * dir);
    break;


    Interestingly, it's the call to box.getCenter() that generates numbers very close to my "magic numbers":



    box.getCenter()
    Vector3 {x: 0.001487499801442027, y: -0.007357006114165027, z: 0.04779449797522323}


    My empirical guess was {x: 0.001, y: -0.00791666666, z: 0.0475} which is %error {x: 32.7%, y: 7.6%, z: 0.61%}, so I was pretty close esp. on the z component, but still not the "perfect" numbers of box.getCenter().






    share|improve this answer




























      0














      The breakthrough for me on this was to re-frame the problem as how do I change the pivot point for the thumbstick, rather than how do I move the thumbstick to the (default and pre-existing) pivot point. To paraphrase JFK, "ask not how you can move to the pivot, but ask how the pivot can move to you" :-)



      After changing my angle of attack, I pretty quickly found the aforementioned link, which yielded my solution.



      I posted an updated glitch here, so now pressing z works as I expected. Here is the relevant code portion:



        factory.onModelLoaded = function(evt) {
      console.log(`onModelLoaded: entered`);

      factory.thumbstick = this.scene.children[1].children[2]
      let thumb = factory.thumbstick;
      // make the thumb red so it's easier to see
      thumb.material = (new THREE.MeshBasicMaterial({color: 0xFF7777}));

      // use method from https://stackoverflow.com/questions/28848863/threejs-how-to-rotate-around-objects-own-center-instead-of-world-center/28860849#28860849
      // to translate the pivot point of the thumbstick to the the thumbstick center
      factory.thumbParent = thumb.parent;
      let thumbParent = factory.thumbParent;
      thumbParent.remove(thumb);

      var box = new THREE.Box3().setFromObject( thumb );
      box.getCenter( thumb.position ); // this basically yields my prev. "magic numbers"
      // thumb.position.multiplyScalar( - 1 );
      var pivot = new THREE.Group();
      thumbParent.add( pivot );
      pivot.add( thumb );

      thumb.geometry.center();

      // add axeshelp after centering, otherwise the axes help, as a child of thumb,
      // will increase the bounding box of thumb, and positioning will be wrong.
      axesHelper = new THREE.AxesHelper();
      thumb.add(axesHelper);
      }


      Which allows my "z" handler to just rotate without having to do translation:



        case 'z':
      case 'Z':
      var axis = new THREE.Vector3(0, 5, -3).normalize();
      var dir = (evt.key === 'z' ? 1 : -1);
      thumb.rotateOnAxis(axis, factory.ONE_DEG * 5.0 * dir);
      break;


      Interestingly, it's the call to box.getCenter() that generates numbers very close to my "magic numbers":



      box.getCenter()
      Vector3 {x: 0.001487499801442027, y: -0.007357006114165027, z: 0.04779449797522323}


      My empirical guess was {x: 0.001, y: -0.00791666666, z: 0.0475} which is %error {x: 32.7%, y: 7.6%, z: 0.61%}, so I was pretty close esp. on the z component, but still not the "perfect" numbers of box.getCenter().






      share|improve this answer


























        0












        0








        0







        The breakthrough for me on this was to re-frame the problem as how do I change the pivot point for the thumbstick, rather than how do I move the thumbstick to the (default and pre-existing) pivot point. To paraphrase JFK, "ask not how you can move to the pivot, but ask how the pivot can move to you" :-)



        After changing my angle of attack, I pretty quickly found the aforementioned link, which yielded my solution.



        I posted an updated glitch here, so now pressing z works as I expected. Here is the relevant code portion:



          factory.onModelLoaded = function(evt) {
        console.log(`onModelLoaded: entered`);

        factory.thumbstick = this.scene.children[1].children[2]
        let thumb = factory.thumbstick;
        // make the thumb red so it's easier to see
        thumb.material = (new THREE.MeshBasicMaterial({color: 0xFF7777}));

        // use method from https://stackoverflow.com/questions/28848863/threejs-how-to-rotate-around-objects-own-center-instead-of-world-center/28860849#28860849
        // to translate the pivot point of the thumbstick to the the thumbstick center
        factory.thumbParent = thumb.parent;
        let thumbParent = factory.thumbParent;
        thumbParent.remove(thumb);

        var box = new THREE.Box3().setFromObject( thumb );
        box.getCenter( thumb.position ); // this basically yields my prev. "magic numbers"
        // thumb.position.multiplyScalar( - 1 );
        var pivot = new THREE.Group();
        thumbParent.add( pivot );
        pivot.add( thumb );

        thumb.geometry.center();

        // add axeshelp after centering, otherwise the axes help, as a child of thumb,
        // will increase the bounding box of thumb, and positioning will be wrong.
        axesHelper = new THREE.AxesHelper();
        thumb.add(axesHelper);
        }


        Which allows my "z" handler to just rotate without having to do translation:



          case 'z':
        case 'Z':
        var axis = new THREE.Vector3(0, 5, -3).normalize();
        var dir = (evt.key === 'z' ? 1 : -1);
        thumb.rotateOnAxis(axis, factory.ONE_DEG * 5.0 * dir);
        break;


        Interestingly, it's the call to box.getCenter() that generates numbers very close to my "magic numbers":



        box.getCenter()
        Vector3 {x: 0.001487499801442027, y: -0.007357006114165027, z: 0.04779449797522323}


        My empirical guess was {x: 0.001, y: -0.00791666666, z: 0.0475} which is %error {x: 32.7%, y: 7.6%, z: 0.61%}, so I was pretty close esp. on the z component, but still not the "perfect" numbers of box.getCenter().






        share|improve this answer













        The breakthrough for me on this was to re-frame the problem as how do I change the pivot point for the thumbstick, rather than how do I move the thumbstick to the (default and pre-existing) pivot point. To paraphrase JFK, "ask not how you can move to the pivot, but ask how the pivot can move to you" :-)



        After changing my angle of attack, I pretty quickly found the aforementioned link, which yielded my solution.



        I posted an updated glitch here, so now pressing z works as I expected. Here is the relevant code portion:



          factory.onModelLoaded = function(evt) {
        console.log(`onModelLoaded: entered`);

        factory.thumbstick = this.scene.children[1].children[2]
        let thumb = factory.thumbstick;
        // make the thumb red so it's easier to see
        thumb.material = (new THREE.MeshBasicMaterial({color: 0xFF7777}));

        // use method from https://stackoverflow.com/questions/28848863/threejs-how-to-rotate-around-objects-own-center-instead-of-world-center/28860849#28860849
        // to translate the pivot point of the thumbstick to the the thumbstick center
        factory.thumbParent = thumb.parent;
        let thumbParent = factory.thumbParent;
        thumbParent.remove(thumb);

        var box = new THREE.Box3().setFromObject( thumb );
        box.getCenter( thumb.position ); // this basically yields my prev. "magic numbers"
        // thumb.position.multiplyScalar( - 1 );
        var pivot = new THREE.Group();
        thumbParent.add( pivot );
        pivot.add( thumb );

        thumb.geometry.center();

        // add axeshelp after centering, otherwise the axes help, as a child of thumb,
        // will increase the bounding box of thumb, and positioning will be wrong.
        axesHelper = new THREE.AxesHelper();
        thumb.add(axesHelper);
        }


        Which allows my "z" handler to just rotate without having to do translation:



          case 'z':
        case 'Z':
        var axis = new THREE.Vector3(0, 5, -3).normalize();
        var dir = (evt.key === 'z' ? 1 : -1);
        thumb.rotateOnAxis(axis, factory.ONE_DEG * 5.0 * dir);
        break;


        Interestingly, it's the call to box.getCenter() that generates numbers very close to my "magic numbers":



        box.getCenter()
        Vector3 {x: 0.001487499801442027, y: -0.007357006114165027, z: 0.04779449797522323}


        My empirical guess was {x: 0.001, y: -0.00791666666, z: 0.0475} which is %error {x: 32.7%, y: 7.6%, z: 0.61%}, so I was pretty close esp. on the z component, but still not the "perfect" numbers of box.getCenter().







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 8 at 1:54









        vt5491vt5491

        1,11611019




        1,11611019
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017039%2fhow-to-programmatically-undo-positional-translation-to-pivot-point%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            ts Property 'filter' does not exist on type '{}'

            Notepad++ export/extract a list of installed plugins