Is there any quick way to get all contacting bodies with specific b2bBody?
for example i have 200 cricles(dynamic bodies) and want to know how many circle contacting with each circle
The most efficient way is to use the BeginContact and EndContact callbacks of the contact listener to update a list for each body, of what it is currently touching.
This page might help: http://www.iforce2d.net/b2dtut/collision-callbacks
That example only tracks the number of touching bodies by using a simple integer counter, but you could also use a std::set or similar structure to keep track of which bodies they are.
Related
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/
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!
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.
I am using box 2d. I just want to move my body to the point. What is the best way to do this?
When you say you just want to move the body, do you mean you want to apply a force to get the body to a point?
There's a joint designed for mouse movement, and it might work well in your case if you want to drag bodies around on an iPhone. It's called the Mouse Joint, and it's under 8.10 on these box2d docs.
If you want a body that doesn't respond to things hitting it, but pushes things around based on where it is and where it is going, go for the b2_kinematicBody on the same docs
Hope it helps. Your question is very vague.
EDIT in response to comment:
Well, generically the way to do this would be cpBodyApplyForce or cpBodyApplyImpulse. There are many ways to use this to move the body to a position, and they can get more complex than I can summarize in a comment. Essentially, you're getting into stuff that can be better covered by game AI programming sources.
The most basic way would be to apply a force that is some multiple (on each axis) of the distance from the object to the target position. If you want the object to slowly stop, the search terms "AI arrive behavior" might be a good idea. I found this discussion on gamedev.net.
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.