player position

Hi everyone,
 
Ok so, I know I'm close because the icon appears close to where it should be, but when I move I can tell it is not right. I am trying to draw an Icon just above the players head along with some small text on the game map. The problem is I cant find the players x and y values in comparison to the screen.
 
Here is what I am working with now.

Code:
    x = $game_player.screen_x - $game_player.x    y = $game_player.screen_y - $game_player.y - 96
I imagine if you want it centered above the PC's head, you'd have to add the screen_x, half the player's bitmap width, and take away half the icon's width, for x; for y, it'd be screen_y minus the icon's height.


$game_player.x and y are the tile they're on, which has nothing to do with the screen.
What if I'm trying to also trying to center the text off of that position. then how would I determine the x and y's?

 x = ($game_player.screen_x - (character_bitmap_width / 2)) - (Graphics.width / 2)Thanks Shaz!
Would it be possible if you just set...

Spoiler
x = $game_player.x
y = $game_player.y - 3
or

Spoiler
x = $game_player.x * 32y = ($game_player.y - 3) * 32
Which one would be correct?
The bottom one would be the correct way to figure out the x and y values. Like Shaz said though screen_x and screen_y would be the right ones to use, seeing as how they do that for you. That $game_player.x and $game_player.y gives you the map grid x and y, and each square is 32.
Does it work when you walk along the corner or edge of the map?
No, not Graphics.width - I mean the width of the player sprite, not the screen. If your characters are 32x32 by default, and your icons 24x24, it would be this:

x = $game_player.screen_x + 4y = $game_player.screen_y - 24where 4 is the result of🙁character bitmap width (32) - icon width (24)) / 2 (because you want it centered)

If your player sprites could be larger than that, you'd need to set up some methods in Game_CharacterBase to return the width and height of the sprite, following the same logic that's used in Sprite_Character's set_character_bitmap method to calculate @cw and @ch. Then the formula would be (assuming your new methods are Game_CharacterBase.width and Game_CharacterBase.height):

Code:
x = $game_player.screen_x + ($game_player.width - 24) / 2y = $game_player.screen_y - 24
(so really you'd only need a method for the width, not the height)
Oh sorry I should have clarified.

x = ($game_player.screen_x - (character_bitmap_width / 2)) - (Graphics.width / 2)is for centering the text with a centered alignment not the icons.

This is the bit of code for centering the icon and text

    x = ($game_player.screen_x - (character_bitmap_width / 2)) - 4    y = $game_player.screen_y - 96    draw_icon(x, y, reward[0], reward[2])    draw_sizable_text((x - (Graphics.width / 2)) + 10, y - 170, Graphics.width, Graphics.height, string, 1, 14)This is the method I created to get the with

  #--------------------------------------------------------------------------  # * Returns Character Graphic Dimensions  #--------------------------------------------------------------------------  def character_bitmap_width    character_name = $game_party.members[0].character_name    bitmap = Cache.character(character_name)    sign = character_name[/^[\!\$]./]    if sign && sign.include?('$')      cw = bitmap.width / 3    else      cw = bitmap.width / 12    end    return cw  endI guess I should do this for the height too though, in the case that the sprite is tall too.

  #--------------------------------------------------------------------------  # * Returns Character Graphic Dimensions  #--------------------------------------------------------------------------  def character_bitmap_dimensions    character_name = $game_party.members[0].character_name    bitmap = Cache.character(character_name)    sign = character_name[/^[\!\$]./]    if sign && sign.include?('$')      cw = bitmap.width / 3      ch = bitmap.height / 4    else      cw = bitmap.width / 12      cw = bitmap.height / 8    end    return [cw, ch]  endThanks again. This post can be closed.

Actually While I'm on the topic maybe someone can help me out. I've noticed that I always have to add or minus 10 when centering text to the screen.

Does anyone know the reason for this?
No, you don't need to do it for the height. screen_x, screen_y is the upper left corner of the sprite, not the base. It does NOT coordinate with a tile on the grid.


When looking at the grid tile occupied by the character, you look at the BASE of the sprite. But screen_y is the TOP of the sprite. So you don't have to subtract the sprite's height. Your - 96 will put the icon 3 tiles above the top of the sprite. I don't think that's what you want.
Actually I did need too, to fit the text and Icon.

x3wq.png


The icon can probably come down a little and more 1 or 2 px left, but this is what it looks like with the dimensions I mentioned above.
ah, okay - didn't realize you wanted icon and text one above the other.


Alrighty, then ... I guess This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/player-position.20849/