最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

c++ - Chipmunk no collision - Stack Overflow

matteradmin8PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far