Any idea how to build an Impenetrable PhysicsBody?

Hello everyone~

I am trying to build a PhysicsBody like a wall that is impenetrable. But when I put any of my sprite in the PhysicsWorld, they pass through each other with some nasty restitution.

Thanks a lot.

Haha, :smiley: LOL

Ok…
So by default if you’re creating a physics box then nothing can pass through it.
Secondly, if you’re creating sprites with physics bodies in your physics world then they must collide by default.

And can you please tell how come it is doing with nasty restitution when it CAN PASS THROUGH THE OTHER SPRITE.
This can be possible, I guess, when you’re giving -ve restitution value.

Note: I don’t know whether -ve restitution is allowed in cocos2d-x physics/box2d/ chipmunk but the real physics says that theoretically if pairs of bodies has -ve restitution then they will pass through.

Hello, thank you for replying~

I am using 3.3 beta. It has its own integrated Physics engine.
Let me show you a sample code that how I create a brick…

auto brick=Sprite::create("brick_normal.png");
auto ran=CCRANDOM_0_1();
auto posi_x=ran*visibleSize.width+origin.x;
auto posi_y=origin.y;

auto brick_body=PhysicsBody::createBox(brick->getContentSize());

brick_body->setDynamic(false);
brick_body->setCollisionBitmask(BRICK_COLLISION_BITMASK);
brick_body->setContactTestBitmask(true);
brick->setPhysicsBody(brick_body);

brick->setPosition(Point(posi_x,posi_y));
layer->addChild(brick);

This brick that I create is not a solid brick= =My other sprites can penetrate this brick and kind of bouncing on it (it depends on the speed they collide.) I know in BOX2D we build physics item and they are solid by default…but I am using the new beta version and the new Physics class (They said they combined chipmunk and Box2D into this new class)

Hey, hi… doesnot matter whether it is solid or just boundary.
If something is outside that brick then it cannot enter… Also what do you mean by

Is it penetrating and bouncing inside/outside it?

Also, if your speed of the moving sprites is too too high then depending upon the framerate of the game, if it is too low, then it may happen that collisions are failed to be calculated at certain instances. But this happens in some cases only when FPS is quite low, not as low as ~40FPS.

Also, have you tried making the physics body as solid box rather than create Box…
Hey wait!! Isn’t createBox creates a solid box?
I guess, your brick with the code you gave should be solid… I use cocos2d-x v3.2 and it has same built-in physics…
For me it created solid box… just set the debug to ALL, I mean put the below statement after you define your physics scene.

scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

And then check whether it is really hollow box!! which I guess should not be…
AFAIK hollow box can be made by createEdgeBox, which is also generally used for screen box. Just check it out.

auto brick_body= PhysicsBody::createBox(brick->getContentSize(),PhysicsMaterial(1,0.5,0.3));
Just set the density of brick_body as follows friction, restitution.

This is what I found earlier when I needed something. But I don’t know how to use this with inbuilt physics system!!
I guess @slackmoehrle might help in this…
I cannot give credit to the correct person because I lost the actual link. But since I stored the information, so here it is…

**By other Person to some other Person: **
This below problem aroused when in cocos2dJS Parkour tutorial the player used to fall through the physics floor.


Ok, this may or may not solve the issue some scenarios, but after looking at it profoundly, the biggest suspect seems to be a lack of steps in the calculations of Chipmunk when the framerate drops (which appears to happen when you resize or minimize/maximize the screen).

The closest thing to a fix that I’ve found comes from a lead in this post in the forums, and which I’ve adapted it like this: where you have this.space.step(dt); in your update function, change it for the following:

var timeWindow = 1/60; //replace by your configured FPS rate if it’s not 60
var steps = Math.ceil(dt / timeWindow);
var i = 0;
for (; i < steps; i++) {
this.space.step(timeWindow);
}

This should ensure that even if your application drops below 60fps the physics simulation will be updated properly, and this problem should not happen anymore.


Have you looked at this: http://www.cocos2d-x.org/wiki/Physics

I also notice that you didn’t set a CategoryBitmask

This is the effect that the brick is moving up slowly. This picture shows the final position of two sprites. They will move up together like this. (BTW, the sprite look like a cowboy is just affected by the gravity and supposed to be falling down)

This is the effect that the brick is moving up faster. The brick will hold the man and moving up like this…

Yes, I suppose that Category bitmask is by default 0xFFFFFFFF. See the post that I reply catch_up. I explain the problem in that post. Thank you

Hey catch_up,
Sorry for bothering you.

Can you give me any sample codes that you create a solid box that will not let other physicsbody pass through? I turned to version 3.2 but my problem is still there.

Hi… I tried experimenting with the types of physics bodies(hollow and solid).
One thing I found is, if solid-solid and hollow-solid collide then they will actually collide and not penetrate.
But when you’ll do hollow-hollow collision then by default they’ll penetrate each other…, so I guess this is the problem I guess you’re facing!!
If not, then I wonder… In the diagram that you attached and explanation along I found a little less.
From the diagram I see that both sprites have bodies created using solid box which is createBox and not createEdgeBox.

Ok, can you give us more information… also, your description seems to be a little inadequate.

Otherwise, for my code part. It is simply creating sprites with physics body with all in same scene: createBox createEdgeBox around screen, createEdgeBox around a sprite, createCircle and then leave them under gravity.
I applied force on them randomly. Only problem was, the sprite(having physics body) created with createEdgeBox was passing out of the createEdgeBox of my game screen.

I’ll try to set correct bit masks and try to handle this… and tell you what I got

Thanks

Turning to v3.2 from 3.3 won’t have helped because they’re having same physics integration with same APIs :smile:

Hi, @slackmoehrle

I was experimenting with category bit mask and group for the physics bodies.

I noticed the createEdgeBox which is my screen box and createEdgeBox which is around a sprite does not collide at all.
I mean, I tried default behavior which is not setting any category or group.
Then i tried setting category and then I tried setting same groups for both.

I noticed that this all above worked for solid-solid and hollow(edge box)-solid physics bodies combinations but didn’t work with hollow-hollow physics bodies…

Can you please tell how to do it… I mean hollow body which is made using createEdgeBox SHOULD collide with other hollow body??
NOTE: I already referred to the physics chapter in programmer’s guide which is under progress by you… :smile:

What is VE Rectitude ? Thanks

It’s not VE rectitude…

-ve” is read as negative. So, it’s negative restitution.

I did try that but caused break point :frowning: