Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected collision resolution with multiple static shapes #158

Closed
JoelBesada opened this issue Nov 4, 2017 · 3 comments
Closed

Unexpected collision resolution with multiple static shapes #158

JoelBesada opened this issue Nov 4, 2017 · 3 comments

Comments

@JoelBesada
Copy link

I've noticed an issue when colliding with multiple static objects at the same time, and reduced it down to a simple case as seen in this video https://gfycat.com/gifs/detail/CommonRecentCentipede.

case

The expected outcome is for the ball to bounce to the right instead of straight up on the first contact. This behavior makes it hard to build complex terrain that works well, because a concave shape needs to be decomposed into stitched together convex polygons, and every seam that this creates makes the above case more likely to happen.

I created this as a demo, here's the source code for it:

#include "chipmunk/chipmunk.h"
#include "ChipmunkDemo.h"

static cpBody *ballBody;

static void
update(cpSpace *space, double dt)
{
	cpSpaceStep(space, dt);
}

static cpSpace *
init(void)
{
	cpSpace *space = cpSpaceNew();
	cpSpaceSetIterations(space, 30);
	cpSpaceSetGravity(space, cpv(0, -800));
	cpSpaceSetCollisionSlop(space, 0.5);
	cpSpaceSetSleepTimeThreshold(space, 1.0f);
	
	cpBody *body, *staticBody = cpSpaceGetStaticBody(space);
	cpShape *shape;
	
	cpVect poly[3] = {cpv(-100, 0), cpv(0, -50), cpv(0, -150)};
	shape = cpSpaceAddShape(space, cpPolyShapeNew(staticBody, 3, poly, cpTransformIdentity, 1.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 0.9f);
	
	cpVect poly2[3] = {cpv(0, -50), cpv(100, -75), cpv(0, -150)};
	shape = cpSpaceAddShape(space, cpPolyShapeNew(staticBody, 3, poly2, cpTransformIdentity, 1.0f));
	cpShapeSetElasticity(shape, 1.0f);
	cpShapeSetFriction(shape, 0.9f);
	
	cpFloat radius = 15.0f;
	ballBody = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero)));
	cpBodySetPosition(ballBody, cpv(0, 100));
	cpBodySetVelocity(ballBody, cpv(0, -500));

	shape = cpSpaceAddShape(space, cpCircleShapeNew(ballBody, radius, cpvzero));
	cpShapeSetElasticity(shape, 0.5f);
	cpShapeSetFriction(shape, 0.5f);
	
	return space;
}

static void
destroy(cpSpace *space)
{
	ChipmunkDemoFreeSpaceChildren(space);
	cpSpaceFree(space);
}

ChipmunkDemo MulticontactIssue = {
	"Multicontact Issue",
	1.0/60.0,
	init,
	update,
	ChipmunkDemoDefaultDrawImpl,
	destroy,
};
@ghostserver10
Copy link

Hey. Have you found solution?

@JoelBesada
Copy link
Author

@ghostserver10 not really, but I wrote about a workaround that I ended up using instead to avoid these scenarios: https://medium.com/@JoelBesada/swang-development-log-fixing-janky-collisions-f84bf4b285e5

@slembcke
Copy link
Owner

slembcke commented Feb 11, 2019

Add a smoothing radius to the polygons. Even a few pixels is going to help tremendously. I would also highly recommend using line shapes to outline large objects like terrain instead of making them solid with polygons. Avoids some of the issues and should give much better performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants