Changes in sensor behavior in Chipmunk 6

Discuss any Chipmunk bugs here.
TomorrowPlusX
Posts: 28
Joined: Tue Jul 12, 2011 6:31 am
Contact:

Changes in sensor behavior in Chipmunk 6

Post by TomorrowPlusX »

Hi,

I just rebuilt my (very alpha) game with Chipmunk 6. In 5.3.4 I've been making heavy use of sensors for pathfinding, and they were working quite well.

In 5.3.4 if a dynamic body/shape were removed from the space, the separate collision handler for my sensors overlapping that shape would be invoked and my pathfinding code could update accordingly. In Chipmunk 6, however, the cpCollisionSeparateFunc is not invoked when a shape overlapping my sensor is removed from the space.

( My game dynamically destroys and adds new shapes at runtime to approximate dynamic cutting/reshaping/etc of my terrain. So it's important that the sensor callbacks are invoked when geometry is removed )

Is this a bug, or a feature of Chipmunk 6 I need to figure out a workaround for?

Here's my setup code for callbacks:

Code: Select all

cpSpaceAddCollisionHandler( 
	space,
	_collisionType,
	CollisionType::PATHFINDING_SENSOR_TYPE,
	&Pathfinder_cpCollisionBegin,
	NULL,
	NULL,
	&Pathfinder_cpCollisionSeparate,
	this
);
And here's my callbacks:

Code: Select all

cpBool Pathfinder_cpCollisionBegin(cpArbiter *arb, struct cpSpace *space, void *data)
{
	cpShape *a = NULL, *b = NULL;
	cpArbiterGetShapes( arb, &a, &b );
	Pathfinder *pathfinder = (Pathfinder*) data;
	
	// my understanding is that 'b' should be my sensor...	
	Pathfinder::Probe *probe = (Pathfinder::Probe*) b->data;
	if ( probe->incrementDynamicMark() ) pathfinder->_probeOccupationStatusChanged = true;
	
		
	return false; // prevents further computation -- _cpCollisionSeparate will still be called however
}

void Pathfinder_cpCollisionSeparate(cpArbiter *arb, cpSpace *space, void *data)
{
	cpShape *a = NULL, *b = NULL;
	cpArbiterGetShapes( arb, &a, &b );
	Pathfinder *pathfinder = (Pathfinder*) data;
	
	// my understanding is that 'b' should be my sensor...	
	Pathfinder::Probe *probe = (Pathfinder::Probe*) b->data;
	if ( probe->decrementDynamicMark() ) pathfinder->_probeOccupationStatusChanged = true;

}
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Changes in sensor behavior in Chipmunk 6

Post by slembcke »

Hmm. Shoot. That's a bug.

I thought I had a unit test to catch that case, but I guess not.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
TomorrowPlusX
Posts: 28
Joined: Tue Jul 12, 2011 6:31 am
Contact:

Re: Changes in sensor behavior in Chipmunk 6

Post by TomorrowPlusX »

Sorry to make work for you :)

But I'm glad it is a bug and not a feature, because I was working out how best to work around this and it wasn't looking pretty.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Changes in sensor behavior in Chipmunk 6

Post by slembcke »

Heh, now I have to do it instead. ;)

Sensors and sleeping shapes are sort of a giant pain for callbacks because they don't keep the collision cache "warm" like regular collisions do. There are few lines of some annoying code because of it. Apparently I did some of it wrong. :-\
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Changes in sensor behavior in Chipmunk 6

Post by slembcke »

So I figured out what the problem is. Let me just say. Oh crap. :(
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
TomorrowPlusX
Posts: 28
Joined: Tue Jul 12, 2011 6:31 am
Contact:

Re: Changes in sensor behavior in Chipmunk 6

Post by TomorrowPlusX »

Sorry to ruin your day. Is there anything I can do to help? I'm not familiar with Chipmunk's internals, but I'm a reasonably smart guy.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Changes in sensor behavior in Chipmunk 6

Post by slembcke »

Well, the way that the separate callbacks happen is unfortunately very delicate and complicated. It's very tightly integrated (not really in a good way any more) with how Chipmunk caches collisions. Chipmunk tracks a number of flags and such in it's collision pairs (cpArbiter) when their state changes (active, cached, etc). There is also a number of edge cases that had to be tacked on such as calling the separate callbacks immediately when shapes are removed because if you waited until the next cpSpaceStep() call, the user might have already freed the shape pointer. Sensor shapes and rejected collisions on sleeping objects also have some annoying hacks to get around the fact that they don't keep their collisions "hot" in the cache.

The bug I made with Chipmunk 6 is that separate callbacks only get called on removal for collisions in the contact graph. The contact graph is mildly expensive to generate so I made it optional to enable it. -_-

To fix it, I'm going to have to redesign how some of the collision caching works. There will probably be some unavoidable performance ramifications for the add/remove functions. I'm not quite sure how to ask for help with that, and explaining how all the little bits of code here and there work sounds kind of hard. I do appreciate the offer though.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
TomorrowPlusX
Posts: 28
Joined: Tue Jul 12, 2011 6:31 am
Contact:

Re: Changes in sensor behavior in Chipmunk 6

Post by TomorrowPlusX »

Well, I guess I'm glad I helped find a bug? ;)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Changes in sensor behavior in Chipmunk 6

Post by slembcke »

Ok, so I wrote a bunch more collision handler unit tests yesterday (they are part of Objective-Chipmunk). I think I fixed the issue, but am looking for a slightly less stupid way to implement it. I'm also writing more tests to try and check more of the edge cases to make sure I did it right this time. I haven't pushed the changes to the public repository yet.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
TomorrowPlusX
Posts: 28
Joined: Tue Jul 12, 2011 6:31 am
Contact:

Re: Changes in sensor behavior in Chipmunk 6

Post by TomorrowPlusX »

Wow! I will patiently wait. The good news is this has forced me to put pathfinding aside for a few days so I'm having a fun time writing tentacle-dynamics for my monster :D
Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests