-1

I know this isn't exactly the "correct" way to find collision points since there could be 1 OR 2 (Not 0 though because the collision is guaranteed) but I wanted to just try it out and see how it rolls out, so I created 3 spheres and 2 of the points (Each point is supposed to represent a collision point between 2 spheres) are way out of where they should be, as if I got the direction vector to be opposite of what it needed to be... here's my Lua code:

    local function getDist(s1, s2)
        local s1_Pos = (s2.Position - s1.Position).unit * s1.Size.X * .5
        local s2_Pos = (s1.Position - s2.Position).unit * s2.Size.X * .5
        return s1_Pos:Lerp(s2_Pos, .5)
    end
  1. What is wrong with this code?
  2. How can I find the collision point(s) of 2 spheres?

I read a few questions on StackOverflow but most were either irrelevant or just didn't seem to make sense.

KapKing
  • 3
  • 4

1 Answers1

0

let p0,p1 be the centers and r0,r1 be the radiuses then

if (|p1-p0|<=r0+r1)  // collision if centers closer then sum of radiuses
 {
 p = p0 + ((p1-p0) * r0 / (r0+r1)); // weighted linear interpolation
 }

The collision point is not the mid point between centers but is shifted closer to the smaller sphere. This equation is precise only if (r0+r1 == |p1-p0|) otherwise both spheres overlap and equation returns mid point of the overlapped volume.

so if p0=(x0,y0,z0), p1=(x1,y1,z1) you can rewrite:

if ((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0)+(z1-z0)*(z1-z0)<=(r0+r1)*(r0+r1)) 
 {
 x = x0 + (((x1-x0)*r0)/(r0+r1));
 y = y0 + (((y1-y0)*r0)/(r0+r1));
 z = z0 + (((z1-z0)*r0)/(r0+r1));
 // (x,y,z) is your collision point
 }

You can also get the overlapp circle bounding the overlap volume. It is circle with center (x,y,z) computed above and perpendiculare to vector (p1-p0) which also defines the cut plane for both spheres. So if you need points inside this circle use basis vectors and simple parametric or implicit function of circle/disc. For more info see:

Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380
  • Thanks, it seems like it worked :D Although a slight explanation of how you get the x, y, z would be heavily appreciated, as I'm a bit confused as to why for example x0 is there twice and x1 is there once, and r0 is there twice and r1 is there once, and all that, but the top line is understandable, seeing if the distance between the 2 sphere centers is the same or smaller than the 2 spheres' radii (Which can be done in a more simplistic way where I'm doing it (p0-p1).Magnitude <= r0+r1) – KapKing Mar 19 '17 at 13:06
  • @KapKing it is called linear interpolation `a = a0+ (a1-a0)*t` if `t=0` than `a=a0` if `t=1` than `a=a1` and if `t` is in between than `a` is also in between `a0,a1` . Now the `t` is called parameter and is on interval `<0.0,1.0>` as you got radiuses and distance instead then you need to convert it somehow: `t = r0 / (r0+r1)` this will give you `t` aligned to the collision mid point (weighted by both `r0,r1` ) – Spektre Mar 19 '17 at 14:56
  • alright, that seems to clear things up a bit, thanks. – KapKing Mar 19 '17 at 17:50