Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Rotating Animations

Alright, here's another request that could probably be useful to many other people. What I need is rather self-explanatory, I need to be able to rotate an animation. It doesn't have to be rotating while animating, more like the whole thing is turned to begin with (every frame has the same rotation). And I'm not talking only about the "Angle" propriety of the Animations Cells, since it has to be rotated according to animation center (which will always be middle - 0,0 , not screen or anything) (so basically there's a bit of trigonometry involved). I can't really do this manually, since I'd probably need about 100 angles out of the 360.

I hope this is actually doable, since this "class Sprite_Character < RPG::Sprite" is where the animation are handled (I think?). If it is great, then I owe you in advance ^^. Also, IF possible, I'd like to be able to stop the animation before a certain frame is reached, but that is if it's not too much trouble (like Animation.call(name, frame, angle) or something).
 
Alright, I'm making some progress, but this is still not quite right.
Unfortunately, I have to go rent a car & drive to Ohio. [sarcasm]WooHoo!!![/sarcasm]

See if you can make some progress with it, and I'll take another look when I get home on Friday

Code:
module RPG
  class Sprite 
    def animation(animation, hit)
      dispose_animation
      @_animation = animation
      return if @_animation == nil
      # set the rotation based on the global variable
      if $anim_rot != nil && $anim_rot != 0
        #for each frame
        for i in 0..@_animation.frame_max - 1
          # for each cell in each frame
          for j in 0..@_animation.frames[i].cell_max - 1
            angle_orig = @_animation.frames[i].cell_data[j,4]
            @_animation.frames[i].cell_data[j,4] = (angle_orig + $anim_rot) % 360
            x_orig = @_animation.frames[i].cell_data[j,1]
            y_orig = @_animation.frames[i].cell_data[j,2]
            r = Math.hypot(x_orig, y_orig)
            # check x_orig so we don't divide by 0
            if x_orig == 0
              sa_rad = 0
              if y_orig > 0
                sa_rad = 90 * (Math::PI / 180)
              end
              if y_orig < 0
                sa_rad = -90 * (Math::PI / 180)
              end
            else
              sa_rad = Math.atan(y_orig / x_orig)
            end
            rot_rad = $anim_rot * (Math::PI / 180)
            ea_rad = sa_rad + rot_rad
            x_new = r * Math.cos(ea_rad)
            y_new = r * Math.sin(ea_rad)
            @_animation.frames[i].cell_data[j,1] = x_new
            @_animation.frames[i].cell_data[j,2] = y_new
          end
        end
      end
      @_animation_hit = hit
      @_animation_duration = @_animation.frame_max
      animation_name = @_animation.animation_name
      animation_hue = @_animation.animation_hue
      bitmap = RPG::Cache.animation(animation_name, animation_hue)
      if @@_reference_count.include?(bitmap)
        @@_reference_count[bitmap] += 1
      else
        @@_reference_count[bitmap] = 1
      end
      @_animation_sprites = []
      if @_animation.position != 3 or not @@_animations.include?(animation)
        for i in 0..15
          sprite = ::Sprite.new(self.viewport)
          sprite.bitmap = bitmap
          sprite.visible = false
          @_animation_sprites.push(sprite)
        end
        unless @@_animations.include?(animation)
          @@_animations.push(animation)
        end
      end
      update_animation
    end
  end
end

Be Well
 
Thank you, it's a very nice start! I'll just wait a little...

Every time I call that animation, the angles add up. Meaning first time it may be +43, the next time will be +86. I think the problem may be that when you set coordinates (X,Y, and angle) to the cells, it actually stays that number. Probably they should be reset to database original numbers (although I'm not quite familiar with modules at all, so...). Then, why use Math.atan? Doesn't tan works by itself, the result (being in radians) being added to the $anim_rot (in radian too)?

(I'm just speculating, don't really know. It doesn't really rotate in the right way when I continuously rotate it +10 (so after 36 button-smash I make a full twist))
 
Oh, and this :

x_new = r * Math.cos(ea_rad)
y_new = r * Math.sin(ea_rad)

Should be

x_new = x_orig + r * Math.cos(ea_rad)
y_new = y_orig + r * Math.sin(ea_rad)

maybe? Still doesn't rotate right, but it could have something to do...
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top