Some problems with camera rotations and object rotations in three.js - javascript

I'm trying to make the object rotate with the camera my code for the rotation currently looks like this:
function update() {
render();
requestAnimationFrame(update)
movement();
var test = camera.getWorldRotation();
player.rotation.y = test.y
}
I can only rotate the object 45 degrees to the left and to the right then the player rotation stops rotating while the camera can keep on rotating.

Related

How to rotate a cube based on where Three.js camera is looking?

Is there a way to rotate a cube in Three.js based on the side of the cube the camera is facing?
I'm using the below code to change the orientation the cube rotates from based on the change in the vector returned by camera.getWorldDirection().
Is there a better way to do this? When I use my mouse to scroll around the cube to a different face than the one the camera originally looked at and hit 'w'(the key to move the cube face up), the face the camera originally looked at will move up instead of the face the camera is looking at.
if (camera.getWorldDirection(new THREE.Vector3()).x > 0.50) {
let rotation = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0,0,1),-rotationStep);
cubeQuaternion.multiplyQuaternions(rotation, cubeQuaternion); // rotate cube
}
else {
let rotation = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(1,0,0), rotationStep);
cubeQuaternion.multiplyQuaternions(rotation, cubeQuaternion);
}

threejs How to rotate transformcontrol itself

I'd like to rotate transformcontrol itself when object which attach to transformcontrol is rotate.
Before rotate
After rotate
As image shown, before rotate, cylinder's top direction is z-axis of transformcontrol, however after rotate is not.
I'd like that the transformcontrol's z axis to be always in the upward direction of the cylinder.
TransformControls returns an object that you can modify directly.
const controls = new TransformControls(camera, renderer.domElement);
controls.setSpace('local');
controls.rotation.x = Math.PI / -2;
If you want to sync its rotation with your mesh, you can add it as a child of that mesh or use Euler#copy.
mesh.add(controls);
// or manually in a render loop, for example
scene.add(controls);
renderer.setAnimationLoop(() => {
controls.rotation.copy(mesh.rotation);
renderer.render(scene, camera);
});

How can I write a line of code for p5.js that works as a camera follower for my player? (var Mover;) This is a 2D game

https://editor.p5js.org/LttntLark/sketches/JBSg2zIfEf
All the code is above.
I've been trying to make a camera that follows the player(Mover), and it says I need WEBGL. Is there a way to do this without it in p5.js? I've searched up tutorials, and nothing says I can make a "camera" for p5 in 2d, and that I "NEED WEBGL" And I cant remodel my game in WEBGL, so I need a code that is a "viewport" that keeps my player centered on the screen while he moves.
To enable WebGL mode, add WEBGL to your createCanvas()
Example:
createCanvas(1500, 2560, WEBGL);
// Let WIDTH be the canvas width
// Let HEIGHT be the canvas height
// Let player be the center of perspective (with x and y positions)
function draw() {
// Do not push/pop
// This line of code should affect all drawing below
//
translate(WIDTH / 2 - player.x, HEIGHT / 2 - player.y);
//
// This line translate the camera making the player always centered
/////////////////
//
// Your other drawings here
//
/////////////////
}

How do I get camera position and rotation in A-Frame?

I can create an entity as camera:
<a-entity id="camera" camera look-controls wasd-controls></a-entity>
and get the position and rotation information, but in VR mode the built-in, default camera automatically takes over.
How do I get the same kind of information about that default camera?
Or maybe I need a different KIND of information?
I tried this:
var scenic = document.querySelector('a-scene');
var cam = scenic.camera;
console.log("camera position x= " + cam.position.x);
But no matter when I trigger the cosole.log (ie; after moving about in the scene) it still signals:
camera position x= 0
For context: I want to make a projectile launcher that follows the camera around and allows users to launch from camera position in the direction and angle of the camera's FOV...
See example (triggered, at the moment, by pressing the P key in the registered component "launcher")
https://glitch.com/edit/#!/query-aframe-camera-position-rotation?path=index.html:27:0
var position = document.querySelector('a-scene').camera.el.object3D.position;

Three js plane geometry disappears when rotating

