My iOS app makes use of RealityKit with Physics to maneuver entities. It requires to place objects on a board. This should be carried out with a completely inelastic affect, i.e. the article should not bounce again when placed on the board.
Sadly, I used to be not in a position to obtain this habits. I thus wrote a little bit check app to research the scenario. This app is proven under.
The app units up a board and above it a field. Each entities use a physicsBody
in order that they will take part within the Physics simulation.
The mode
of the board is .static
, i.e. it doesn’t transfer. The mode
of the field is .dynamic
, i.e. it strikes instantly after app begin down by gravity.
The PhysicsMaterialResource
of each entities has its restitution
set to 0, i.e. the affect needs to be completely inelastic.
Each entities have collision shapes, in order that the field collides with the board after a brief fall.
The important check parameter is the scaleFactor
at first of the code. Relying on its worth, I noticed completely different habits:
- If
scaleFactor
is = 0.1, the field collides with the board, and stops as anticipated. - If
scaleFactor
is = 0.02, the field is bounced again hardly, and disappears in direction of the highest. - If
scaleFactor
is = 0.01, The field doesn’t collide with the board, and falls via.
I imagine that is RealityKit bug, and if any individual can verify this, I’ll write a bug report.
The code:
import SwiftUI
@most important
struct RestitutionApp: App {
var physique: some Scene {
WindowGroup {
ContentView()
}
}
}
import RealityKit
import SwiftUI
let scaleFactor: Float = 0.1 // right habits
//let scaleFactor: Float = 0.02 // affect shouldn't be inelastic
//let scaleFactor: Float = 0.01 // field falls via the board
struct ContentView: View {
let scale = scaleFactor * SIMD3(1, 1, 1)
let boxSize: Float = 0.5/scaleFactor
var physique: some View {
RealityView { content material in
let board = makeBoard()
board.place = [0, 0, -3]
content material.add(board)
let field = makeBox()
field.place = [0, 1, 0]
board.addChild(field)
}
}
func makeBoard() -> ModelEntity {
let mesh = MeshResource.generateBox(width: 2, peak: 0.2, depth: 1.0)
var materials = UnlitMaterial(); materials.shade.tint = .purple
let boardEntity = ModelEntity(mesh: mesh, supplies: [material])
boardEntity.generateCollisionShapes(recursive: false)
let physicsMaterial = PhysicsMaterialResource.generate(friction: 0, restitution: 0)
boardEntity.physicsBody = PhysicsBodyComponent(massProperties: .default,
materials: physicsMaterial,
mode: .static) // The board doesn't transfer
return boardEntity
}
func makeBox() -> ModelEntity {
let mesh = MeshResource.generateBox(measurement: boxSize)
var materials = UnlitMaterial(); materials.shade.tint = .inexperienced
let boxEntity = ModelEntity(mesh: mesh, supplies: [material])
boxEntity.scale = scale
boxEntity.generateCollisionShapes(recursive: false)
let physicsMaterial = PhysicsMaterialResource.generate(friction: 0, restitution: 0)
boxEntity.physicsBody = PhysicsBodyComponent(massProperties: .default,
materials: physicsMaterial,
mode: .dynamic)
return boxEntity
}
}