移動 Actors
在將 actor 添加到場景中(無論是通過編輯器放置還是在運行時生成)後,您可能想要將其移動到不同的位置或旋轉它。有兩種方法可以實現這一點:
直接在 actor 上設置新的位置和旋轉。
通過將 actor 添加到物理系統中並賦予動態或運動學物理體來模擬物理效果。
設置 Actor 的變換
如果您的物體不需要嚴格遵守物理定律,您可以簡單地使用 actor 的 position
和 rotation
屬性來設置位置和旋轉。這將更新物體的渲染變換,但不會在物理世界中更新它。要在物理世界中更新,您還需要通過物理系統應用 actor 的變換。
在下面的例子中,我們有一個 actor,它具有由碰撞形狀給出的渲染和物理表示。在每一幀中,我們根據速度和方向更新 actor 的位置。
@Actor()
class BallActor extends BaseActor {
private physics = inject(PhysicsSystem)
public speed = 0.4
public direction = new THREE.Vector3(0,0,1)
private mesh = attach(MeshComponent, {
mass: 1,
bodyType: PhysicsBodyType.static,
object: new PhysicalShapeMesh(
new SphereGeometry(.2, 20, 10),
new MeshStandardMaterial({ color: 0xeff542 }),
new SphereCollisionShape(.2))
})
onUpdate(deltaTime: number) {
this.position.addScaledVector(this.direction, this.speed * deltaTime)
// Also update the physics system so that the physics body is updated
this.physics.updateActorTransform(this)
}
}
最后更新于