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

0 favourites
  • 7 posts
From the Asset Store
A whole set you need to create a gorgeous winter 2d game
  • 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.

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

    timeTaken = abs((sqrt(2 * y_1 * g - 2 * y_0 * g + (sin(theta) * speed)^2) - sin(theta) * speed) / g)[/code:4pq98oc2]
    Where:
    [ul]
    	[li]y_0 is the starting y coordinate of the projectile[/li]
    	[li]y_1 is the y coordinate of the projectile's target[/li]
    	[li]g is the acceleration of gravity in pixels per second[/li]
    	[li]theta is the angle between the ground and the initial direction of the projectile[/li]
    	[li]speed is the initial speed of the projectile[/li]
    [/ul]
    The following equation should get you the required animation speed of the projectile.
    
    [code:4pq98oc2]animationSpeed = numberOfFrames / timeTaken[/code:4pq98oc2]
    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:4pq98oc2]animationSpeed = numberOfFrames / (distance(projectileStartX, projectileStartY, projectileEndX, projectileEndY) / projectileSpeed)[/code:4pq98oc2]
  • 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?

    [attachment=0:tw3duf1e][/attachment:tw3duf1e]

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

    Thank you very, very much for the code!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

  • 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.

    timeTaken = abs( (sqrt(2 * distance(attackerX, attackerY, Token.X, Token.Y) * acceleration + initialSpeed^2) - initialSpeed) / acceleration )[/code:3hyecwtn]
    [code:3hyecwtn]animationSpeed = numberOfFrames / timeTaken[/code:3hyecwtn]
    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.
  • linkman2004 It's working pretty well. Thank you, thank you, thank you for the code and help!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)