- #1
mariano_donat
- 8
- 0
I'm creating a tennis game and at this point I'm trying to model ball motion. I found multiple lectures on projectiles equations of motion which seem to apply well for when ball is not moving, but they do not take into account the velocity may have at the time of impact. For example, I found a function that gives me the angle of launch so ball can impact a target point at a given speed. If the ball is not moving, it works well, however if it is, it yields incorrect results. So how would one go about plugging current velocity in projectile equations of motion? For reference, this is how the function to calculate the angle looks like:
Code:
func launchAngle(speed: Float, distance: Float, yOffset: Float, gravity: Float, angle0: inout Float, angle1: inout Float) -> Bool {
angle0 = 0
angle1 = 0
let speedSquared = speed * speed
let operandA = powf(speed, 4)
let operandB = gravity * (gravity * (distance * distance) + (2 * yOffset * speedSquared))
if operandB > operandA {
return false
}
let root = sqrt(operandA - operandB)
angle0 = atan((speedSquared + root) / (gravity * distance))
angle1 = atan((speedSquared - root) / (gravity * distance))
return true
}