- Куб вращается с угловой скоростью, пропорциональной жесту перетаскивания.
- Куб постепенно замедляется (угловое демпфирование), пока не останавливается.
- Затем куб отскакивает назад либо лицом вперед (угол 0), либо назад (угол π), в зависимости от того, что ближе.
- При отскоке назад он слегка выскакивает и колеблется перед стабилизацией.
В теле моего представления SwiftUI у меня есть RealityView который содержит:
Код: Выделить всё
updateSubscription = content.subscribe(to: SceneEvents.Update.self) { event in
if isDragging {
return // Pause spring behaviour whilst dragging
}
applySpringTorque(to: cube)
}
Код: Выделить всё
private func applySpringTorque(to entity: ModelEntity) {
guard let body = entity.components[PhysicsBodyComponent.self],
body.mode == .dynamic else { return }
guard var motion = entity.components[PhysicsMotionComponent.self] else { return }
let currentRotation = entity.orientation
let angle = currentRotation.angle
if angle.isNaN { return }
// Determine the axis of rotation (normalised)
var axis = currentRotation.axis
if simd_length(axis) < 1e-5 || !axis.x.isFinite {
// If the axis is too small or invalid, default to y-axis rotation
axis = [0, 1, 0]
} else {
axis = simd_normalize(axis)
}
let angularVelocity = motion.angularVelocity
let angularSpeed = simd_length(angularVelocity)
// Stop motion completely if it is slow enough and near the rest position
if angularSpeed < 0.1 && abs(angle) < 1e-3 {
motion.angularVelocity = [0, 0, 0]
entity.components.set(motion)
return
}
let restoringTorque = -springStrength * angle * axis
let dampingTorque = -dampingCoefficient * angularVelocity
let totalTorque = restoringTorque + dampingTorque
motion.angularVelocity += totalTorque * 0.015
entity.components.set(motion)
}
Как мне адаптировать этот код так, чтобы куб подпрыгивал либо к 0, либо к π в зависимости от того, какая ориентация ближе? Если кто-нибудь может объяснить правильный способ реализации такой системы или указать на хорошие ссылки из Unity, Unreal или физического моделирования с похожим поведением, это будет очень признательно.
Подробнее здесь: https://stackoverflow.com/questions/798 ... pending-on
Мобильная версия