Bounce, stick, ..

Official forum for the Chipmunk2D Physics Library.
Post Reply
OFred
Posts: 6
Joined: Sun Oct 12, 2008 3:11 pm
Contact:

Bounce, stick, ..

Post by OFred »

Hello,

I'm new with this beautiful library. To learn how to use it I just want a ball to bounce on the left/right walls and stick on the top wall. And when I throw another ball, I want it to stick with other balls, and top wall but bounce on the left/right walls.

To do this I tried two things:
1) Playing with elasticity and friction coefficient, but I dosn't work.
2) I use cpSpaceAddCollisionPairFunc to detect the collision with the top wall, then I set the velocity of the ball to 0 and I remove the collisionPairFunc (to save cpu?!) It's OK.
I add another addCollisionPairFunc to detect collision between two balls and set their velocity to 0 but it doesn't work the way I want :(

Can someone explain me the best way to do this ?

Cheers,

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

Re: Bounce, stick, ..

Post by slembcke »

Depends on how solid you want them to be after they get stuck.

If you want them to be bouncy like this: http://www.youtube.com/watch?v=0VuB2XFMHk8 you will need to create joints and springs.

If you just want them to be completely solid, that is easier. Make a collision callback between the ball and "sticky" objects. The callback should do the following:
  • Set the velocity of the ball to (0,0)
  • Set the mass and moment of inertia to infinity
  • Set the shape of the ball to be a sticky shape
  • Remove the ball's body from the space (don't free it until the game is done)
This way the ball stops moving, becomes static (you set it's mass properties to infinity, and removed it from the space), it becomes part of the sticky geometry.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
OFred
Posts: 6
Joined: Sun Oct 12, 2008 3:11 pm
Contact:

Re: Bounce, stick, ..

Post by OFred »

Wow It's exactly the kind of game I would like to dev !!

"Set the shape of the ball to be a sticky shape" how do I do this ?

Thanks a lot !

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

Re: Bounce, stick, ..

Post by slembcke »

I don't mean that there are stickiness parameters in Chipmunk. I mean to use the collision type to identify which are sticky and which are not and let the collision pair callback to the rest.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
OFred
Posts: 6
Joined: Sun Oct 12, 2008 3:11 pm
Contact:

Re: Bounce, stick, ..

Post by OFred »

Ok thanks !!
That works great !
OFred
Posts: 6
Joined: Sun Oct 12, 2008 3:11 pm
Contact:

Re: Bounce, stick, ..

Post by OFred »

I'm back, life is not so easy :(

I think there is a good place to remove a shape:
i've tried to do it in the Collision callback but it's freeze and die which leads me to the cpArtiber.c file line:
con->r1 = cpvsub(con->p, a->p);
I think it tries to access the destroyed shape ?!

It's hard to dev with something we don't know how it works.

Can someone help me ?

Cheers,

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

Re: Bounce, stick, ..

Post by slembcke »

Oh, sorry. It looks as though I forgot to copy the section on collision pair functions over from the old documentation. I'll have to get that added back.

At this time, you cannot free objects that you remove from a space during a collision callback.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
OFred
Posts: 6
Joined: Sun Oct 12, 2008 3:11 pm
Contact:

Re: Bounce, stick, ..

Post by OFred »

In your game scribBall, after an animation you make the ball disapear (explosion) So, when do you free objects ? Not in the collision pair function ?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Bounce, stick, ..

Post by slembcke »

No, we use the collision pair functions to find groups of like colored balls. After the physics step is complete, we run a graph algorithm to find all the groups larger than 4.

Now that I'm thinking about it, it wouldn't really be all that hard to change so that you can remove the objects inside of a collision pair function. For now, you'll just have to make a list of the objects that you want to remove and then remove them once cpSpaceStep() returns.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
OFred
Posts: 6
Joined: Sun Oct 12, 2008 3:11 pm
Contact:

Re: Bounce, stick, ..

Post by OFred »

ok Thanks for the tips !!! It simply works great !
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests