Optimisation?

Official forum for the Chipmunk2D Physics Library.
Post Reply
__fastcall
Posts: 4
Joined: Thu May 29, 2008 8:12 pm
Contact:

Optimisation?

Post 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;
			}
		}
	}
?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Optimisation?

Post 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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
__fastcall
Posts: 4
Joined: Thu May 29, 2008 8:12 pm
Contact:

Re: Optimisation?

Post by __fastcall »

Ah, I see now.

Thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests