0

I have two halves of a card, I need to animate the card opening. So thus, I need to rotate the card's front around the leftmost point of that piece, not the centre, obtaining code to "rotate around a point" yields poor results:

//geometry for each half
var geometry1 = new THREE.PlaneGeometry( 10, 15 );
//rotation, offset by half width
...
  else if (e.keyCode == '87'){
        openAng+=0.1;
        rotCusAxis(card, new THREE.Vector3( -5, 0, 0 ),new THREE.Vector3(0,1,0),openAng,false);
        }

...
function rotCusAxis(obj, point, axis, theta, pointIsWorld){
    pointIsWorld = (pointIsWorld === undefined)? false : pointIsWorld;

    if(pointIsWorld){
        obj.parent.localToWorld(obj.position); // compensate for world coordinate
    }

    obj.position.sub(point); // remove the offset
    obj.position.applyAxisAngle(axis, theta); // rotate the POSITION
    obj.position.add(point); // re-add the offset

    if(pointIsWorld){
        obj.parent.worldToLocal(obj.position); // undo world coordinates compensation
    }

    obj.rotateOnAxis(axis, theta); // rotate the OBJECT
}

issue.png

What alternative way could I do to fix this?

BinkyNichols
  • 586
  • 4
  • 14
  • It's not exactly clear how you want to unfold the card, but I'm assuming it's not something like this [examples](https://jsfiddle.net/t3nyvoph/), is it? – ScieCode Jul 25 '19 at 19:37
  • No it's two THREE.Meshs of type THREE.PlaneGeometry. I'm trying var x = Math.cos(rot)*(width/2); var z = Math.cos(rot)*(width/2); card.applyMatrix( new THREE.Matrix4().makeTranslation( -x, 0, -z ) ); card.applyMatrix( new THREE.Matrix4().makeRotationY( openAng) ); card.applyMatrix( new THREE.Matrix4().makeTranslation( x, 0, z ) ); but it just rotates it around a circle, which I knew it would but thought it would look right (ie the two card halves still look connected) – BinkyNichols Jul 25 '19 at 21:18

1 Answers1

1

You're on the right track. The "subtract, rotate, add" pattern is correct. But three.js provides some nice convenience functions to make performing these tasks a lot easier.

For more information, check out this answer: Three JS Pivot point

const W = window.innerWidth
const H = window.innerHeight

const renderer = new THREE.WebGLRenderer({
  antialias: true
})
renderer.setSize(W, H)
renderer.setClearColor(0xfafafa)

document.body.appendChild(renderer.domElement)

const scene = new THREE.Scene()

const camera = new THREE.PerspectiveCamera(28, W / H, 50, 150)
camera.position.set(0, 25, 100)
camera.lookAt(scene.position)
scene.add(camera)

const light = new THREE.DirectionalLight(0xffffff, 1)
light.position.z = -1
camera.add(light)

const cardGeo = new THREE.PlaneBufferGeometry(15, 25)
const leftMat = new THREE.MeshLambertMaterial({
  color: "orange",
  side: THREE.DoubleSide
})
const rightMat = new THREE.MeshLambertMaterial({
  color: "blue",
  side: THREE.DoubleSide
})

const left = new THREE.Mesh(cardGeo, leftMat)
left.position.x = -7.5

const right = new THREE.Mesh(cardGeo, rightMat)
right.position.x = 7.5

scene.add(left)
scene.add(right)

function render() {
  renderer.render(scene, camera)
}

const rotateCard = (function(){
  const hingeAxis = new THREE.Vector3(0, 1, 0)
  let angle = 0
  let direction = 1
  let motion = 0
  return function(){
    // For me, the pivot point is at the origin.
    // See https://stackoverflow.com/questions/42812861/three-js-pivot-point/42866733#42866733
    // for non-origin pivot points
    motion = 0.025 * direction
    left.position.applyAxisAngle(hingeAxis, motion) // rotate position
    left.rotateOnAxis(hingeAxis, motion) // rotate object
    if(angle + motion <= 0 || angle + motion >= Math.PI){
      direction *= -1
    }
    angle += motion
  }
})()

const axis = new THREE.Vector3(0, 1, 0)
function animate(){
  requestAnimationFrame(animate)
  camera.position.applyAxisAngle( axis, 0.005 )
 camera.lookAt( scene.position )
  rotateCard()
  render()
}
animate()
html,
body {
  margin: 0;
  padding: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/106/three.min.js"></script>
TheJim01
  • 8,411
  • 1
  • 30
  • 54