ID:148497
 
I have attempted to redo Theodis's pixel based movement demo and have been stumped on the collision system. The distance formula seems to be the problem because all distance outputs seem a little small compared to the pixel coords. This system is based from pixels. (come to think of it my finding of the positions of pixel centers may be the problem! Not sure though.)
//remapping collision detection system
atom/proc/Collision(atom/movable/A)//algorithms
atom/movable/proc/Collided(atom/A)
atom/proc/Radius() return collisionradius
atom/proc{pxpos()return x*64-16+pixel_x;pypos()return y*64-16+pixel_y}
atom/var/collisionradius = -1 //make sure you can't collide with nondense

proc/CheckDist(atom/A,atom/M)
var/ax = A.pxpos() // A.x*32-16+A.pixel_x//find A's centerpoint
var/ay = A.pypos() // A.y*32-16+A.pixel_y
var/mx = M.pxpos() // M.x*32-16+M.pixel_x//find M's centerpoint
var/my = M.pypos() // M.y*32-16+M.pixel_y
var/dist = abs(sqrt((abs(ax-mx))^2+(abs(ay-my))^2)) //based from pythagorean theorum
// world << "[dist] [A.Radius()],[M.Radius()]"
world << abs(sqrt((abs(ax-mx))^2+(abs(ay-my))^2))
if(dist < A.Radius()+M.Radius()) return 0
else return 1

mob/Move(turf/T)
var/no = null
for(var/atom/A in range(2,src)-src)
if(!CheckDist(A,src))
spawn A.Collision(src)
spawn src.Collided(A)
src << "You have collided with [A]!"
no = 1
if(!no)
if(loc)loc.Exit(src)
if(T)T.Enter(src)
if(T)src.loc = T

mob/collisionradius = 12
mob/Collided() velocity = 0
You don't need to use abs() around sqrt(); that's pretty pointless. Likewise you don't need to use abs() before squaring the values, because squaring them will fix the problem for you.

The reason your results are coming out wrong is that you used the wrong operator. You used ^ when you should have used **. In some languages, ^ is for exponentiation, but in BYOND it's for a bitwise XOR.

I wouldn't recommend using **, however, or your code's gonna be dog slow. You also don't need to use sqrt(), which will be dog slow as well. Instead, use this:
var/dx = A.pxpos() - M.pxpos()
var/ay = A.pypos() - M.pypos()
var/r = A.Radius() + M.Radius()
return( dx*dx + dy*dy >= r*r )
Better still, I'd actually avoid the pxpos() and pypos() procs, because this is something you're going to be doing a lot and efficiency is crucial to a routine like this.
var/dx = (A.x<<5) - (M.x<<5) + A.pixel_x - M.pixel_x
var/dy = (A.y<<5) - (M.y<<5) + A.pixel_y - M.pixel_y

Lummox JR