7 posts
• Page **1** of **1**

I have a projectile animation with 36 frames. As it moves from shooter to target it appears to rise and then fall. What is the calculation needed so that the animation starts at frame 0 from shooter and then finishes on frame 35 at the target? So it runs one full cycle.

Last edited by locohost on Mon Mar 09, 2015 11:26 am, edited 1 time in total.

**Posts:**280**Reputation:**1,578

B

13
S

4
The following equation should get you the exact amount of time taken for the projectile to reach its destination.

Where:

- y_0 is the starting y coordinate of the projectile

- y_1 is the y coordinate of the projectile's target

- g is the acceleration of gravity in pixels per second

- theta is the angle between the ground and the initial direction of the projectile

- speed is the initial speed of the projectile

The following equation should get you the required animation speed of the projectile.

This will work for a side view where the projectile start and end positions lie at separate Y coordinates. If you're talking about a top down view, the following(much simpler) equation should get you what you need.

- Code: Select all
`timeTaken = abs((sqrt(2 * y_1 * g - 2 * y_0 * g + (sin(theta) * speed)^2) - sin(theta) * speed) / g)`

Where:

- y_0 is the starting y coordinate of the projectile

- y_1 is the y coordinate of the projectile's target

- g is the acceleration of gravity in pixels per second

- theta is the angle between the ground and the initial direction of the projectile

- speed is the initial speed of the projectile

The following equation should get you the required animation speed of the projectile.

- Code: Select all
`animationSpeed = numberOfFrames / timeTaken`

This will work for a side view where the projectile start and end positions lie at separate Y coordinates. If you're talking about a top down view, the following(much simpler) equation should get you what you need.

- Code: Select all
`animationSpeed = numberOfFrames / (distance(projectileStartX, projectileStartY, projectileEndX, projectileEndY) / projectileSpeed)`

**Posts:**1,586**Reputation:**19,116

Moderator

B

113
S

41
G

20
Yes it is a top down game.

I tried the last calculation you provided. It appears the animation only gets about halfway through the 36 frames when it hits the target Token. Here is my early version shooting code. Do you see an error in the calculation? Or did I use the wrong one?

I tried the last calculation you provided. It appears the animation only gets about halfway through the 36 frames when it hits the target Token. Here is my early version shooting code. Do you see an error in the calculation? Or did I use the wrong one?

You do not have the required permissions to view the files attached to this post.

**Posts:**280**Reputation:**1,578

B

13
S

4
My equation assumes the projectile is moving at a constant velocity, but your bullet is accelerating. The following updated equation should get you what you need.

Your code looks fine as far as implementing the equation I gave you. You should check your animation and make sure each frame has a speed of 1, as higher or lower values could throw things off. It could additionally be an issue of timing differences. I've given you mathematically exact equations, but simulations like these aren't necessarily mathematically ideal.

- Code: Select all
`timeTaken = abs( (sqrt(2 * distance(attackerX, attackerY, Token.X, Token.Y) * acceleration + initialSpeed^2) - initialSpeed) / acceleration )`

- Code: Select all
`animationSpeed = numberOfFrames / timeTaken`

Your code looks fine as far as implementing the equation I gave you. You should check your animation and make sure each frame has a speed of 1, as higher or lower values could throw things off. It could additionally be an issue of timing differences. I've given you mathematically exact equations, but simulations like these aren't necessarily mathematically ideal.

**Posts:**1,586**Reputation:**19,116

Moderator

B

113
S

41
G

20
**Posts:**280**Reputation:**1,578

B

13
S

4
7 posts
• Page **1** of **1**

## Who is online |

Users browsing this forum: No registered users and 26 guests |