1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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, \$\endgroup\$ – FreezePhoenix Jan 27 '18 at 22:23
  • \$\begingroup\$ no, acceleration over less time leads to less velocity which leads to less distance \$\endgroup\$ – JoeBob123 Jan 27 '18 at 22:31
  • \$\begingroup\$ distance over less time, i believe. @JoeBobe123 \$\endgroup\$ – FreezePhoenix Jan 27 '18 at 22:33
  • \$\begingroup\$ Something that might be useful: developer.apple.com/documentation/spritekit/skphysicsbody \$\endgroup\$ – FreezePhoenix Jan 28 '18 at 0:24
  • \$\begingroup\$ and: developer.apple.com/documentation/spritekit \$\endgroup\$ – FreezePhoenix Jan 28 '18 at 0:25
0
\$\begingroup\$

A much more straightforward solution is simply to make a hidden or transparent copy of the projectile, remove collision masks, and launch it. You can either leave a trail of small sprite nodes at various points in its lifespan, or you could utilize a particle emitter so it leaves a trail of short-life particles in its wake. Finally, remove the projectile and trail if the user changes angles, cancels the launch, or launches the real projectile.

No math, no random variables you didn't consider, and it doesn't break if you make the scenario more complicated in the future (changing gravity, adding gravitational fields, etc). Even if you decided to add wind speed that varied moment to moment, this model would compensate if updated consistently.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.