Slide 18
Slide 18 text
タイトル
RealityView { content in
// OrbitSystemを登録
OrbitSystem.registerSystem()
...
// OrbitComponentを追加(半径1m、周期10秒、Y軸まわり)
sphereEntity.components.set(OrbitComponent(radius: 1.0, period: 10.0, axis: [0, 1, 0]))
content.add(sphereEntity)
}
struct OrbitComponent: Component {
let radius: Float
let angularVelocity: Float
var currentAngle: Float
let axis: SIMD3
init(radius: Float, period: Double, axis: SIMD3 = [0, 1, 0]) {
self.radius = radius
// 周期から角 度を計算(
1周 = 2π rad)
self.angularVelocity = Float(2.0 * .pi / period)
self.currentAngle = 0.0
self.axis = normalize(axis)
}
}
コーディングガイドラインを参照させてリトライ
## コーディングガイドライン
@.claude/RealityKitDevelopmentRules.md
RealityKit OrbitAnimationを使って、
Entityを円周軌道上でアニメーションさせてい
る 良さそうだが、複雑な構成 シーンや
動きだと厳しそう
struct OrbitSystem: System {
static let query = EntityQuery(where: .has(OrbitComponent.self))
init(scene: RealityKit.Scene) {}
func update(context: SceneUpdateContext) {
for entity in context.entities(matching: Self.query, updatingSystemWhen: .rendering) {
guard var orbitComponent = entity.components[OrbitComponent.self] else { continue }
// 角度を更新
orbitComponent.currentAngle += orbitComponent.angularVelocity * Float(context.deltaTime)
// 軌道上 位置を計算
// Y軸まわり 回転 場合:
X-Z平面上で円運動
if orbitComponent.axis == SIMD3(0, 1, 0) {
let x = cos(orbitComponent.currentAngle) * orbitComponent.radius
let z = sin(orbitComponent.currentAngle) * orbitComponent.radius
entity.position = SIMD3(x, 0, z)
} else {
// 他 軸 場合 回転行列を使用
let rotationMatrix = simd_float3x3(
simd_quatf(angle: orbitComponent.currentAngle, axis: orbitComponent.axis)
)
let initialPosition = SIMD3(orbitComponent.radius, 0, 0)
entity.position = rotationMatrix * initialPosition
}
// コンポーネントを更新
entity.components[OrbitComponent.self] = orbitComponent
}
}
}
ちゃんとSystemを登録したり、
Componentを追加している
System.update内でもちゃんとXZ平面で 座
標を計算して更新している