Page 1 of 1

Optimisation?

Posted: Thu May 29, 2008 8:19 pm
by __fastcall
Couldn't the code:

arbiter.c @ line 115

Code: Select all

for(int i=0; i<arb->numContacts; i++){
		cpContact *old = &arb->contacts[i];
		
		for(int j=0; j<numContacts; j++){
			cpContact *new_contact = &contacts[j];
			
			// This could trigger false possitives.
			if(new_contact->hash == old->hash){
				// Copy the persistant contact information.
				new_contact->jnAcc = old->jnAcc;
				new_contact->jtAcc = old->jtAcc;
			}
		}
	}
be optimised to:

Code: Select all

for(int i=0; i<arb->numContacts; i++){
		cpContact *old = &arb->contacts[i];
		
		for ( int j = i + 1; j<numContacts; j++){  // j = i + 1
			cpContact *new_contact = &contacts[j];
			
			// This could trigger false possitives.
			if(new_contact->hash == old->hash){
				// Copy the persistant contact information.
				new_contact->jnAcc = old->jnAcc;
				new_contact->jtAcc = old->jtAcc;
			}
		}
	}
?

Re: Optimisation?

Posted: Fri May 30, 2008 10:39 am
by slembcke
No because it's not comparing entries in the same array. The first element from the first array could match up with the first in the second array.

Also, that's not a very expensive loop at all. 99% of the time, you are only going to have 1 or 2 contacts in an arbiter (pair of shapes). The only way you'd have more is if you are colliding an extremely detailed polygon that's deeply penetrating another object. Even then, this loop will be far less expensive that the actual collision query.

Re: Optimisation?

Posted: Fri May 30, 2008 4:29 pm
by __fastcall
Ah, I see now.

Thanks!