RealityKit and Coordinates

Hello,


I wanted to move a certain object by clicking the new position. Is there a way I can do that in RealityKit. The behavior allows moveBy and moveTo. I want to pass the new coordinates of the object by another touch.

Answered by maxxfrazer in 401008022

Assuming you've set up a UITapGestureRecognizer for your ARView, and have a function that starts like this:

@objcfunc handleTap(_ sender: UITapGestureRecognizer? = nil) {


Get the CGPoint of that touch:

guard let touchInView = sender?.location(in: self.arView) else {
  return
}


Perform a raycast at that CGPoint:

if let result = arView.raycast(
  from: touchInView,
  allowing: .existingPlaneGeometry, alignment: .horizontal
).first {
  print(result.worldTransform)
  // worldTransform is of type simd_float4x4
}


From there, use this 4x4 matrix to position your entity at the touch location in the scene using moveTo.

Accepted Answer

Assuming you've set up a UITapGestureRecognizer for your ARView, and have a function that starts like this:

@objcfunc handleTap(_ sender: UITapGestureRecognizer? = nil) {


Get the CGPoint of that touch:

guard let touchInView = sender?.location(in: self.arView) else {
  return
}


Perform a raycast at that CGPoint:

if let result = arView.raycast(
  from: touchInView,
  allowing: .existingPlaneGeometry, alignment: .horizontal
).first {
  print(result.worldTransform)
  // worldTransform is of type simd_float4x4
}


From there, use this 4x4 matrix to position your entity at the touch location in the scene using moveTo.

Thank you very much!


How do I pass the notification (with coordinates) back to realitycomposer? or do I perform the move in Xcode itself.

RealityKit and Coordinates
 
 
Q