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.

Player Position Relative to a Specific X and Y

Okay, since the title's fucked up again, lemme picture it...

http://img262.imageshack.us/img262/5021/schemern7.jpg[/img]

I try to make the player character look into the direction of a specific object. Picture [1] shows what I have with the code in the following spoiler: The player changes his view according to the position of the respective object, but in a way that'd work for isometric movement; for example, if the object is in the lower-right part of the screen, the player looks down.

Code:
  def turn_towards_object
    if @object.screen_x >= self.screen_x
      if @object.screen_y >= self.screen_y
        turn_down
      else
        turn_right
      end
    else
      if @object.screen_y >= self.screen_y
        turn_up
      else
        turn_left
      end
    end
  end

Picture [2] shows what I want to get: If the object is 'more in a specific direction than in another' (worst explanation ever, but the picture should do that job), the player should look there.

Thanks in advance.
 
Change your code for this one:
Code:
  def turn_towards_object
    angle = Math.atan2(-(@object.screen_y - self.screen_y),
      @object.screen_x - self.screen_x) * 180 / Math::PI
    if (angle >=-135) && (angle <-45)
      turn_down
    else
      if (angle >=-45) && (angle <45)
        turn_right
      else
        if (angle >=45) && (angle <135)
          turn_up
        else
          if (angle >=135) || (angle < -135)
            turn_left
          end
        end
      end
    end
  end

I don't know how you want to look your PJ if the object it's exactly in a diagonal. To change that, change the >= for > and < for <=.

Final note:
If the PJ looks down when it should look up and viceversa, change the second line to:
angle = Math.atan2((@object.screen_y - self.screen_y),
 
The one inbetween your code tags works like a charm, but one last question (I'm not the newest guy to RGSS, but those math stuff is really my weak point):
Can I somehow include an if-statement to only trigger that if the player is about 64px away from @object... just in case you wonder, @object has a pixel-based movement ;) I tried to ebrace the whole code above (without the def-line, of course ;) ) with this conditional:
Code:
  def turn_towards_cursor_position
    x_value = (@object.screen_x - self.screen_x).abs
    y_value = (@object.screen_y - self.screen_y).abs
    if x_value > 64 and y_value > 64
      # ...
    end
  end
Didn't work because it scans the whole horizontal and vertical area, and I kinda want only a circle area... well, a rectangular would do the job as well.

Also, thanks up to this point, of course ^^
 

Zeriab

Sponsor

Certainly you can.
Just use this, and you don't even have to care about using the absolute values.
Code:
if x_value*x_value + y_value*y_value > 64*64
  # ...
end

I have not tested it, but this might do the trick:
Code:
  def turn_towards_cursor_position
    x_value = @object.screen_x - self.screen_x
    y_value = @object.screen_y - self.screen_y
    if x_value*x_value + y_value*y_value > 64*64
      if x_value > 0
        if y_value > x_value
          turn_down
        elsif y_value < -x_value
          turn_up
        else
          turn_right
        end
      else
        if y_value > -x_value
          turn_down
        elsif y_value < x_value
          turn_up
        else
          turn_left
        end
      end
    end
  end

The turns may be inverted, though I am sure you know what to do in that case ^_^
 

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