# calculating the trajectory of an object in SpriteKit when an impulse is applied to it

I want to make a trajectory line of little balls of where the object is going to go after an impulse, sort of like what is in Angry Birds. I did some research and it seems that the physics in spriteKit are calculated just as in real life with si units and other stuff. Using the displacement formula ∆x = Vi∆t + 1/2a∆t^2 i used this formula to set the position of nine balls of where the object will be after the impulse.

This is what I have tried so far:

func calculateTrajectory(mass: CGFloat, force1: CGFloat, force2: CGFloat){

for i in 0...8{

var x = CGFloat()
var y = CGFloat()

let a1 = CGFloat(force1/mass)//I am taking the force applied to the object and calculating the acceleration from that
let a2 = CGFloat((force1/mass) - 10)//I am subtracting 10 for y because of the -10m/s/s of gravity
let t = CGFloat((i/16)^2)//I am taking the number that the ball is in the line and dividing it by 16 so the line can show where the object will be from 0-0.5 secnds

x = 0.5 * a1 * t
y = 0.5 * a2 * t

trajectory.copiedNodes[i].position = CGPoint(x:ball.position.x + x, y: ball.position.y + y)

}

}


I also use this in touches moved to do a drag back sling shot sort of thing.

calculateTrajectory(mass: (ball.physicsBody?.mass)!, force1: startPositionDrag.x - movedLocation.x, force2: startPositionDrag.y - movedLocation.y)


The force for calculation is the same as impulse that I will use to shoot the ball in touches ended:

ball.physicsBody?.applyImpulse(CGVector(dx:startPositionDrag.x - endPositionDrag.x, dy:startPositionDrag.y - endPositionDrag.y ))


When I calculate this the balls are not close to where the object goes, and when I make the time change for the balls a shorter time amount by dividing i by more, the balls get farther apart even though I am calculating their positions after less time. Is there any way I am doing this wrong? Are my conversions to CGFloat wrong? Please help me.

• well, I know nothing of this coding, but one thing you may have done wrong is that indeed you are calculating the position after less time, but therefor they move faster. The rate of generation is the same no matter what, – FreezePhoenix Jan 27 '18 at 22:23
• no, acceleration over less time leads to less velocity which leads to less distance – JoeBob123 Jan 27 '18 at 22:31
• distance over less time, i believe. @JoeBobe123 – FreezePhoenix Jan 27 '18 at 22:33
• Something that might be useful: developer.apple.com/documentation/spritekit/skphysicsbody – FreezePhoenix Jan 28 '18 at 0:24
• – FreezePhoenix Jan 28 '18 at 0:25