admin管理员组

文章数量:1025526

I probably missing something, but I could not find out.

The physics is working fine (see the video, green boxes are bodies )

However, a dynamic body do not hit an static one.

Here is my relevant code that creates the body and shape.

  body_ptr body{nullptr, cpBodyFree};
  std::unordered_map<bodytype, std::function<void()>> mapping = {
      {bodytype::stationary, [&body]() {
         body = body_ptr(cpBodyNewStatic(), [](cpBody *body) { cpBodyFree(body); });
       }},
      {bodytype::kinematic, [&body]() {
         body = body_ptr(cpBodyNewKinematic(), [](cpBody *body) { cpBodyFree(body); });
       }},
      {bodytype::dynamic, [&body, &size]() {
         body = body_ptr(cpBodyNew(1.0, cpMomentForBox(1.0, size.width(), size.height())), [](cpBody *body) { cpBodyFree(body); });
       }}
  };

  const auto p = j["physics"];

  mapping[p["type"].get<bodytype>()]();

  auto shape = shape_ptr(cpPolyShapeNew(body.get(), n, vertices, cpTransformIdentity, 0.0), [](cpShape *shape) { cpShapeFree(shape); });

  cpShapeSetFriction(shape.get(), p.value("friction", 0.5f));
  cpShapeSetElasticity(shape.get(), p.value("elasticity", 0.3f));
  cpSpaceAddShape(_world->space().get(), shape.get());
  cpSpaceAddBody(_world->space().get(), body.get());

What I am missing?

I searched, but the old solutions uses a function that does not exist anymore.

I probably missing something, but I could not find out.

