fguihen Posted January 3, 2005 Posted January 3, 2005 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. Person is a class i wrote to define a person in a game.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. it seems like something to do with the dot net framework that i dont understand. 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; } Quote
Diesel Posted January 3, 2005 Posted January 3, 2005 This code is in what method? Anyway, you should be implementing the IComparable interface for each person. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemicomparableclasstopic.asp You can then use the hashcode in the CompareTo function, you dont need to, you can compare the objects themselves, but as long as the hashcode is unique it should be faster. Also, I think your design is not very efficient. Shouldnt the collision detection be done outside the person class? Also, that would leave less combinations to go through. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.