So I got a 3d system and some coordinates:
- Start coordinates (x, y, z) of a rocket (on the ground)
- Target coordinates (x, y, z) of the rockets target (also on the ground)
I got some initialize values like:
- maximum_velocityZ = 0.5
- maximum_resVelocityXY = 0.3
- gravity factor = 9.81
How can I calculate the flight velocitys (velocityX, velocityY and velocityZ)
for every update frame?
let maximum_velocityZ = 0.5
let maximum_resVelocityXY = 0.3
let gravity_factor = 9.81
let rocketPosition = {
x: 3,
y: 0,
z: 2
}
let rocketTarget = {
x: 7,
y: 5,
z: 8
}
let rocketVelocity = {
x: 0,
y: 0,
z: 0
}
let update = function() {
rocketPosition.x += rocketVelocity.x
rocketPosition.y += rocketVelocity.y
rocketPosition.z += rocketVelocity.z
let distanceX = (rocketTarget.x - rocketPosition.x)
let distanceY = (rocketTarget.y - rocketPosition.y)
let distanceZ = (rocketTarget.z - rocketPosition.z)
let factorXY = Math.abs(distanceX / distanceY)
rocketVelocity.x = maximum_resVelocityXY / Math.sqrt((1 / factorXY ** 2) + 1) * (distanceX > 0 ? 1 : -1)
rocketVelocity.y = maximum_resVelocityXY / Math.sqrt((factorXY ** 2) + 1) * (distanceY > 0 ? 1 : -1)
rocketVelocity.z = maximum_velocityZ * distanceZ;
rocketVelocity.z /= gravity_factor;
console.log("x:", Math.round(rocketPosition.x), "y:", Math.round(rocketPosition.y), "z:", Math.round(rocketPosition.z))
}
setInterval(update, 300)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…