admin管理员组文章数量:1434922
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 badges1 Answer
Reset to default 0After 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
版权声明:本文标题:c++ - Chipmunk no collision - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745633492a2667413.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论