Jump to content
  • Advertisement
Sign in to follow this  

trouble comparing objects. code included

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

If you intended to correct an error in the post then please contact us.

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;
				}
				if(distance < this.meshRadius*2)
				{
					int k = 2;//they are overlapped
					this.sphereCenter.Z +=2;
					p.sphereCenter.Z += 0;
				}

Share this post


Link to post
Share on other sites
Advertisement
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 If
else
{
// they are overlapping
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!