Post subject: How do you find hitboxes?
Player (71)
Joined: 8/24/2004
Posts: 2562
Location: Sweden
I was wondering how you go about to find the hitbox for a certain enemy, items, or what ever it might be, in any game there is. Do you search them in the same way always, or does it differ between different games? The thing that I would want to do is to draw hitboxes around enemies for example, using LUA. Are there any good tutorials for LUA involving the emulators we use at this site? Thanks in advance.
Emulator Coder, Skilled player (1311)
Joined: 12/21/2004
Posts: 2687
It's very different depending on the game. First you'll want to start by figuring out how to draw a simple point or same-sized box at the position of each object/enemy, which tends to be more similar between games than drawing a full hitbox. Basically you have to find where the relevant position values are stored by using whatever RAM search functionality is available. It's probably best to start with drawing something at the main character's position, and hopefully you'll find that the same method automatically works for all the other enemies and objects onscreen with a slight address change (for example in Sonic 1 and 2 you can add 0x40 to the address to go to the next object, and the player is always the first object). You may have to find the position of the camera too unless the position values are already in screen coordinates, but that's usually the easiest part. (One of the Lua samples with Gens shows how to draw a box at the player position in Sonic 1, including taking the camera into account.) Once that's done, now all that's left is to figure out how big the box should be for each object. In the best case, if the game stores all its information about objects together, you can find the hitbox width and height values close in memory to the position values. (It tends to be that easy with Sonic games, for example.) In that case I guess you'd usually check for something like 2 consecutive values each between 8 and 64 (a wild guess but that's the normal range of size values I've seen so far). In the worst case, every type of thing in the game has its own routine that does its own collision checking where the size is hardcoded in the routine and there is no general way of knowing the size of an object. In that case, maybe the best you can reasonably do is figure out where the object type ID is stored, then make a big table of approximate hitbox sizes for all the object types you know about, and use the type ID to choose the size from the table. An in-between case is if the game keeps a table of all object sizes somewhere in ROM or RAM, but only keeps indirect pointers or indices into that table in the objects themselves. In that case, it can take some serious analysis (or at least a good disassembler) to figure out where that size data is stored and how to use it, although of course you can always use the previous method of making a list of sizes manually instead as a last resort. Best best case: all objects in the game are the same size and you're already done as soon as you know where they are. Worst worst case: the game's collision shapes aren't even boxes, and you'll have to figure out how to calculate it and draw circles or rotated rectangles or multi-polygonal shapes, if the aim is to accurately represent the collision.
Skilled player (1828)
Joined: 4/20/2005
Posts: 2161
Location: Norrköping, Sweden
Here's a lua manual, and qFox has made a list of the different lua-commands for the emulators here. If the game is simple enough, it's not that hard to make a script that draws hitboxes around enemies. Because of the way boxes are drawn in lua, you want to have a function that, given a point (x,y) and a width and height, draws a box around (x,y) with that given height and width. Such a script could look something like: (xoffset and yoffset is how many pixels the box is moved from the center, useful in some cases, but they should be both 0).
local function box(xcenter,ycenter,width,height,xoffset,yoffset,color)

left  = xcenter-width/2+xoffset
down  = ycenter+height/2-yoffset
right = xcenter+width/2+xoffset
up    = ycenter-height/2-yoffset

	if (left > 0 and left <255> 0 and right <255> 0 and down <224> 0 and up < 224) then
		gui.drawbox(left,down,right,up,color);

	end;
end;
The if-statements are to make sure that the box is on screen, otherwise the box isn't drawn. Now, if you put the enemy's x and y position as the xcenter and ycenter argument, and give the function values for width and height, it draws a box around the enemy. Ex: Let's say that the enemy's x and y positions are stored at RAM addresses $0100 and $0101 respectively. To draw a red 16*16 pixel box around that enemy, you write:
enemy1x = memory.readbyte(0x100)
enemy1y = memory.readbyte(0x101)

box(enemy1x, enem1y, 16,16,0,0,"red")
If the enemies' hitboxes are of different sizes, you'll have to make some if-statement or so that takes respect to what type of enemy you're dealing with, and in that case you'll have to find the RAM address for the enemy type.
P.JBoy
Any
Editor
Joined: 3/25/2006
Posts: 850
Location: stuck in Pandora's box HELLPP!!!
Not all games use height and width from the centre of the sprite, some games have an offset from the position of the character for the top boundry, left boundry, right boundry, and bottom boundry (for example, Fusion in which the position is the centre-bottom of the sprite)
Joined: 3/7/2006
Posts: 720
Location: UK
Fighting games are also likely to offset from the bottom centre of the sprite, as this is a natural anchoring point for turning and such.
Voted NO for NO reason
Banned User
Joined: 12/23/2004
Posts: 1850
Most games will use the anchor point as the top left and have hitboxes in increments of 8x8 pixels. There are always some oddballs, but for the most part they're anchored at the top left.
Perma-banned
Player (71)
Joined: 8/24/2004
Posts: 2562
Location: Sweden
Thanks for the info. :)