Simulating physics joints in your RealityKit app
At a glance
| Item | Summary |
|---|---|
| Purpose | Create realistic, connected motion using physics joints. |
| App architecture | RealityKitPhysicsJointApp presents MainView, which directly constructs entities, physics bodies, and joints inside RealityView; the compact sample intentionally has no separate model layer. |
| Main patterns | SwiftUI scene composition, UI–RealityKit bridge, Direct joint composition |
| Project style | Code-rich sample with 6 scanned Swift file(s) and 427 implementation line(s). |
Project structure
Source bundle/
└── RealityKit-PhysicsJoint/
└── RealityKit-PhysicsJoint/
├── MainView.swift # MainView
├── RealityKitPhysicsJointApp.swift # RealityKitPhysicsJointApp
├── PendulumSettings.swift # PendulumSettings
├── MainView+PendulumCreation.swift
├── MainView+PendulumModels.swift
└── MainView+PhysicsAndPins.swift
Structure observations
- The runtime boundary is WindowGroup + volumetric window; the tree is pruned to lifecycle, state, framework, rendering, and ECS files.
- The source is Swift; role-named types and feature folders separate the major responsibilities.
Overall architecture
flowchart LR
RealityKitPhysicsJointApp_1["RealityKitPhysicsJointApp"]
WindowGroup___volumetric_window_2["WindowGroup + volumetric window"]
MainView_3["MainView"]
RealityKit_entity_graph_4["RealityKit entity graph"]
RealityKit_physics_bodies_joints_5["RealityKit physics bodies/joints"]
RealityKitPhysicsJointApp_1 --> WindowGroup___volumetric_window_2
WindowGroup___volumetric_window_2 --> MainView_3
MainView_3 --> RealityKit_entity_graph_4
RealityKit_entity_graph_4 --> RealityKit_physics_bodies_joints_5
Reference code
RealityKit-PhysicsJoint/RealityKit-PhysicsJoint/RealityKitPhysicsJointApp.swift:11 — executable or app-lifecycle anchor.
struct RealityKitPhysicsJointApp: App {
var body: some Scene {
WindowGroup {
MainView()
}
#if os(visionOS)
.windowStyle(.volumetric)
#endif
}
}The diagram is a responsibility flow, not a claim that every adjacent node directly calls the next. It separates lifecycle, presentation, stable state, framework/session work, and the RealityKit entity graph.
Ownership and state
classDiagram
MainView *-- PendulumSettings : pendulumSettings
MainView *-- Array : pendulums
PendulumSettings --> Color : stringColor
PendulumSettings --> Color : attachmentColor
Ownership evidence
RealityKit-PhysicsJoint/RealityKit-PhysicsJoint/MainView.swift:18 — representative stored state or nearest verified lifecycle anchor.
struct MainView: View {
// ...
let pendulumSettings = PendulumSettings()
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
MainView |
PendulumSettings as pendulumSettings |
creates and retains | The owning type coordinates writes |
MainView |
Array as pendulums |
owns SwiftUI value state | The owning type coordinates writes |
PendulumSettings |
Color as stringColor |
stores and coordinates | The owning type coordinates writes |
PendulumSettings |
Color as attachmentColor |
stores and coordinates | The owning type coordinates writes |
Ownership is intentionally narrow: environment, bindings, observed objects, weak links, and delegate callbacks do not prove lifetime ownership. Initialized stored services and wrapper-managed state do; entities added inside a RealityKit content closure belong to that entity graph.
Class and protocol design
| Type | Responsibility | Depends on or conforms to |
|---|---|---|
RealityKitPhysicsJointApp (RealityKit-PhysicsJoint/RealityKit-PhysicsJoint/RealityKitPhysicsJointApp.swift:11) |
Declares app lifecycle and top-level dependency lifetime. | App |
MainView (RealityKit-PhysicsJoint/RealityKit-PhysicsJoint/MainView.swift:12) |
Presents UI and forwards gestures or lifecycle events. | View |
The reviewed source defines no local substitution protocol. Its App, View, delegate, renderer, ARKit, and RealityKit conformances are framework-facing contracts, so this document does not label the whole app protocol-oriented.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
struct RealityKitPhysicsJointApp: App { (RealityKit-PhysicsJoint/RealityKit-PhysicsJoint/RealityKitPhysicsJointApp.swift:11) |
implicit internal |
With no narrower modifier, Swift keeps the declaration internal to the module. | Inference: Allow app-target collaboration without exporting a library API. |
struct MainView: View { (RealityKit-PhysicsJoint/RealityKit-PhysicsJoint/MainView.swift:12) |
implicit internal |
With no narrower modifier, Swift keeps the declaration internal to the module. | Inference: Allow app-target collaboration without exporting a library API. |
No reviewed Swift access example uses fileprivate, private(set), public, open; declarations without a modifier are internal.
Reference code
RealityKit-PhysicsJoint/RealityKit-PhysicsJoint/RealityKitPhysicsJointApp.swift:11 — representative visibility boundary.
struct RealityKitPhysicsJointApp: App {
// ...
}Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Application/command lifecycle | RealityKitPhysicsJointApp |
The executable boundary determines app, controller, scene, or command lifetime. |
| Presentation and user intent | MainView |
The view/controller forwards input and owns only presentation-local work. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| SwiftUI scene composition | RealityKit-PhysicsJoint/RealityKit-PhysicsJoint/RealityKitPhysicsJointApp.swift:11 |
Keeps windows, volumes, and immersive presentation visible at the app boundary. |
| UI–RealityKit bridge | RealityKit-PhysicsJoint/RealityKit-PhysicsJoint/MainView.swift:27 |
Builds or hosts the RealityKit entity graph from SwiftUI or a platform view/controller lifecycle. |
| Direct joint composition | RealityKit-PhysicsJoint/RealityKit-PhysicsJoint/RealityKitPhysicsJointApp.swift:11 |
Builds a compact, inspectable relationship between bodies and joints directly in the RealityView scene. |
Naming conventions
- Role suffixes are evidence, not decoration: App:
RealityKitPhysicsJointApp; View:MainView. - Protocols: no app-defined protocol in the reviewed source.
- Commands use verb-led methods:
impulseButtonAction,buildPendulumScene,createPendulum,makeBallEntity,makeStringEntity,makeAttachmentEntity,addPinsTo,addBallPhysics. - Files and folders use primary-type or responsibility names such as
Views,Models,Rendering,Components,Systems,Packages, or feature names where those boundaries exist.
Architecture takeaways
- Treat
RealityKitPhysicsJointAppas the owner of executable or scene lifecycle, not automatically as the owner of every entity created later. - Keep view/controller input near presentation, but move sessions, reconstruction, capture, rendering, shared game state, or documents into stable owners when their lifetime exceeds one callback or render pass.
Source map
| Source file | Relevant symbols |
|---|---|
RealityKit-PhysicsJoint/RealityKit-PhysicsJoint/MainView.swift:12 |
MainView |
RealityKit-PhysicsJoint/RealityKit-PhysicsJoint/RealityKitPhysicsJointApp.swift:11 |
RealityKitPhysicsJointApp |
RealityKit-PhysicsJoint/RealityKit-PhysicsJoint/PendulumSettings.swift:13 |
PendulumSettings |
RealityKit-PhysicsJoint/RealityKit-PhysicsJoint/MainView+PendulumCreation.swift:1 |
Feature implementation |
RealityKit-PhysicsJoint/RealityKit-PhysicsJoint/MainView+PendulumModels.swift:1 |
Feature implementation |
RealityKit-PhysicsJoint/RealityKit-PhysicsJoint/MainView+PhysicsAndPins.swift:1 |
Feature implementation |