Page 1 of 1

Changing position of body/Collisions

Posted: Fri Jan 31, 2014 9:30 pm
by mjfransen
I am using Cocos2D V3 with Chipmunk integrated. I've posted a rough screenshot of the app I am working on. The pigs fly across the screen. I am doing this by giving them a random position and then applying force. The ball (physics body) in the middle is attached to spring joint. If the pigs run into the ball they collide on the borders as expected. I want the user to be able to move this ball using their finger. The problem is that when I change the position of the node in touchMoved then the collisions don't happen right away. The pig and the ball will overlap and then they will slowly correct themselves so they are not overlapping. If this ball is in motion due to the spring joint, then they collide like what you would expect. I am convinced that I shouldn't be moving the ball by setting the position property while touchMoved is called, but I don't know how to do it so it can continuously move as touchedmoved is called. Any ideas on how I can move the ball and have the pigs and ball not overlap?

Image

Re: Changing position of body/Collisions

Posted: Sat Feb 01, 2014 12:01 pm
by slembcke
So the problem is that when you set the position of a body, you aren't asking it to move smoothly through time and space like a regular object, you are making it teleport instantly to the new position without having it's velocity changed. If you teleport a body into a position where it overlaps with something else, Chipmunk resolves the overlap the same way it does any overlap by assuming it's a small overlap and softly pushing the two objects apart. Normally objects move because they have a velocity set on them that causes the position to change smoothly over time. Then when they collide, it's their velocities that determine most of the result of collision.

So in other words, you need to update both the position and velocity of the ball as you drag it around. Lars wrote some code to do that in the templates here: https://github.com/cocos2d/cocos2d-ipho ... onSphere.m

There is also the ChipmunkMultiGrab class that is in Objective-Chipmunk that does pretty much exactly what you want to do, but isn't wrapped in CCPhysics yet. It wouldn't be too hard to use this if you wanted, but you'd have to dig into how CCPhysics is implemented a bit to use it.

Re: Changing position of body/Collisions

Posted: Mon Feb 03, 2014 10:09 am
by mjfransen
Thank you so much for the help! I used Lars code as a guide and this is now working exactly how I want. Thanks again!

Re: Changing position of body/Collisions

Posted: Mon Feb 03, 2014 6:16 pm
by mjfransen
Is there a lot of Chipmunk that isn't integrated with Cocos2D yet? It seems like there are many more joint types that are in Chipmunk and not in CCPhysics.

Re: Changing position of body/Collisions

Posted: Mon Feb 03, 2014 7:15 pm
by slembcke
Yes, though some of it is on purpose. CCPhysics is intended to be a lot easier to learn than Chipmunk, and to some extent that means leaving out some of Chipmunk's fancier features. Hopefully that is made up for by making it extendable and open source. If you game needs to do something fancy, then it's fairly easy to get at the Chipmunk guts underneath and extend it.