First: transform settings in different ways for "X" or "Y" caused massive rotation. I changed both of them to:
$(".cube").css("transform", "rotateX(" + X + "deg) rotateY(" + Y +"deg)");
If you think you want the cube to always rotate in the direction of the swipe. Keep reading if this is the case: or if not , all you had to change was the code above.
Second: When you rotate the cube, you change orientation. The x-axis is never confused, but the y-axis and z-axis are switched without much attention.
Now you can add rotateZ to the mix:
$(".cube").css("transform", "rotateX(" + X + "deg) rotateY(" + Y +"deg) rotateZ(" + Z + "deg)");
I changed the code here in this CodePen to give an example. I did not finish it because I solved this problem, but I do not have time to improve it.
$(function() { var X = 0, Y = 0, Z = 0; var hammertime = new Hammer($(".thirdDimension")[0], {domEvents: true}); hammertime.get('swipe').set({ direction: Hammer.DIRECTION_ALL }); function rotate() { $(".cube").css("transform", "rotateX(" + X + "deg) rotateY(" + Y +"deg) rotateZ(" + Z + "deg)"); $("#debug").html($("#debug").html() + $(".cube").attr("style") + "<br>").scrollTop($("#debug")[0].scrollHeight); } $(".thirdDimension").on("swipeleft", function(e){ //Y -= 90; check(-90); rotate(); }); $(".thirdDimension").on("swiperight", function(e){ //Y += 90; check(90); rotate(); }); $(".thirdDimension").on("swipeup", function(e){ X += 90; rotate(); }); $(".thirdDimension").on("swipedown", function(e){ X -= 90; rotate(); }); function check(num) { var temp = Math.abs(X % 360); switch (temp) { case 0: console.log(temp); Y += num; break; case 90: console.log(temp); Z -= num break; case 180: console.log(temp); Y -= num; break; case 270: console.log(temp); Z -= num; break; } } });
You need to check the y axis ( Math.abs(Y % 360)) ) and possibly the z axis and rotate the y axis and z axis (+ - 90) accordingly.
This basically works, but requires fine tuning when determining the correct axis to rotate. Good luck.
source share