0
votes

I am making a top down game where I am having cannons fire and having their projectiles move to the clicked location via the SKAction moveTo:duration. I am supposed to have wind change the trajectory so I have the cannonball implemented as an SKPhysicsBody and I am setting gravity to be a the windspeed since it is the only thing I can find that applies a constant force like wind would. The problem I am having is moveTo is probably the wrong way to be implementing the cannonball. The ball moves according to the path it should but then lands at the tapped location which is not what I want. I can't find a good alternative to moveTo. Any ideas?

2
do you want to move the cannon to a particular position? - rakeshbs
Don't use SKActions for realtime motion, instead calculate the velocity needed to travel to a particular point. This will allow your projectiles to be more dynamic. - Epic Byte
@rakeshbs I want the user to be able to click a target location and then have the wind manipulate the flight path so that the end position is different than where they clicked. The problem is wind can change mid flight so I can't just pre-compute the endpoint - TovrikTheThird
use applyImpulse: instead - Daniyar

2 Answers

0
votes

You have to solve the projectile equations to determine the force needed and angle to applyImpulse. Solving the equation is only considering the gravity force and impulse force applied to projectile using the applyImpulse with your distances known from your cannons to user's click projectile destination.

Your applyImpulse launches the projectile, you start your wind and other external forces which will have an impact on the projectile path, changing it's destination from that of user's clicked.

You need to know few physics and math to re-arrange the equations and solve them :)

0
votes

Not sure if you need to do all the advanced math, just set the angle of your cannon and apply impulse to the cannonball, and use the scene.physicsWorld.gravity as your wind, just make sure you also give your cannonball a physicsbody that has dynamic and affectedByGravity set to true.

To calculate the angle, you would do:

let angle = atan2(Double(touch.y - cannon.y),Double(touch.x - cannon.x))