Hi everyone
I would like to build an AR experience (without web xr). The idea is to populate your surrounding with 3D cubes and be able to walk around them. I’ve created a github/codesandbox with a minimal demo. If you want to test the code run it from your favorite mobile phone. (it’s just an experiment with some code sorry if it’s not working on all phones tested with ios13+ and android OxygenOS 10.0 only for now)
What I did so far:
~Populate your surrounding with 3D objects
~Create a gyro camera to see around you all the objects
~Update the camera position using the accelerometer from you phone to walk around the 3D scene and objects
Details:
- In my scene I have X number of cubes placed around me given a distance and an angle as follow:
public async randomPosition(object: THREE.Object3D) {
const normalizedDistance = new THREE.Vector3()
const randomPosition = new THREE.Vector3()
const distance = getRandomNumber(1, 10)
const angleDeg = getRandomNumber(0, 360)
normalizedDistance.copy(new THREE.Vector3(0, 0, this.distance))
const angleRad = THREE.MathUtils.degToRad(this.angleDeg)
randomPosition.copy(normalizedDistance.applyAxisAngle(new THREE.Vector3(0, 1, 0), angleRad))
object.position.copy(this.randomPosition)
}
}
-
Then I used a gyro camera and now when I move around with my phone I’m able to see all the cubes around me. For now this code is based on the DeviceOrientationControls.
-
Finally I would like to use the
devicemotion
event to get the acceleration x/y/z of my device and update the camera’s position with those values every frame to be able to walk around my cubes. At the moment I’m doing this:
const normalizedDistance = new THREE.Vector3()
const currentPosition = new THREE.Vector3()
const accX = this.findDistance(this.accelerationX, 0.9) // m/s^2 convert to distance <=> 0.5 * acceleration * speed ** 2
const accY = this.findDistance(this.accelerationY, 0.9)
const accZ = this.findDistance(this.accelerationZ, 0.9)
normalizedDistance.copy(new THREE.Vector3(accX, accY, accZ))
const angleRad = THREE.MathUtils.degToRad(this.rotationGamma)
currentPosition.copy(
normalizedDistance.applyAxisAngle(new THREE.Vector3(0, 1, 0), angleRad)
)
this.camera.position.copy(currentPosition)
The result I’m getting is the camera jiggling a lot when using a value of ~1s for the findDistance (see code above: findDistance(this.accelerationX, speed) // m/s^2 convert to distance <=> 0.5 * acceleration * speed ** 2
) and the camera is not moving when using the clock delta time for the speed.
Also when I’m moving my phone the whole scene is moving with it, at the same time, so it doesn’t give the effect of a camera moving throughout the 3D environment around you, if it makes sense.
I’m looking for advices, maybe someone has already been through this and would know if I’m on the good way or not. Thank you.