I'm trying to create a very simple scene containing a triangular planar face continuously rotating about the x axis.
Here's the code creating the geometry object, as indicated in this previous SO question:
// create triangular plane geometry
var geometry_1 = new THREE.Geometry();
var v1 = new THREE.Vector3(0,0,0);
var v2 = new THREE.Vector3(3,0,0);
var v3 = new THREE.Vector3(0,3,0);
geometry_1.vertices.push(v1);
geometry_1.vertices.push(v2);
geometry_1.vertices.push(v3);
geometry_1.faces.push(new THREE.Face3(0, 1, 2));
The animation function renders the scene and adds a small rotation to the mesh:
function animate() {
requestAnimationFrame( animate );
mesh_1.rotation.x += 0.005;
renderer.render( scene, camera );
}
Everything works fine until the value of mesh.rotation.x goes into the [Math.PI, 2*Math.PI] interval, at which point it disappears for exactly half of the cycle. This JSFiddle replicates the behavior I'm observing.
This is not a light problem, as there are an ambient light and a directional light supposed to illuminate the mesh at all points of it revolution.
This should not be a material problem, as I did set its side property to THREE.DoubleSide and in fact in the interval mesh.rotation.x into [0, Math.PI] I already observe both faces.
I tried adding another face to the same geometry with geometry_1.faces.push(new THREE.Face3(0, 2, 1)); but that still didn't solve the problem.
Adding a second geometry with an opposite face geometry_2.faces.push(new THREE.Face3(0, 2, 1)); and having the mesh rotate negatively mesh_2.rotation.x -= 0.005; allows me to observe the desired result because the two geometries are now disappearing in opposite halves of the [0, 2*Math.PI] interval. This however is a hacky and not ideal solution.
So what is going on? How can I solve it? Thanks!
Documentation says:
OrthographicCamera( left, right, top, bottom, near, far )
so, set your camera like this:
camera = new THREE.OrthographicCamera(-FRUSTUM_SIDE/2, FRUSTUM_SIDE/2,
FRUSTUM_SIDE/2, -FRUSTUM_SIDE/2);
thus you'll have default near and far camera frustrum planes (0.1 and 2000).
Explanation: You set your cam at z-position, which is equal to FRUSTRUM_SIDE/2 and also you set your far camera frustrum plane with the same value. So you see everything between your cam position and the distance from it, which is FRUSTRUM_SIDE/2. In world coordinates, your far plane is at point (0, 0, 0). That's why your triangle disappears when it goes further then the distance of FRUSTRUM_SIDE/2 from your cam.
Extending the answer from #prisoner849, the problem shown in the JSFiddle has nothing to do with the geometry or the material of the mesh, but with the shape and extension of the frustum defined by the OrthographicCamera.
As explained nicely in this video tutorial and in the documentation the frustum of an OrthographicCamera is a rectangular parallelepiped defined by the values left, right, top, bottom, near, far:
The camera should effectively be thought of as being attached to the surface on the near side and facing towards negative values of the z axis.
Once the frustum's shape is defined with:
FRUSTUM_SIDE = 20;
camera = new THREE.OrthographicCamera(
-FRUSTUM_SIDE/2, FRUSTUM_SIDE/2,
FRUSTUM_SIDE/2, -FRUSTUM_SIDE/2,
-FRUSTUM_SIDE/2, FRUSTUM_SIDE/2);
we will be able to see in our image all the objects in the scene which are entirely contained in it.
However, after defining the frustum the position of the camera is changed: camera.position.z = FRUSTUM_SIDE/2; (line 24 of the fiddle). This effectively moves the whole frustum and not just the location of the image, so while previously any object in (0,0,0) was in the center of the frustum, now it will lie on the very far end plane of it.
The animation function:
function animate() {
requestAnimationFrame( animate );
mesh_1.rotation.x += 0.005;
renderer.render( scene, camera );
}
rotates the triangle towards the image plane, but for angles between [Math.PI, 2*Math.PI] the plane is effectively pointing outside of the frustum and thus becomes invisible.
Removing the camera.position.z = FRUSTUM_SIDE/2; line, or defining a different frustum, or moving the mesh position to the middle of the new frustum are all possible solutions. I.e. corrected fiddle.

Categories