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.

Sprite Floating Point Woes

khmp

Sponsor

I've been trying at this for a little bit but I can't get it. I'm trying to make a sprite work with floating points so I don't need to externally keep track of where the Sprite should be. Like if I try moving a Sprite +2.5 pixels in the x and +1.5 pixels in the y. It truncates the number and the path is wrong over time. And if you set the amount of movement per frame to a number less than zero it remains stationary. 0.5 becomes 0, heck 0.9 becomes 0. Anyway thats the reasoning behind this little object.

The problem is that either I'm screwing up the way I'm doing aliasing because where ever I tell the sprite to go gets ignored, or I'm not understanding what little I do about the Sprite object. In any case I made a nice little test script for anyone that might have any ideas on how to solve this.

Code:
#==============================================================================
# ** Sprite_Float < Sprite
#------------------------------------------------------------------------------
#  A sprite whose coordinates are Floating point instead of Fixnum. So movement
#  can be based on floating point math. No more 1.6 = 1 all the time nonsense.
#==============================================================================

Debug_Output = true

class Sprite_Float < Sprite
  #--------------------------------------------------------------------------
  # * Alias Methods
  #--------------------------------------------------------------------------
  alias_method :khmp_sprite_float_x=, :x=
  alias_method :khmp_sprite_float_y=, :y=
  alias_method :khmp_sprite_float_z=, :z=
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super()
    @fx = @fy = @fz = 0.0
  end
  #--------------------------------------------------------------------------
  # * x=
  #     x : the new x coordinate
  #--------------------------------------------------------------------------
  def x=(x)
    p "Sprite_Float.x= has been called. x = " + x.to_s if Debug_Output
    khmp_sprite_float_x=(@fx = x.to_f)
  end
  #--------------------------------------------------------------------------
  # * y=
  #     y : the new y coordinate
  #--------------------------------------------------------------------------
  def y=(y)
    p "Sprite_Float.y= has been called. y = " + y.to_s if Debug_Output
    khmp_sprite_float_y=(@fy = y.to_f)
  end
  #--------------------------------------------------------------------------
  # * z=
  #     z : the new z coordinate
  #--------------------------------------------------------------------------
  def z=(z)
    p "Sprite_Float.z= has been called. z = " + z.to_s if Debug_Output
    khmp_sprite_float_z=(@fz = z.to_f)
  end
  #--------------------------------------------------------------------------
  # * x !OVERRIDE PARENT!
  #--------------------------------------------------------------------------
  def x
    return @fx
  end
  #--------------------------------------------------------------------------
  # * y !OVERRIDE PARENT!
  #--------------------------------------------------------------------------
  def y
    return @fy
  end
  #--------------------------------------------------------------------------
  # * z !OVERRIDE PARENT!
  #--------------------------------------------------------------------------
  def z
    return @fz
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    super()
  end
end

#--------------------------------------------------------------------------
# Enable_Sprite_Test:
#   True to "see" Sprite_Float
#   False to see a regular Sprite instead
#--------------------------------------------------------------------------
Enable_Sprite_Test = true

class Scene_Title
  alias_method :khmp_sprite_float_main, :main
  def main
    Enable_Sprite_Test ? @test = Sprite_Float.new : @test = Sprite.new
    @test.bitmap = Bitmap.new(100, 100)
    @test.bitmap.fill_rect(0, 0, 100, 100, Color.new(0, 0, 0))
    @test.x = 80
    @test.y = 50.0
    @test.z = 100
    
    p "@test.x = " + @test.x.to_s if Debug_Output
    p "@test.y = " + @test.y.to_s if Debug_Output
    p "@test.z = " + @test.z.to_s if Debug_Output
    khmp_sprite_float_main
    
    @test.bitmap.dispose
    @test.dispose
  end
end

Thanks for taking the time to read this. :thumb:
 
Hey there,
you're calling your alias wrong, instead of calling the alias you are creating a local variable and setting it. Add 'self.' before the alias. And I like the idea by the way but don't think that z needs to be floating point as well as x and y.
 

khmp

Sponsor

Thanks for the reply and it works. So it was setting, let's take x for example, it was setting khmp_sprite_float_x to whatever @fx was equal to? Aw man that makes sense now. The importance of self. I did the z axis for the sake of continuity.

Thanks again verballydecapitating! :thumb: Now I can put that vector math to work.
 

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