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
Retrieve Data from an Unknown Child Name with Firebase and Swift
Get Out of Navigation Controller and Go Back to Tab Bar View
Arkit: Plot a Node at a Specific Pixel at a Specific Z Distance from Camera
Alamofire Encodes Image in Which Format
How to Bend a Rectangle in Sprite Kit
Animate Line Under One Button to Another
Performance Issue with Swiftui List
Double Variable in Mkmapitem Array
Issues with Uploading from Xcode 9 to App Store/ Itunes Connect
Not Getting Screenlock Notification on Swift 4 on Mac
Hit Fatal Error: Unexpectedly Found Nil While Unwrapping an Optional Value (Lldb)
How to Move an Object Towards a Direction Without Stopping
How to Decorate Siesta Request with an Asynchronous Task
Swift: Handling an Unexpected Nil Value, When Variable Is Not Optional