Object.GetAtRoomXY implementation

Started by vga256, Mon 15/01/2024 00:07:45

Previous topic - Next topic

vga256

I did some searches through github and reviewed the engine documentation, but I couldn't find an answer to this problem: I'm trying to understand how Object.GetAtRoomXY works at an algorithmic level.

Does the hit test involve a simple check whether there is a room object - defined by the object's height and width at room coordinates, regardless of the object's sprite transparency? Or does it only return an object when the point is opaque (e.g. non-transparent) in the object's transparency mask?

eri0o

In your general settings, you can set the pixel perfect click detection, in the visual category.

https://adventuregamestudio.github.io/ags-manual/GeneralSettings.html#visual

QuotePixel-perfect click detection - normally, when the player clicks the mouse, AGS just checks to see if the cursor is within the rectangular area of each character and object on the screen. However, if this option is checked, then it will further check whether the player clicked on an actual pixel of the object graphic, or whether it was a transparent part of the graphic. If this option is enabled and they click on a transparent pixel, then the hotspot behind the object will be activated instead.

As far as I remember it does a reverse projection of the point in the transformed rectangle of the sprite and then picks up the pixel color and returns true if it doesn't match the COLOR_TRANSPARENT

It uses some sorting to figure it out the order too (which can be slow, but it sorts only on Objects I think).

vga256

#2
Quote from: eri0o on Mon 15/01/2024 00:35:55As far as I remember it does a reverse projection of the point in the transformed rectangle of the sprite and then picks up the pixel color and returns true if it doesn't match the COLOR_TRANSPARENT

It uses some sorting to figure it out the order too (which can be slow, but it sorts only on Objects I think).

Perfect, thanks! That explains why I was getting no object found when it was hitting transparent pixels on my object. (I had Pixel Perfect enabled).

I tested it, and it is true. This is kind an interesting undocumented feature of the engine: Object.GetAtRoomXY's point hit detection is determined by the Pixel-perfect click detection setting.

Crimson Wizard

#3
Quote from: vga256 on Mon 15/01/2024 00:38:58I tested it, and it is true. This is kind an interesting undocumented feature of the engine: Object.GetAtRoomXY's point hit detection is determined by the Pixel-perfect click detection setting.

It's not a "undocumented feature", but rather not documented well enough. The "click" in the setting's name is for a historical reason. I suppose that in early versions of AGS there were no functions that would find an object, and all the player click behavior was hardcoded in the engine.

Today there's no difference between "click" and "hit", as clicks are done through a hit test, so that's effectively exact same thing.
Everything that deals with finding objects under cursor depends on this setting, because each related action goes through the same hit test algorithm. This includes ProcessClick, GetLocationType, GetLocationName (and consequently "@OVERHOTSPOT@" macro label), GetAtScreenXY, GetAtRoomXY, and anything else I might forget.

EDIT: updated this in manual.

SMF spam blocked by CleanTalk