Overlapping of Box2d bodies, and get stuck - cocos2d-iphone

I'm using box2D with cocos2D in my iOS game. While creating box2d bodies randomly on the screen(as they are too much in number) some time two bodies are created so closely that they got stuck with each other. The following bodies (image) are created using polygon.
So how can i detect these bodies and separate them from each other? so i can move the body individually currently they behave like a single body.

Related

How can I get a list of only objects that moved in Box2D?

This should be a fairly easy question to some. It is more of a system design & Box2D API question.
I have custom game engine I am working on and am implementing Box2D as the physics engine. I am trying to only update the game objects' transform that have b2body* attached to them as a component and that are categorized as kinematic or dynamic (not static). I emphasize the "only" because I am trying to have my overall engine to have good performance. I looked at the Box2D documentation and did not notice a way to get a list or an array of b2body* that moved when the step() function was called.
So, did I just miss when reading the docs, or should I just keep track of which objects are kinematic and dynamic through a list based data structure and only update those every time I step?
Note: the third type of bodies are static bodies and should not move and do not intend to moved them so I will not update the position.
Thank you everyone in advance.
Link to the Box2D documentation: https://box2d.org/documentation/

Can box2d track my Cocos2d actions of sprites?

I just started using Cocos2D this week. While playing around with Box2d i was wondering if it was possible to move CCSprites with the help of CCActions and use box2ds collisiion detection feature to detect collision between those sprites..
I'm pretty sure this must be possible?
If you don't need real physics behavior, I'd highly recommend to "manually" deal with your collision logic. That said, for your scenario I would start with this approach.-
Create one body per sprite, and assign each sprite to the user data.
Your 'static' scenario would map to static bodies (i.e floor, platforms, etc...)
Your 'dynamic' sprites would map to dynamic bodies, which only fixture would be marked as sensor
You'd register a b2ContactListener to listen for the collisions.
As for the tricky part, you'd need to set in each iteration of the main loop, the position of each body to the position of each sprite (of course, translating pixels to meters), in order to avoid that they just behave as physics bodies. You could try just to not calling world->step, but not sure if contactListener would work then.
Hope it helps!

Box2d - spawning bodies ontop of eachother

is it possible to spawn objects in the same location, but make their bodies not join together?
Basically i have a bunch of b2bodies that i spawn at a given time, and i need them to bounce off each other rather than stick to each other..
So this would create a sort of explosion of the created bodies because they repel against each
other.
Anyone have any ideas?
Is this possible with box2d?
Based from my experience, dynamic bodies with single fixtures would immediately repel each other the moment their physics simulation steps begin. Bodies with multiple fixtures would have problems if their fixtures crisscross each other because they would stick together.
I'm not sure if you can get explosion this way, because the repulsion force is usually weak. What you can do is to set up a contact listener that would detect contacts and apply opposing forces to bodies that are touching during spawning. Or maybe you can do AABB query before spawning a body to check if there is already a body at the spawn location, and apply the forces to the bodies.

Chipmunk body move with Parallax?

I'm using Cocos2D, SpaceManager and Chipmunk. I have a parallax node with 4 layers on it, this is tied to the location of a playable chipmunk body. This body needs to collide with static objects on one of the parallax layers, the static bodies must start off screen then move into screen and collide.
I know you are not supposed to move static bodies with Chipmunk, unless you rehash. However, rehashing 60 time a second to keep up with the framerate seems messy. Can anyone think of an alternate way to do this?
Cheers.
One thing you could try is joining all the objects in each parallax layer to a non-colliding body using rigid joints. Then move that body along with the parallax layer to drag the objects along. THis may not be the best method, though.

How to implement moving platforms with Cocos2d, TMXTiledMaps and Chipmunk

I'm making slow but steady progress with a Cocos2d game, but I'm stuck creating moving platforms.
The main character needs physics and collision detection, and is therefore a chipmunk shape/body. I wrote a class to iterate over the TMXTiledMap in order to cut back on the amount of bodies in the chipmunk space. So with a map like this
----------
--------x-
-xxx----x-
----------
instead of having 5 individual bodies (rects), there are two bodies, One is three tiles wide, the other is two tiles tall.
I've managed to get the code working to identify which tiles are part of a moving platform and to move the tiles as needed.
However, the bodies need to move with the tiles in order for this to work properly. And this is where I'm stuck. The bodies are of a static mass so...
platformShape->body->p = cpv(x,y);
Doesn't do anything (I'm guessing that this is the expected behavior).
But if I set their mass to anything other than static, all the physics comes into play and the bodies do not behave as expected, or they behave perfectly depending on you how you look at it. They move erratically and the rotate when they hit another body (eg: the main character). What I'm after is the typical type of moving platform you would expect to find in a typical platform game that moves smoothly in any given direction.
My question is; Has anyone implemented something like this before and what was your technique? Or, if you were to implement something like this, how would you do it?
The relevant code is here. I put it in a pastebin since I figure it's more of a conceptual misunderstanding than anything else.
It turns out you need to call
cpRehashStaticShapes
Obvious really, but easy to miss in my opinion.