I'm taking a sprite sheet and splitting it up into an array. I then take that array and rotate the image by a certain granularity and push that onto the end of the array too to pre bake the rotations for faster drawing (no hardware acceleration). The final result looks something like this:
array[0] = frame1 of the animation
array[1] = frame2 of the animation
array[2] = frame3 of the animation
array[3] = frame1 of the开发者_如何学编程 animation rotated at 45 degrees
array[4] = frame2 of the animation rotated at 45 degrees
array[5] = frame3 of the animation rotated at 45 degrees
array[6] = frame1 of the animation rotated at 90 degrees
... etc
So now I'm trying to come up with a function that'll return the right element for any angle at the right granularity. For example, say my in-game object is currently rotated 30 degrees and is on frame1 of the animation, I would want array[3]. If I was on frame2 of the animation, I would want array[4]. If the object was rotated 80 degrees and was on frame1, I would want array[6], and so on.
These are the constants I have: The # of animation frames (pre-rotation) The # of animation frames (post-rotation) The Granularity of the Rotations (from 1 to 360, ie granularity of 45 would be the above example).
This is the signature of the function to calculate the frame:
function calculateImageIndex(animationFrame : Number, currentAngle : Number) : Number
Any ideas on how to calculate this? I'm stumped.
nf: total number of frames
gr: granularity of rotation
r: actual angle of rotation
f: actual frame number
i = round(r / gr) * nf + f - 1
n = Number of frames (Pre-rotation) g = granularity of rotation r = actual angle of rotation f = actual frame
I think these are the constants you need and are given.
The first thing we will want to do it find the number of rotations it would take to get to the angle desired. In the above example getting to an angle of 45 would be 1 rotation. 90 degrees = 2 rotations and so on. Let R = number of rotations.
R = r / g This should always be an integer as you should never need an angle that doesn't fit the gratuity you used.
Next we will calculate the starting index of that rotation "group". In your example above the first group with a rotation 0 would start at index 0. 45 degress or 1 rotation would start at index 3. And so on. To do this we need to multiply the number of rotations (R) by the number of frames pre-rotation (n). Let j = the starting index of that rotation group. j = R * n
The last step would be to figure out how much you must add to the starting index (j) to reach the frame you want. I will assume the first frame will be numbered 1 as in your example above, but if the first frame is numbered 0 then remove the -1 in the algorithm. Let i = the final index. i = j + (f - 1)
I will be the index you are looking for. To put this together in one algorithm it would look like this.
i = ((r / g) * n ) + (f - 1)
Hope this helps! Let me know if you need my to clarify anything.
精彩评论