Calculating screen position for gui to be drawn over character's head

Started by MUFFINinc, Sun 22/12/2013 04:58:26

Previous topic - Next topic

MUFFINinc

Hey folks.  I'm creating a game where I want to have the mode selection interface appear as a thought bubble over the character's head when the character is clicked on.  My first idea was to do it like this:

Code: ags

gMenu.SetPosition(player.x - xOffset, player.y - yOffset);


gMenu is the name of the GUI I want drawn.  xOffset and yOffset are arbitrarily defined values based on the width and height of the character sprite itself and are used to make sure the thought bubble gets drawn over the character's head in the right place instead of at his feet, since player.x and player.y are based on the center-bottom of the character.  As far as I can tell they are not related to my problem and can be ignored for my purposes here.

Anyway, my script works fine in rooms where the room dimensions are the same as the screen dimensions.  However, in scrolling rooms, where the room dimensions are bigger than the viewport, it started behaving strangely.  From what I've been able to figure based on the AGS documentation, the issue is that character position coordinates are based on the dimensions of the room itself, whereas GUI position is based solely on screen coordinates that remain the same regardless of how big the room is or where the character is on the screen vs. the room.  So, the script is drawing the GUI at a location based on the player's location in the room, rather than on the screen. 

So, what I need to know is:  is there some way to script this so that it will draw the thought bubble at the same location over the player's head regardless of where the player is standing or how big the room is?  I'm thinking there has to be some sort of way to calculate this mathematically, but I am pretty much the world's worst math student.  Anyone with any help or insight will earn my eternal gratitude.

Crimson Wizard

Conversion between room and screen coordinates is done with the help of Viewport class (the "camera"):
Code: ags

gGUI1.X = cCHAR1.x - GetViewportX();
gGUI1.Y = cCHAR1.y - GetViewportY() - y_offset;

Or vice versa:
Code: ags

int gui_over_room_x = gGUI1.X + GetViewportX();
int gui_over_room_y = gGUI1.Y + GetViewportY();

MUFFINinc

This works perfectly, thank you.  You are indeed a wizard, sir.

SMF spam blocked by CleanTalk