ID:1923599
 
(See the best response by Ter13.)
Code:
HitRange(var/rng=12)
var/a_x=abs_px()
var/a_y=abs_py()
switch(src.dir)
if(NORTH)
return obounds(a_x-(rng/2),a_y+16,rng,rng,src.z)
//return obounds(src,0,rng,0)
if(SOUTH)
//return obounds(src,0,-rng,0)
return obounds(a_x-(rng/2),a_y-rng,rng,rng,src.z)
if(EAST)
return obounds(a_x+bound_width,a_y-16,rng,rng,src.z)
//return obounds(src,rng,0,0)
if(WEST)
//return obounds(src,-rng,0,0)
return obounds(a_x-rng,a_y-16,rng,rng,src.z)
if(SOUTHWEST)
//return obounds(src,-round(max(6,rng/2)),-round(max(6,rng/2)),0)
//return obounds(a_x-rng,a_y-rng,rng,rng,src.z)
return obounds(a_x-TILE_WIDTH,a_y-TILE_HEIGHT,rng,rng,src.z)
if(SOUTHEAST)
//return obounds(src,round(max(6,rng/2)),-round(max(6,rng/2)),0)
//return obounds(a_x+bound_width,a_y-rng,rng,rng,src.z)
return obounds(a_x+bound_width+TILE_WIDTH,a_y-TILE_HEIGHT,rng,rng,src.z)
if(NORTHEAST)
//return obounds(src,round(max(6,rng/2)),round(max(6,rng/2)),0)
return obounds(a_x+bound_width+TILE_WIDTH,a_y+TILE_HEIGHT,rng,rng,src.z)
if(NORTHWEST)
//return obounds(src,-round(max(6,rng/2)),round(max(6,rng/2)),0)
return obounds(a_x-TILE_WIDTH,a_y+TILE_HEIGHT,rng,rng,src.z)


Problem description:
TLDR
This is my hit box code. I began trying to improve it a few days ago by using some absolute coodinates.
The idea is to position the "hit area" dynamically based on the size of the box and the direction.

So for instance if I wanted to create a 24x24 size box, and my character is facing RIGHT, it would be positioned to the east of me and slightly to the south.

For the most part this code above works out well, it just seems to be offset unproperly so I was hoping someone could help with the maths.
Best response
proc
melee_bounds(atom/movable/ref,dir,dist,width,align)
var/w,h

if(istype(ref))
w = ref.bound_width
h = ref.bound_height
else
w = TILE_WIDTH
h = TILE_HEIGHT

switch(dir)
if(NORTH)
. = bounds(ref,floor((w-width)/2)+align,h,width-w,dist-h)
if(SOUTH)
. = bounds(ref,floor((w-width)/2)+align,-dist,width-w,dist-h)
if(EAST)
. = bounds(ref,w,floor((h-width)/2)+align,dist-w,width-h)
if(WEST)
. = bounds(ref,-dist,floor((h-width)/2)+align,dist-w,width-h)


This is more or less what you are looking for. If you have a reference object, you don't need to use absolute coordinates. Better to use the offset coordinate formula.
I just got back to this.
I am using the object as a reference. I still want to use the absolute coordinates of the object.