Find viewing angle from DeviceOrientationControls in threejs - javascript

I'm making a WebVR environment. I've added controls so you can navigate in the scene and added DeviceOrientationControls so I can use the scene in my cardboard.
However, the controls I'm using now are influencing the camera and when I'm looking around (by tilting my phone around) the controls don't get translated to where I'm looking.
For example, I'm in my scene and I'm looking backwards (so I turned my head 180°), if I now go forwards with my controls, I will visually be going backwards because my vision has been rotated 180°.
I'd like my controls to "follow" my viewing angle. I found a Stack Overflow post suggesting I'd put it in a THREE.Object3D() so I can rotate the parent object, but I'm not sure this will be the desired effect, or will it?...

I probably should've spent more time looking at the (many) three.js examples. There's an example for this available at https://threejs.org/examples/?q=poin#misc_controls_pointerlock

Related

ThreeJS raycast to detect animation model?

I want to detect when the user in my ThreeJS is looking at one of the animation models. Currently I am using this code:
ourRaycaster.setFromCamera(g_Mouse, g_ThreeJsCamera);
g_AryObjectsLookedAt = ourRaycaster.intersectObjects(g_ThreeJsScene.children, true);
The problem is that although the raycaster will detect any collisions between the current camera line of sight and a child object in the animation models group, it will not intersect with any empty spaces in the group. Although this makes sense from a technical point of view, it doesn't match what we do as humans when looking at an object. When we look at an object, we mentally draw a shape around the overall object and if we are looking inside that pseudo-shape we feel that we are looking at the object.
For example, if you have a big head and wide shoulders, but a very thin neck, if I focus momentarily at the space alongside your neck I still feel that I am looking at "you".
What I need is a raycaster that when it passes through an animation model's group of children, does approximately the same thing.
Here is a concrete example. I am using the sample robot animation model from the ThreeJS examples. It has a really big head. When I do the raycast from this distance, no part of the robot is in the intersecting objects list:
But if I get right up "in its face", it is detected (it's "face" is detected):
This is technically accurate but I need something a lot more forgiving so I can create proper logic for interactions between animation model and the user. How can I do this without having to iterate manually with every animation model in the virtual scene?

Three.js SkinnedMesh jitter when far from scene root (0,0,0) on IOS

I'm working on a 3rd person RPG style game using Three.js and Blender. The world terrain is tiled and endlessly loops in all directions, getting triggered when the player obj nears an edge defined along z or x. I'm using the FBX importer, and things work fine on most platforms.
On IOS devices when I move the player object any significant distance from the scene root, the surface materials start to jitter and break apart. The further from root, the worse it gets. It's apparently related to how IOS devices handle floating point calcs, as referenced in these related threads:
GitHub Three.js
Related (from '04)
Related from '07
If working solutions have been figured out, no specific/applied examples have been given that I could find.
I've tried multiple approaches, but unsuccessfully. I don't really want to move the world instead of the player. When I've tried, I'm just not smart enough to figure it out. Moving along z isn't bad, but I can't get object rotations on y or shifting pivot points to work, no matter what groups or parent/child relations are established, or what gets copied and set from what. Matrix transforms of the mesh haven't worked well either.
This is likely a really dumb question, but is there a simpler way to just reset an origin or scene root to wherever the players current position is? Like gets done with cameras, lights, positions and rotations?
I'd really like to get this project working on IOS devices.
Thanks in advance for any suggestions.
When you run into floating-point precision issues, you have two choices:
Make your world smaller. Let's say if your main character is 1000 units tall, you're "wasting" a lot of precision. You could make it 10 or 1 unit tall, and also scale everything else down so you never reach values high enough to encounter the bug.
You could simply nest your environment separately from your main character, and move that instead. Something like this:
scene = new THREE.Scene();
// Instead of adding environment features to scene,
// you add them to the environment group
environment = new THREE.Group();
environment.add(trees);
environment.add(streets);
environment.add(buildings);
character = new THREE.Mesh(geometry, material);
// Add environment separately from your character
scene.add(environment);
scene.add(character);
// Instead of moving your character,
// move the environment in the opposite direction
environment.position.set(-x, -y, -z);
This way, everything that's visible is close to 0, 0, 0. If you need to perform rotations, use the same rule: apply it to the environment, but in the opposite direction.

Is it possible to make a viewport that follows the player using vanilla javascript?

I'm wondering how to make a viewport that follows the player such as in sidescrolling games. I have a semi-working version, but it requires me to move everything except the player.
ctx.translate(canvX,canvY);
drawBlocks();
ctx.restore()
This works for now, but I will have to draw enemies and other objects, and I don't want to constantly have to redo the process. I'm looking for a simple solution that basically involves a camera that follows the player. Is this possible?
Use something like three.js for games. Because you have to draw as many frames per second, and canvas just isn't great for that (if you don't believe me now, wait until you have to draw more things on the screen).
However, for your current code, one thing I notice is you're missing a save.
If that's not the problem, which I dont think it is, based on your question, you don't want to re-draw everything, only the background? You could actually use multiple layers, so that each enemy is an HTML element, and you only redraw the enemy when their animation frame changes. Then you just move their element ( a little cheaper than re-drawing in terms of performance ).
THREE.JS is what you should learn.. it will really help you out.

How can I move the camera in ThreeJS relative to perspective?

Question:
I've been working on a first-person maze game with Threejs. I have recently included DeviceOrientationControls to start moving it towards VR, but my previous system for moving the camera is separated from the camera. The camera no longer moves with the arrow keys.
How can I move my camera using arrow keys again while the camera is updated with DeviceOrientationControls?
If possible, how can I automate forward movement relative to the camera's perspective?
Update:
Alex Fallenstedt found a perfect example of what I wanted.
However, I have some questions;
How does the script make the camera move?
How can I simplify this and/or implement this into my work?
Resources:
How to control camera both with keyboard and mouse - three.js
Detecting arrow key presses in JavaScript
How to move camera along a simple path
How to control camera movement with up,down,left,right keys on the keyboard
Comparison:
Here's how it behaved prior (with working controls)
http://orenda.ga/stackoverflow/Nonvr.mp4
Here's how it behaves now (with Orientation)
http://orenda.ga/stackoverflow/VR.mp4
Note:
I've not included my script since I think that it isn't needed for this question. If you need it, please ask and I will insert it here.
To answer you two questions:
1) How does the script make the camera move?
Lets break the script up to its fundamentals. The script begins by adding a bit of state to determine if the user is moving forward.. This state changes when the user interacts with W,A,S,D. We add event listeners that will change this state when a user presses a key or lifts up from a key.. Now, every single frame that is rendered, we can add velocity in specific directions depending on the state of what keys are pressed. This happens here. We now have a concept of velocity. You should console log the velocity values in animate() and checkout how it changes as you move around.
So how does this velocity variable actually move the camera? Well, this script is also using an additional script called PointerLockControls. You can see the full script for PointerLockControls here. Notice how PointerLockControls' only argument is a camera. In order to move the camera, we need to use some nifty functions in PointerLockControls like getObject.. This returns the THREE.js object that you passed into PointerLockControls (aka the camera).
Once we can obtain the camera, we can then move the camera by translating its x, y, and z values by the velocity we created earlier in our animate function.. You can read more about translating objects with these methods in in the Object3D documentation.
2) How can I simplify this and/or implement this into my work?
This is probably the easiest way to implement first person controls to a camera. Everything else in the script example I shared with your earlier is for checks. Like if the user is in a threeJS object, allow them to jump. Or adding a concept of mass and gravity to always pull the user down after they jump. One thing that you might find very useful is to check if the pointer is in the browser's window!. Take the example, break it down to its simplest components, and build from there.
Here is a codepen example that shows you how to move the camera forward smoothly, without pointerlockcontrols, Start at line 53 https://codepen.io/Fallenstedt/pen/QvKBQo

