Realitykit - Updating Entity's Translation Returns Unexpected Values

RealityKit – Rotating an Entity affects its Scale

You need a starting transform "point" and ending transform "point". If a value of referenceEntity (relativeTo) argument equal to nil it means relative to world space. Since the same 4x4 matrix slots are used for rotation values ​​as for scaling, when the model is rotated, its scale also changes at the same time, if there is a difference in scale.

For perpetual transform animation use some of RealityKit 2.0 tricks.

And, of course, there is a Trigonometry that was really conceived for perpetual orbiting.

Here's a correct version of your code:

import UIKit
import RealityKit
import Combine

class ViewController: UIViewController {

@IBOutlet var arView: ARView!
var cancellable: Cancellable? = nil
let anchor = AnchorEntity()

override func viewDidLoad() {
super.viewDidLoad()

cancellable = ModelEntity.loadAsync(named: "drummer.usdz").sink { _ in
self.cancellable?.cancel()
} receiveValue: { entity in
self.anchor.addChild(entity)
self.arView.scene.addAnchor(self.anchor)

let rotation = Transform(pitch: 0, yaw: .pi, roll: 0)
entity.move(to: rotation,
relativeTo: entity,
duration: 5.0,
timingFunction: .linear)
}
}
}

RealityKit – AnchorEntity moves only to Origin

Apply anchor's rotation relative to model entity:

let model = ModelEntity(mesh: .generateBox(size: 0.2))
let anchor = AnchorEntity(world: [0, 0.5, 0])
anchor.addChild(model)

let currentMatrix = anchor.transform.matrix
let rotation = simd_float4x4(SCNMatrix4MakeRotation(.pi/2, 0, 1, 0))
let transform = simd_mul(currentMatrix, rotation)
anchor.move(to: transform, relativeTo: model, duration: 3.0)

This post is also useful.



Related Topics



Leave a reply



Submit