The physics is working fine (see the video, green boxes are bodies https://youtu.be/RFkP0zuVm3U)

However, a dynamic body do not hit an static one.

Here is my relevant code that creates the body and shape.

  body_ptr body{nullptr, cpBodyFree};
  std::unordered_map<bodytype, std::function<void()>> mapping = {
      {bodytype::stationary, [&body]() {
         body = body_ptr(cpBodyNewStatic(), [](cpBody *body) { cpBodyFree(body); });
       }},
      {bodytype::kinematic, [&body]() {
         body = body_ptr(cpBodyNewKinematic(), [](cpBody *body) { cpBodyFree(body); });
       }},
      {bodytype::dynamic, [&body, &size]() {
         body = body_ptr(cpBodyNew(1.0, cpMomentForBox(1.0, size.width(), size.height())), [](cpBody *body) { cpBodyFree(body); });
       }}
  };

  const auto p = j["physics"];

  mapping[p["type"].get<bodytype>()]();

  auto shape = shape_ptr(cpPolyShapeNew(body.get(), n, vertices, cpTransformIdentity, 0.0), [](cpShape *shape) { cpShapeFree(shape); });

  cpShapeSetFriction(shape.get(), p.value("friction", 0.5f));
  cpShapeSetElasticity(shape.get(), p.value("elasticity", 0.3f));
  cpSpaceAddShape(_world->space().get(), shape.get());
  cpSpaceAddBody(_world->space().get(), body.get());

What I am missing?

I searched, but the old solutions uses a function that does not exist anymore.

Share Improve this question asked Nov 17, 2024 at 13:23 RodrigoRodrigo 57116 gold badges76 silver badges162 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

After briefly looking at the documentation, it seems that you are missing the definition for the collision behavior of your shapes and the appropriate collision callbacks.

The collision type getters and setters are defined like this in the manual (link):

 cpCollisionType cpShapeGetCollisionType(const cpShape *shape) void
 cpShapeSetCollisionType(cpShape *shape, cpCollisionType value)

And then you will have to implement your collision callback functions using the Collision Handler API (link). Generally, it will involve defining the callbacks defined in this struct:

struct cpCollisionHandler {
    cpCollisionType typeA, typeB;
    cpCollisionBeginFunc beginFunc;
    cpCollisionPreSolveFunc preSolveFunc;
    cpCollisionPostSolveFunc postSolveFunc;
    cpCollisionSeparateFunc separateFunc;
    cpDataPointer userData;
};

I would also recommend you look at the example code in the manual and the open source example code to see how all of these come together, here is an example I found.

I hope this is enough to get you unstuck.

I probably missing something, but I could not find out.

The physics is working fine (see the video, green boxes are bodies )

However, a dynamic body do not hit an static one.

Here is my relevant code that creates the body and shape.

  body_ptr body{nullptr, cpBodyFree};
  std::unordered_map<bodytype, std::function<void()>> mapping = {
      {bodytype::stationary, [&body]() {
         body = body_ptr(cpBodyNewStatic(), [](cpBody *body) { cpBodyFree(body); });
       }},
      {bodytype::kinematic, [&body]() {
         body = body_ptr(cpBodyNewKinematic(), [](cpBody *body) { cpBodyFree(body); });
       }},
      {bodytype::dynamic, [&body, &size]() {
         body = body_ptr(cpBodyNew(1.0, cpMomentForBox(1.0, size.width(), size.height())), [](cpBody *body) { cpBodyFree(body); });
       }}
  };

  const auto p = j["physics"];

  mapping[p["type"].get<bodytype>()]();

  auto shape = shape_ptr(cpPolyShapeNew(body.get(), n, vertices, cpTransformIdentity, 0.0), [](cpShape *shape) { cpShapeFree(shape); });

  cpShapeSetFriction(shape.get(), p.value("friction", 0.5f));
  cpShapeSetElasticity(shape.get(), p.value("elasticity", 0.3f));
  cpSpaceAddShape(_world->space().get(), shape.get());
  cpSpaceAddBody(_world->space().get(), body.get());

What I am missing?

I searched, but the old solutions uses a function that does not exist anymore.

I probably missing something, but I could not find out.

The physics is working fine (see the video, green boxes are bodies https://youtu.be/RFkP0zuVm3U)

However, a dynamic body do not hit an static one.

Here is my relevant code that creates the body and shape.

  body_ptr body{nullptr, cpBodyFree};
  std::unordered_map<bodytype, std::function<void()>> mapping = {
      {bodytype::stationary, [&body]() {
         body = body_ptr(cpBodyNewStatic(), [](cpBody *body) { cpBodyFree(body); });
       }},
      {bodytype::kinematic, [&body]() {
         body = body_ptr(cpBodyNewKinematic(), [](cpBody *body) { cpBodyFree(body); });
       }},
      {bodytype::dynamic, [&body, &size]() {
         body = body_ptr(cpBodyNew(1.0, cpMomentForBox(1.0, size.width(), size.height())), [](cpBody *body) { cpBodyFree(body); });
       }}
  };

  const auto p = j["physics"];

  mapping[p["type"].get<bodytype>()]();

  auto shape = shape_ptr(cpPolyShapeNew(body.get(), n, vertices, cpTransformIdentity, 0.0), [](cpShape *shape) { cpShapeFree(shape); });

  cpShapeSetFriction(shape.get(), p.value("friction", 0.5f));
  cpShapeSetElasticity(shape.get(), p.value("elasticity", 0.3f));
  cpSpaceAddShape(_world->space().get(), shape.get());
  cpSpaceAddBody(_world->space().get(), body.get());

What I am missing?

I searched, but the old solutions uses a function that does not exist anymore.

Share Improve this question asked Nov 17, 2024 at 13:23 RodrigoRodrigo 57116 gold badges76 silver badges162 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

After briefly looking at the documentation, it seems that you are missing the definition for the collision behavior of your shapes and the appropriate collision callbacks.

The collision type getters and setters are defined like this in the manual (link):

 cpCollisionType cpShapeGetCollisionType(const cpShape *shape) void
 cpShapeSetCollisionType(cpShape *shape, cpCollisionType value)

And then you will have to implement your collision callback functions using the Collision Handler API (link). Generally, it will involve defining the callbacks defined in this struct:

struct cpCollisionHandler {
    cpCollisionType typeA, typeB;
    cpCollisionBeginFunc beginFunc;
    cpCollisionPreSolveFunc preSolveFunc;
    cpCollisionPostSolveFunc postSolveFunc;
    cpCollisionSeparateFunc separateFunc;
    cpDataPointer userData;
};

I would also recommend you look at the example code in the manual and the open source example code to see how all of these come together, here is an example I found.

I hope this is enough to get you unstuck.

本文标签: cChipmunk no collisionStack Overflow