3D cubes draggable to grid

I'm working on a project for school in HTML5 en CSS3.
The goal of the project is to teach young children how to calculate simple equations.
The first step towards learning this is teaching them to recognize numbers in different shapes.
A first exercise would be:
showing a random number and letting the child select a 3D cube and dragging it inside of a grid.
The number of cubes in the grid should correspond ofcourse with the given number.
Example given below:
I have no idea where to start. I know about a canvas in HTML5 but I'm not really familiar with it.
How can I snap the cubes into position when they come close?
How do I even draw a 3D cube in HTML5?
How can I check how many cubes were drawn on screen?
Can I draw something more pleasing for the children to look at than cubes, but still have a 3D effect?
Above all will it be capable of running on a iPad?
A dedicated App is out of the question as it should also be able to run on a desktop.
Hoping some of you might have a good solution.
Thanks
Only way to make genuin 3D cube that will work on ios browser is css3
You can put this cube(graphic representation) in to the present element that will represent its logically and use standard browser ways to manage drag and drop Usefully library and since this is DOM based implementation it can be styled with css. You can add color transition, animation delays or even deformation with the css3 transition it will have much better performance than js animations. Also it's easier to implement.
Here(video, images) is really cool animation guidance from Walt Disney Studios to help you make it visually appealing.

Categories