Is there any way to tell if a shape collides with another shape im trying to make a simple game to test things. Im drawing circles with fillpie.
Is there any way to tell if a shape collides with another shape im trying to make a simple game to test things. Im drawing circles with fillpie.
Simplest collision detection.
Circle to circle for 2D.
Sphere to Sphere for 3D
Radius of sphere???? (r)
Radius of Sphere A = rA
Radius of Sphere B = rB
Center position of Sphere A = vA
Center position of Sphere B = vB
You mentioned a circle, but for 2D or 3D but same principle applies.
Calculate distance between two points! (Pythagorean Thereom)
vD = vB - vA;
vD.x = vB.x - vA.x
vD.y = vB.y - vA.y
vD.z = vB.z - vA.z // If 3D
fDist = sqrt( (vD.x * vD.x) + (vD.y * vD.y) ); // 2D
fDist = sqrt( (vD.x * vD.x) + (vD.y * vD.y) + (vD.z * vD.z) ); // 3D
So fDist is distance between the two points.
If distance between the two sphere centers is equal to rA + rB then you are touching. Less then that then you're merged!
So if (fDist < rA+rB) collided
else if (fDist == rA+rB) touching
else not collision
If you have a lot of objects doing the square root takes time so instead do the distance calculation but skip the square root.
Then calculate but square the radius distance
r = rA + rB
r = r * r;
So now do the same comparison, but no need to spend processor time doing the square root operation.
thanks. I really should have thought of that.
There are many ways to optimize this, however, implementing those in script is difficult. Does anyone have any suggestions?