I'm trying to calculate the Angle of reach to hit a target in a scenekit scene. My maths ability is about 3/Potato, but after a few hours on Khan Academy, I think I've managed to tease out the logic of this "Angle of Reach" formula from wikipedia, (Which I stumbled across in this Stack Exchange answer)...
I created a function to returns the SCNVector3
to apply a force to a sphere with a given mass, to hit a target.
As I understand it, the ±
in the formula means there are two possible angles, that I'll call angleA
and angleB
which use plus and minus respectively.
The function I came up with is close, but there are two problems.
angleB
is sometimes a teeny tiny bit off the mark.angleA
always falls short.
I can live with angleB
being a little bit off sometimes. I'm putting that down to SceneKit's physics engine not being deterministic. Someone please tell me if that's not correct.
But I really want to know what's going wrong with angleA
. AngleA
should provide the larger angle to produce the tall, sweeping, parabolic trajectory I'm looking for.
Here's the function in full.
func launchVectorFor(
target: SCNVector3,
fromPosition origin: SCNVector3,
withProjectileMass mass: Float) -> SCNVector3 {
let g: Float = -1 * (scene?.physicsWorld.gravity.y)!; //m/s
// Distance (Displacemet)
let Dx: Float = (target.x - origin.x)
let Dy: Float = (target.y - origin.y)
// Velocity
// I'm just fudging a Velocity here, using the distance
// between he origin and the target
let V: Float= sqrt(Dx * Dx + Dy * Dy)
// Useful powers
let V2: Float = pow(V, 2)
let V4: Float = pow(V, 4)
let Dx2 = pow(Dx, 2)
// All the math
let sqrtInput: Float = V4 - ( g * ( g * Dx2 + 2 * Dy * V2))
let numeratorPlus: Float = V2 + sqrt(sqrtInput)
let numeratorMinus: Float = V2 - sqrt(sqrtInput)
let angleInRadiansA = atan(numeratorPlus / ( g * Dx ) )
let angleInRadiansB = atan(numeratorMinus / ( g * Dx ) )
let angleA = angleInRadiansA * (Float(180) / Float.pi)
let angleB = angleInRadiansB * (Float(180) / Float.pi)
print("solutions A: \(angleA), -- B \(angleB)")
// Get X & Y components of force * angle * mass
let Fx = (V * cos(angleInRadiansA)) * mass
let Fy = (V * sin(angleInRadiansA)) * mass
let multiplier: Float = Dx > 0 ? 1 : -1
return SCNVector3(Fx * multiplier, Fy * multiplier, 0)
}
And here's the code that goes into calling my function, in case it's of any relevance...
func handleTap(_ gestureRecognize: UIGestureRecognizer) {
let launchPosition = convertTouchToWorldCoords(location: gestureRecognize.location(in: self.view))
let target: SCNNode = (scene?.rootNode.childNode(withName: "target", recursively: false))!
let ball: SCNNode = createBall(atLocation: launchPosition)
scene?.rootNode.addChildNode(ball)
let launchVector = launchVectorFor(target: target.position, fromPosition: launchPosition, withProjectileMass: 0.5)
ball.physicsBody?.applyForce(launchVector, asImpulse: true)
print(launchVector)
}
func createBall(atLocation location: SCNVector3) -> SCNNode {
let sphere = SCNSphere(radius: 0.5)
let shape = SCNPhysicsShape(geometry: sphere, options: [:])
let ball = SCNNode(geometry: sphere);
let pBody = SCNPhysicsBody(type: .dynamic, shape: shape)
pBody.restitution = 0.95
pBody.mass = 0.5
ball.physicsBody = pBody
ball.position = location
ball.castsShadow = true
return ball;
}
func convertTouchToWorldCoords(location: CGPoint) -> SCNVector3 {
let hits = scnView?.hitTest(location, options: nil)
if let hitResult = hits?.first {
return hitResult.worldCoordinates
}
return SCNVector3Zero
}
Any help much appreciated