If you want to do it yourself and are interested in mathematics read these:
http://www.gamedev.net/reference/articl ... le1026.asp
http://www.gamedev.net/reference/progra ... /page2.asp
the first one is not quite correct.. my actuall programm uses these theories.
Both even work with concave polygons and use raycasting.
Notice: The first one works with circles only(but will be enough for your game I guess)/took me 3 days to find out why it doesn't work for elipsoids(the corners react wrong)
The following will be interesting to:
http://www.gamasutra.com/features/20000 ... der_01.htm
On page 2 you will find "Don’t Cross that Line".. That theorie is Perfect for non raycasted point-checks.... I'm willing to give my actuall code for the last one:
Pointx and Pointy are the polygons with index 1 up to each number you like Pointny and nx is the polygon normal for line between given point and the next one.
Code: Select all
function closestPointOnPolygon(x,y, index)
temp=1000000000
rx=0
ry=0
for index2,content2 in pairs(Pointx[index]) do
if(Pointx[index][(index2+1)]==nil) then index3=1 else index3=index2+1 end
tx, ty = closestPointOnLine(x,y, Pointx[index][index2],Pointy[index][index2], Pointx[index][index3],Pointy[index][index3])
if(((tx-x)^2+(ty-y)^2)<temp) then
rx=tx
ry=ty
temp=((tx-x)^2+(ty-y)^2)
end
end
return rx, ry
end
function PointInPolygon(px,py)
for index,content in pairs(Pointx) do
temp=0
for index2,content2 in pairs(Pointx[index]) do
if(Pointx[index][(index2+1)]==nil) then index3=1 else index3=index2+1 end
if((((Pointy[index][index2]-py)<=0) and ((Pointy[index][index3]-py)>0)) or (((Pointy[index][index2]-py)>0) and ((Pointy[index][index3]-py)<=0))) then
tx, ty = closestPointOnLine(px,py, Pointx[index][index2],Pointy[index][index2], Pointx[index][index3],Pointy[index][index3])
if(tx>=px) then temp=1-temp end
end
end
if(temp==1) then return 1, closestPointOnPolygon(px,py, index) end
end
return 0, 0, 0
end
To let the normals of the lines be computed upon load after giving the polygons(This function only works with the Vertexes of a polygon given clockwise):
Code: Select all
Pointnx={}
Pointny={}
for index,content in pairs(Pointx) do
Pointnx[index]={}
Pointny[index]={}
for index2,content2 in pairs(Pointx[index]) do
if(Pointx[index][(index2+1)]==nil) then index3=1 else index3=index2+1 end
linelen=math.sqrt((Pointx[index][(index3)]-Pointx[index][(index2)])^2+(Pointy[index][(index3)]-Pointy[index][(index2)])^2)
Pointnx[index][index2]=(Pointy[index][(index3)]-Pointy[index][(index2)])/linelen
Pointny[index][index2]=-(Pointx[index][(index3)]-Pointx[index][(index2)])/linelen
end
end
If you really want to controll the physics of your game try understanding these functions and what they do. Read, Read, code, find out that it's wrong, read again and go on with try and error, you will learn much doing this.