# trouble comparing objects. code included

This topic is 4971 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i am passing the ArrayList Persons to the person class so that each person can check to see if any of the rest of the persons are coliding. its only very basic right now but im stepping through it and i can see that the sphereCenter for the current object is always the same as the sphereCenter for the object the current object is checking its position against. here is the code. i have the hash code comparison here to check if the current person is being compared to itself. dont know if this is the correct solution,
foreach (Person p in Persons)
{
if(this.GetHashCode() == p.GetHashCode())
{
//break;
int d = 90;
Persons.Remove(p);
}
float a =  this.sphereCenter.X - p.sphereCenter.X;
float b = this.sphereCenter.Y - p.sphereCenter.Y;
float c = this.sphereCenter.Z - p.sphereCenter.Z;
float distance =(float) Math.Sqrt(a*a + b*b + c*c);

if (distance > this.meshRadius*2)
{
int i =0;//they are not overlapped;
}
if ( distance == this.meshRadius*2)
{
int j = 1;//they are just touching;
}
{
int k = 2;//they are overlapped
this.sphereCenter.Z +=2;
p.sphereCenter.Z += 0;
}


##### Share on other sites
Quote:
 if ( distance == this.meshRadius*2){ int j = 1;//they are just touching;}

This will probably never work ok.
You should set some kind of "tolerance", I'm not good at english so I'll post some code:
if( distance > this.meshRadius*2-tolerance &&    distance <= this.meshRadius*2 ){   // they are just touching}

Also, you should use else if insted of if alone.
This is what the code should look like in the end:
if( distance > this.meshRadius*2 ){  // they are not overlapping}// Notice that you don't need to test for "distance <= this.meshRadius*2"//because you know that the value will be less or equal to this.meshRadius*2//from the If above.else if( distance > this.meshRadius*2-tolerance ){  // they are just touching}// If they are not not overlapping nor just touching they must be overlapping,//so there's no need to one more Ifelse{  // they are overlapping}

1. 1
2. 2
frob
15
3. 3
4. 4
Rutin
12
5. 5

• 13
• 12
• 58
• 14
• 15
• ### Forum Statistics

• Total Topics
632120
• Total Posts
3004220

×