rotation - THREEJS: Rotating the camera while lookingAt -
i have moving camera in camera container flies arond scene on giving paths airplane; can move position x,y,z positive , negative. camera container looking @ own future path using spline curve.
now want rotate camera using mouse direction still keeping general looking @ position while moving forward object. say, want turn head on body: while moving body having general looking @ direction, turning head around lets 220 degree , down. can't behind body.
in code cameracontainer responsible move on pline curve , lookat moving direction. camera added child cameracontainer responsible rotation using mouse.
what don't working rotation of camera. guess common problem. lets camera when moving on x-axes moves not straight, moves curve. specially in different camera positions, rotation seems different. tryiing use cameracontainer avoid problem, problem seems nothing related world coordinates.
here have:
// camera in container cameracontainer = new threejs.object3d(); cameracontainer.add(camera); camera.lookat(0,0,1); cameracontainer.lookat(nextpositiononsplinecurve); // here goes rotation depending on mouse // vertical var mouseverti = 1; // 1 = top, -1 = bottom if(window.app4d.mouse.y <= instance.domcenterpos.y) // mouse bottom? mouseverti = -1; // how far mouse away center: 1 most, 0 near var ymouseperc = math.abs(math.ceil((instance.domcenterpos.y - window.app4d.mouse.y) / (instance.domcenterpos.y - instance.domboundingbox.bottom) * 100) / 100); var yanglediffside = (instance.config.scene.camera.maxangle - instance.config.scene.camera.angle) / 2; var yrotateran = mouseverti * yanglediffside * ymouseperc * math.pi / 180; instance.camera.rotation.x += yrotateran; // rotation x = vertical // horizontal var mousehori = 1; // 1 = top, -1 = bottom if(window.app4d.mouse.x <= instance.domcenterpos.x) // mouse left? mousehori = -1; // how far mouse away center: 1 most, 0 near var xmouseperc = math.abs(math.ceil((instance.domcenterpos.x - window.app4d.mouse.x) / (instance.domcenterpos.x - instance.domboundingbox.right) * 100) / 100); var xanglediffside = (instance.config.scene.camera.maxangle - instance.config.scene.camera.angle) / 2; var xrotateran = mousehori * xanglediffside * xmouseperc * math.pi / 180; instance.camera.rotation.y += xrotateran; // rotation y = horizontal
would thankful if can give me hint!
i got answer after more trial , error. solution take initial rotation of y in consideration.
when setting camera container , real camera child of container, had point camera frontface of camera container object, in order let camera looking in right direction. lead initial rotation of 0, 3.14, 0 (x,y,z). solution added 3.14 y rotation everytime assigned (as mentioned westlangley) mouse rotation.
camerareal.lookat(new three.vector3(0,0,1)); camerareal.rotation.y = xrotateran + 3.14;
Comments
Post a Comment