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.

[Resolved] Math problem with speed and angles

Status
Not open for further replies.
Im having a little trouble with this script, it works fine and the angles are all good except that the bullet that the script fires is slower at 45 degree angles. When the angle gets closer to 90 degrees or 0 degrees the bullet goes WAY too fast. Im sure theres an easy mathematical solution but im not sure what it is, anyone have an idea? (Im aware of the other problems with this script such as firing from x = 0 or y = 0, ill fix them later, all I need is a solution to this problem and no other)

(Relevent code and entire project is here)
Code:
angle = (Math.atan2( ((Mouse.pixels[1]-240)*-1), (Mouse.pixels[0]-320) ))*57.3065
bullet.x_speed = (@bulletspeed / Math.sin(angle/57.3065)) #Set the x speed
bullet.y_speed = (@bulletspeed / Math.cos(angle/57.3065)) #set the y speed


-Charles-
 

arev

Sponsor

I modified your methods a bit. Here are the "Bullet" and "Game_Weapon" classes:

Code:
#==============================================================================
# ** Game_Weapon
#------------------------------------------------------------------------------
#  This script controls the gun operations.
#==============================================================================
class Bullet
  attr_accessor :x,:y,:x_speed,:y_speed,:need_delete
  def initialize
    #Your Stuff just copied
    @x = 320
    @y = 240 
    @x_speed = 10.00 
    @y_speed = 10.00
    @sprite = Sprite.new
    @sprite.bitmap = RPG::Cache.picture("bullet.PNG")
    @need_delete = false
  end
  #-----------------------------------------------------------------------------
  #Moves the Bullet
  def update
    return if @sprite.disposed?
    #if @x_speed <= 0 and @y_speed >=0
      @x += @x_speed
      @y += @y_speed
    #elsif @x_speed >= 0 and @y_speed <=0
    #  @x -= @x_speed
    #  @y += @y_speed
    #else
    #  @x += @x_speed
    #  @y -= @y_speed
    #end
    @sprite.x = @x
    @sprite.y = @y
    #checks if outside the screen
    if @sprite.x < 0 || @sprite.x > 640 || @sprite.y < 0 || @sprite.y > 480
      @need_delete = true
      #Removes sprite
      @sprite.dispose if !@sprite.disposed?
    end
  end
end
#==============================================================================
class Game_Weapon
  #-----------------------------------------------------------------------------
  def initialize
    @bullets = []
    @bulletspeed = 3 #Set this to your own value
  end
  #-----------------------------------------------------------------------------
  def trigger
    if Mouse.click?(1)
      if ( @bullets.size < 300) #Check if you haven't used the 30 bullets
        bullet = Bullet.new
        angle = Math.atan2( (Mouse.pixels[1]-240), (Mouse.pixels[0]-320) )
        bullet.x_speed *= Math.cos(angle) #Set the x speed
        bullet.y_speed *= Math.sin(angle) #set the y speed
        
      #  print Math.sin(angle/57.3065), "  ",Math.cos(angle/57.3065)
        #Adds the new Bullet
        @bullets.push(bullet)
      end
    end
  end
  #-----------------------------------------------------------------------------
  #UPDATE ALL BULLETS
  def update
    #Runs through all the bullets and Updates them
    for i in 0...@bullets.size
      @bullets[i].update
      #IF outside the screen
      if @bullets[i].need_delete
        @bullets.delete(@bullet)
      end
    end
  end
end
 
Status
Not open for further replies.

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