# [Solved] Calculate exact frames/sec for a set distance

Get help using Construct 2

### » Sun Mar 08, 2015 8:06 pm

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.
B
14
S
4
Posts: 300
Reputation: 1,643

### » Sun Mar 08, 2015 8:37 pm

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

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)`
Moderator
B
114
S
41
G
20
Posts: 1,586
Reputation: 19,161

### » Sun Mar 08, 2015 8:51 pm

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?

C2CalculatingAnimationSpeedOnMissile.PNG
You do not have the required permissions to view the files attached to this post.
B
14
S
4
Posts: 300
Reputation: 1,643

### » Sun Mar 08, 2015 8:58 pm

Removed the acceleration value and it's much better. Still testing.

Thank you very, very much for the code!
B
14
S
4
Posts: 300
Reputation: 1,643

### » Sun Mar 08, 2015 9:01 pm

@linkman2004 Still arriving several frames early in the animation run. Do I have an error in the code above? It looks like I copied what you posted, but maybe I'm missing something.
B
14
S
4
Posts: 300
Reputation: 1,643

### » Sun Mar 08, 2015 9:09 pm

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.

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.
Moderator
B
114
S
41
G
20
Posts: 1,586
Reputation: 19,161

### » Sun Mar 08, 2015 10:42 pm

@linkman2004 It's working pretty well. Thank you, thank you, thank you for the code and help!
B
14
S
4
Posts: 300
Reputation: 1,643