Sphere to Sphere Collision Detection

Does anyone know where to start when it comes to Sphere-Sphere Collision Detection? I'm drawing a complete blank, and have no clue.
distance from centers < sum of radii
its pure geometry, the code is simple. you can eliminate the sqrt and work with distance squared instead of distance with minor modifications to the standard geometry, which is faster, and x1*x1 is faster than pow(x,2). I say that because 99% of these problems are about millions of spheres and efficiently solving large problems.
Write the positions of the centres of the two moving spheres as a function of time t.

Write the distance (squared) between these centres and set it equal to the sum of radii (squared). This will give you a quadratic in t.

If the quadratic has no roots (or only negative roots) then they don't collide in positive time. Otherwise, the smallest positive root will give the collision time.
I have to do Sphere to Sphere Collision Response and Sphere to Sphere Collision Detection
this is what I have so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "Collider.h"
#include "Plane.h"
#include "Body.h"

bool Collider::SphereSphereCollisionDetection(Body& body1, Body& body2)
{
	Body tempBody1;
	Body tempBody2;
	Vec3 distance = tempBody1.getPos() - tempBody2.getPos();

	float distmag = sqrt(distance.x * distance.x + distance.y * distance.y + distance.z * distance.z);
}

bool Collider::SpherePlaneCollisionDetection(Body& body1, Plane& plane)
{
	Plane plane1;
	float distance = plane1.distance(body1);
	Body tempBody = body1;
	if (distance <= tempBody.getRadius())
	{
		return true;
	}

	return false;
}

void Collider::SphereSphereCollisionResponse(Body& body1, Body& body2, float e)
{
	//find normal
	Vec3 normal = body1.getPos() - body2.getPos();

	//normalized the normal
	Vec3 normalizedNormal = VMath::normalize(normal);

	//find the projection
	float projMag1= VMath::dot(-body1.getVel(), normalizedNormal);
	float projMag2 = VMath::dot(-body2.getVel(), normalizedNormal);

	//add CoR/Elasticity
	float a1 = ((body1.getMass() - e * body2.getMass()) * (1 + e) * body2.getMass() * projMag2) / (body1.getMass() + body2.getMass());
	float a2 = ((body2.getMass() - e * body1.getMass()) * (1 + e) * body1.getMass() * projMag1) / (body2.getMass() + body1.getMass());

	//setting the final velocity
	body1.setVel((body1.getVel() + (a1 - projMag1) * normalizedNormal));
	body1.setVel((body2.getVel() + (a2 - projMag2) * normalizedNormal));
}

void Collider::SphereStaticSphereCollisionResponse(Body& body1, Body& body2)
{
	Body tempBody2 = body2;

	//find normal
	Vec3 normal = body1.getPos() - tempBody2.getPos();

	//normalized the normal
	Vec3 normalizedNormal = VMath::normalize(normal);

	//find the projection
	Vec3 projection = VMath::dot(-body1.getVel(), normalizedNormal) * normalizedNormal;

	//setting the final velocity
	body1.setVel(body1.getVel() + 2 * projection);
}

void Collider::SpherePlaneCollisionResponse(Body& body1, Body& body2)
{
	//find normal
	Vec3 normal;
	normal.x = plane.x;
	normal.y = plane.y;
	normal.z = plane.z;

	//normalized the normal
	Vec3 normalizedNormal = normal / sqrt(plane.x * plane.x + plane.y * plane.y + plane.z * plane.z);

	//find the projection
	Vec3 projection = VMath::dot(-body1.getVel(), normalizedNormal) * normalizedNormal;

	//setting the final velocity
	body1.setVel(body1.getVel() + 2 * projection);
}


Well, if YOU wrote that then you clearly wouldn't have any difficulty detecting a collision, would you? But I should do that first - as explained in my last post. Then you'll know when and where the spheres are when they collide.

There's no point in having both moving and static sphere routines - the latter is just a special case with velocity zero.
I got this from someone, I just wanted to double check
I got from the teacher during class
Topic archived. No new replies allowed.