#Placing an object on a tracked surface

6 messages · Page 1 of 1 (latest)

blissful quartz
#

I'm trying to simply place an object onto a surface, but I'm having some trouble with the APIs. I have a method that works in the simulator, but not on device.

var surfacePosition = deviceTracking.hitTestWorldMesh(screenPoint)

The docs for this method say that world tracking needs to be enabled, but even when I enable it, it doesn't work on my phone. Ideally I would be able to just use surface tracking and perform a hit test that way.

I see there are ray casting method for physics, but do I really need to do all that for this simple thing?

blissful quartz
#

I tried ray casting, but it's not hitting the ground collider.

var camPos = cameraTransform.getWorldPosition()
probe.rayCast(camPos, camPos.add(cameraTransform.forward.uniformScale(1000)), function (hit) {
print('hit? ' + hit)
})

polar lynx
#

One of snap's scripts in the real world scale template has an example of ray casting with surface tracking, here it is extracted:

//@input Component.Camera cam
//@input SceneObject itemToPlace

const EPS = 0.0001;

function hitTestPlane(screenPos, planePoint, planeNormal) {
  const rayOrigin = script.cam.getTransform().getWorldPosition();
  const rayTarget = script.cam.screenSpaceToWorldSpace(screenPos, 10)
  const rayVec = rayTarget.sub(rayOrigin).normalize()
  const denom = planeNormal.dot(rayVec)
  if (Math.abs(denom) > EPS) {
    const difference = planePoint.sub(rayOrigin)
    const t = difference.dot(planeNormal) / denom
    if (t > EPS) {
      const hit = rayOrigin.add(rayVec.uniformScale(t))

      return [{ position: hit, normal: planeNormal }]
    }
  }
  return []
}


var touchStart = script.createEvent("TouchStartEvent")
touchStart.bind(function(eventData){
    var res = hitTestPlane(eventData.getTouchPosition(), new vec3(0,0,0), new vec3(0,1,0))
    if(res && res.length > 0){
        script.itemToPlace.getTransform().setWorldPosition(res[0].position)
    }
})

all you need is the cam with surface tracking enabled

blissful quartz
#

Nice, thanks for the reference! I did manage to get my simple script working (I was adding when I should have been subtracting), but for some reason it would bug out with a black screen on device if I used lerp within the hittest callback

blissful quartz
#

I tracked down the cause of the issue and it was because i used
new quat.fromEulerAngles() and the new was throwing an error on device

#

Here's my script in case it helps anyone