V
V
Vsevolod Zhdanov2014-07-10 22:58:11
JavaScript
Vsevolod Zhdanov, 2014-07-10 22:58:11

How to move an object in 3D space?

Here I perfectly understand how in 2D space you can move an object by a certain degree:

x+=Math.cos(angle)*speed;
y+=Math.sin(angle)*speed;

But I do not understand at all how to attribute the third coordinate and the third axis here. I am writing this all in Three.js. Here is the actual code:
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 20000);

var light = new THREE.PointLight( 0xffffff, 1, 2000 );
light.position.set(0,0,0);scene.add( light );

var x = 0, y = 0, z = -500, speed = 0, angle = 0, angle2 = 0;

var geometry = new THREE.CylinderGeometry( 4, 15	, 100, 6 );
var material = new THREE.MeshLambertMaterial( {color: 0xffffff} );
var cylinder = new THREE.Mesh( geometry, material );
scene.add( cylinder );

var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
renderer.shadowMapEnabled = true;
renderer.setClearColorHex( 0x00000, 1 );

var render = function () {
  requestAnimationFrame(render);
  renderer.render(scene, camera);
  switch (key){ 
    case 38: angle2+=0.1; break;
    case 40: angle2-=0.1; break;
    case 37: angle+=0.1; break;
    case 39: angle-=0.1; break;

    case 90: speed-=0.5; break;
    case 88: speed+=0.5; break;
  }
  light.position.set(camera.position.x,camera.position.y+3,camera.position.z-3);

  cylinder.rotation.z=-angle;
  cylinder.position.set( x+=Math.sin(angle)*speed*2, y+=Math.cos(angle)*speed, z );
};
document.onkeydown = keyDn; document.onkeyup = keyUp; key=0;
function keyDn(e) {key = e.which;console.log(key)}
function keyUp(e) {key = 0;}
render();
Заранее спасибо за помощь!)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vsevolod Zhdanov, 2014-07-11
@jobsstar

It turns out that for each axis you need an angle ... Got it)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question