The problem was that I was giving the spheres so little linear velocity that it looked like setting the mode to .dynamic was stopping them but it was just friction. The following seems to work:
sphere.physicsBody?.mode = .kinematic
sphere.physicsMotion?.linearVelocity = [.random(in: -3...3), 0, .random(in: -3...3)]
DispatchQueue.main.asyncAfter(deadline: .now() + DispatchTimeInterval.milliseconds(1)) {
	sphere.physicsBody?.mode = .dynamic
}
In dynamic mode applyLinearImpulse() seems to work but the entity needs to be active. I don't know a good way to detect when an entity becomes active apart from a delay after an anchor is added to the session.
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
DispatchQueue.main.asyncAfter(deadline: .now() + DispatchTimeInterval.seconds(1)) {
sphere.applyLinearImpulse([.random(in: -3...3), 0, .random(in: -3...3)], relativeTo: nil)
}
}
Topic:
Graphics & Games
SubTopic:
RealityKit
Tags: