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.

Need some help with Trig via RGSS

Ok, so I am working on getting my TBS updated, but I have run into a quick block.  The problem is that when you are attacked from behind it should recalculate damage and hit percentages slightly different, but what if they are behind you and to the side(long range) it should also that into consideration.  I just need a simple method to check if the attacker is "behind" them using the angle from which they are to the player.  To my understanding of Trig I know 2 sides and 1 angle, I just need to figure out the angle opposite.
Code:
      /|
     / |
    /  |
   /   |5
  /    |
a/_____|  < obviously a 90* angle.
   2
What I need to find is a formula that will help me figure this out, can someone help me out with this?  I am not the best at trig and so I am getting lost.

Here is what I have so far.
Code:
def from_back?(attacker)
    back = false
    case self.direction
    when 2 #down
      if (attacker.y < self.y) #if attacker y is less than 
        angle = Math.tan((attacker.x - self.x)/(attacker.y - self.y))
        p angle 
      end
    when 4
    when 6
    when 8
      if (attacker.y > self.y) 
        back = true
      end
    end
    return back
  end

Well any help would be appreciated, otherwise this function will be pretty dumb. 
I was hopeing to say that if the angle returned was like a \/ (using a 90* angle) from behind them, it would be considered from_back, while outside of that, again check if from side.. being > < from the character, else from front.

Thanks again
 
by what you're showing me:

tan a = 5/2

which means

a = arctan 5/2

arctan is the inverse function of tan

hope that helps ;P

PS: I dunno if there's a way to represent the arctan of an angle in RGSS .-.
 
Ruby uses Radians for Trig, so you'll have to do a conversion (radians * 180 / PI = degrees).

However, instead of using trig, you could use the character's facing direction to get the opposite side. (10 - current_dir)  It might be a lot easier for you to do.
 
Well, I think you have given me what I need, I will figure the rest out.  The kicker was I was doing 2/5 instead of 5/2, which tends to make a different in the calculation. 

Here is what I ended up with...
Code:
angle = Math.to_d(Math.atan((attacker.y - self.y)/(attacker.x - self.x.abs)))
if angle < 45 and angle > (-45)
  back = true
end
because the to_d isnt a actual command, I created it..
Code:
module Math
  def self.to_d(rad)
     return rad * 180/Math::PI
  end
end

Thanks alot guys, you were most helpful!
 

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