sphere collision

I am working on a school assignment and I am stuck on one part. I need to change a if statement
1
2
3
4
5
6
7
8
9
bool SphereCollision(Vector3 p1, float r1, Vector3* v1, float m1,
					 Vector3 p2, float r2, Vector3* v2, float m2,
					 float E) {
	Vector3 temp;

	temp = Subtract(p2, p1); //Here, p1 and p2 are the center of each sphere

//Add the proper sphere-sphere collision detection here in this one line of the if statement:
	if (temp.length() <= sqrt( r1+r2)  ) {
I added the sqrt(r1+r2). It was if(temp.length()<=r1). I know the formula for collision detection is if the distance is less then the radii then there is a collision. I have tried to change the temp.length() to sqrt(temp.length()) according to the book that I have and it stops before it even touches it. I just need some help figuring out how to code the correct line for it to work.
The first step is to think about this physically - what does it mean for the two objects to collide in terms of their radii and separation vector?

Then think about the tools you have at hand: for example, what does the function Vector3::length() do for you?

EDIT: Oh, and I assume you're not breaking the rules of your assignment by asking for help. That is up to you to check.
Last edited on
From what the lab says it just shows the length. I believe that is the distance between the spheres. With that looking at my book it I just noticed something that I might have missed before. I saw where they had a code very simular to this if (temp.length() <= r1+r2 ) . So I think the problem I am having is in these lines.
1
2
3
4
5
//modify these two lines of code so that the correct final speed formulas are used:
//hint: look at the last set of formulas from the Week 6 lecture.
        v2f = v2i;
		v1f = v2f; 
  
No I'm not breaking the rules by asking for help. I'll work on them and see if that is the problem.
Last edited on
I got it figured out.
Topic archived. No new replies allowed.