Kinematic body fails collision detection,but dynamic ok!Why?

Hello.

I have a kinematic body with a certain (slow) velocity. The problem is that, even though I have declared edges, the object jumps out of them (collision detection & resolution is not performed).

I i make the body dynamic, the edges make the collision successfully, keeping the object inside.

Why does this happen, do I need to enable anything? Perhaps CCD? How?

(XCode, iPhone, box2d latest version)
So is this is a kinematic hollow box made with four edge polygons? Do you have dynamic bodies inside that are falling out? Do they collide with the box at all?
Collisions have to have at least one dynamic body.

In my game, I ended up having to make a bunch of static sensors dynamic just so that they would work properly.
I have attached below the setup with DebugDraw enabled. We're talking about box2d+cocos2d-iphone.

The edges are defined like this:
Code:
// Define the ground body.
      b2BodyDef groundBodyDef;
      groundBodyDef.position.Set(0, 0);
      b2Body *groundBody = world->CreateBody(&groundBodyDef);
      
      // Define the ground box shape.
      b2PolygonShape groundBox;     
      
      CGSize worldSize=CGSizeMake(screenSize.width/PTM_RATIO, screenSize.height/PTM_RATIO);   //Change values as needed for your world size.
      //-------------------------------------------------------------------
      // bottom
      //-------------------------------------------------------------------
      groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(worldSize.width,0));
      groundBody->CreateFixture(&groundBox);
      //-------------------------------------------------------------------
      // top
      //-------------------------------------------------------------------
      groundBox.SetAsEdge(b2Vec2(0,worldSize.height), b2Vec2(worldSize.width,worldSize.height));
      groundBody->CreateFixture(&groundBox);
      //-------------------------------------------------------------------
      // left
      //-------------------------------------------------------------------
      groundBox.SetAsEdge(b2Vec2(0,worldSize.height), b2Vec2(0,0));
      groundBody->CreateFixture(&groundBox);
      //-------------------------------------------------------------------
      // right
      //-------------------------------------------------------------------
      groundBox.SetAsEdge(b2Vec2(worldSize.width,worldSize.height), b2Vec2(worldSize.width,0));
      groundBody->CreateFixture(&groundBox);


The moving body is declared like this:
Code:
bodyDef.angle = 15 * 0.0174532925;
      bodyDef.type = b2_kinematicBody;
      bodyDef.position.Set(5.0f, 300/PTM_RATIO);      
      b2Vec2 speed(5.0f, 5.0f);      
      bodyDef.linearVelocity = speed;   
      body = world->CreateBody(&bodyDef);
      
      // Define another box shape for our dynamic body.
      b2PolygonShape dynamicBox;
      dynamicBox.SetAsBox(0.5f, 0.5f);
      
      // Define the dynamic body fixture.
      b2FixtureDef fixtureDef;
      fixtureDef.shape = &dynamicBox;
            
      // Set the box density to be non-zero, so it will be dynamic.
      fixtureDef.density = 0.5f;
      
      // Override the default friction.
      fixtureDef.friction = 0.3f;
      
      fixtureDef.restitution = 0.9f;
      
      fixtureDef.userData = (void*)1;
      
      // Add the shape to the body.
      body->CreateFixture(&fixtureDef);


The problem is that the body gets out of the edges of the screen 🙁

If I make it dynamic, it is ok, but I would like it to be kinematic.

Thanks!
A kinematic body effectively has infinite mass. Box2D cannot stop bodies with infinite mass, so it doesn't even try.

A good case for a kinematic body is a platform, not a bouncing box.
Thanks. I understand.

So what would be a good approach for a ball that moves with a given speed, and when it hits a wall, to bounce back with the SAME speed (I've tried restitution, but it's different).

I juts want to keep the same speed, only change direction.
Any ideea for this approach?
I've noticed I can set the linearvelocity to a dynamic body, but they are affected by gravity 🙁

How could I combine a fixed speed (being maintained even after collisions) with a dynamic body?
Make it a sensor

Unfortunately, that also gets rid of collisions.
You can get velocity reflection if you set the restitution to 1 and friction to 0.
That wouldn't prevent gravity though.

Original thread: https://box2d.org/forum/viewtopic.php?t=4442