mob/Player/CheckHit(atom/S,Slope)
//y=mx+b
var
Rad = 6
Y1 = y+pixel_y/32+16-Rad //Bottom
Y2 = y+pixel_y/32+16+Rad //Top
X1 = x+pixel_x/32+16-Rad //Left
X2 = x+pixel_x/32+16+Rad //Right
if(Slope)
var
BI = (Y1-S.y)/Slope //Bottom intercept
TI = (Y2-S.y)/Slope //Top intercept
if((X1<=BI && BI<=X2) || (X1<=TI && TI<=X2))
world<<"-----------HIT----------"
return src
var
LI = Slope*X1+S.y //Left intercept
RI = Slope*X2+S.y //Right intercept
if((Y1<=LI && LI<=Y2) || (Y1<=RI && RI<=Y2))
world<<"-----------HIT----------"
return src
return 0
Problem description:
I've been working on this bit of code for a while now, and I think this method should work, but it doesn't. Can anyone see any reason? It works at a few different slopes, but not at all of them, and it sometimes registers a hit even if I'm facing 90 degrees away from them...
Thanks in advance!
For this example, let's ditch the "grid" and use circles instead.
So, let's write our checkhit() proc.
Without comments, it looks like this:
Does that make sense? Let's give a test example.
You (at origin (0,0)) shoot at a circle at (4,7) with radius=3, with an angle of 40 degrees.
Like I said, all distance formula. And a little law of sines to spice things up.
Please do double-check my work. It's hard to think about this stuff without scratch paper.