我一直试图建立一个基于RealityKit的Billiard游戏,但是物理模拟还不够精确。当参数太小时,RealityKit似乎会产生粗略的计算结果。
下面的代码介绍了我到目前为止所做的工作。有办法配置RealityKit并提高物理精度吗?
import RealityKit
import ARKit
let ballMaterial = PhysicsMaterialResource.generate(friction: 0.055, restitution: 0.95)
game.allBalls.forEach { entity in
guard let physicsEntity = entity as? HasPhysics else { return }
physicsEntity.physicsBody?.massProperties.mass = 0.17
physicsEntity.physicsBody?.material = ballMaterial
}
let wallMaterial = PhysicsMaterialResource.generate(friction: 0.005, restitution: 0.5)
game.wallEntities.forEach { entity in
guard let physicsEntity = entity as? HasPhysics else { return }
physicsEntity.physicsBody?.material = wallMaterial
}
let tableSurfaceMatrial = PhysicsMaterialResource.generate(staticFriction: 0.001, dynamicFriction: 0.2, restitution: 0.25)
game.tableSurfaceEntities.forEach { entity in
guard let physicsEntity = entity as? HasPhysics else { return }
physicsEntity.physicsBody?.material = tableSurfaceMatrial
}增加移动白色球的力量:
// ballWhite as? Entity & HasPhysics
game.ballWhite.physicsBody?.mode = .dynamic
game.ballWhite.addForce(rayForce, relativeTo: nil)发布于 2021-12-26 16:07:00
如果将restitution参数设置为大于1的值,则RealityKit物理引擎开始产生更逼真的弹跳模拟,但在动态效应衰减时仍然不够真实--苹果工程师必须更加努力地在RealityKit 3.0中实现健壮的物理模拟。
我用了一个简单的真实作曲场景,里面有一个半径为0.1米的球。
import AppKit
import RealityKit
class GameViewController: NSViewController {
@IBOutlet var arView: ARView!
override func awakeFromNib() {
arView.environment.background = .color(.black)
let ballScene = try! Experience.loadBall()
let ball = ballScene.sphere!.children[0] as! ModelEntity
ball.components[CollisionComponent.self] =
CollisionComponent(shapes: [.generateSphere(radius: 0.1)],
mode: .default,
filter: .sensor)
ball.components[PhysicsBodyComponent.self] =
PhysicsBodyComponent(shapes: [.generateSphere(radius: 0.1)],
mass: 0.05,
material: .generate(friction: 0.9,
restitution: 1.35))
arView.scene.anchors.append(ballScene)
}
}

https://stackoverflow.com/questions/70487697
复制相似问题