gpt4 book ai didi

c++ - 连接两个 Box2d 实体

转载 作者:太空宇宙 更新时间:2023-11-04 11:45:47 25 4
gpt4 key购买 nike

我正在创建一个应用程序,我在其中尝试以这样的方式连接两个 body ,即当我移动一个 body 时,第二个 body 应该在第一个 body 内移动。

这是我创建主体的代码:

    - (b2Body *)addBoxBodyForDynamicSprite:(CCSprite *)sprite {

b2BodyDef spriteBodyDef;
spriteBodyDef.type = b2_dynamicBody;
//spriteBodyDef.position.Set(sprite.position.x/PTM_RATIO, sprite.position.y/PTM_RATIO);
CGPoint asolutePoint = [sprite.parent convertToWorldSpace:sprite.position];
spriteBodyDef.position.Set(asolutePoint.x/PTM_RATIO, asolutePoint.y/PTM_RATIO);
spriteBodyDef.userData = (__bridge void*)sprite;
b2Body *spriteBody = world->CreateBody(&spriteBodyDef);

b2PolygonShape spriteShape;
spriteShape.SetAsBox(sprite.contentSize.width/PTM_RATIO/2,
sprite.contentSize.height/PTM_RATIO/2);
b2FixtureDef spriteShapeDef;
spriteShapeDef.shape = &spriteShape;
spriteShapeDef.density = 0.3;
spriteShapeDef.isSensor = true;
spriteBody->CreateFixture(&spriteShapeDef);

return spriteBody;
}

一个物体是运动的,另一个物体是动态的。我正在使用以下方法移动这些物体:

theBody->SetTransform(locationWorld, theBody->GetAngle());

如果我在这里施加线性力,物体不会移动,我用来固定它们的关节是 b2WeldJoint

b2JointDef jointDef;
jointDef.bodyA = another;
jointDef.bodyB = leftHandFixBody;
aJoint = (b2Joint *)world->CreateJoint(&jointDef);

它移动动态主体,但运动主体保持在其位置。我想把两个 body 一起移动。任何帮助将不胜感激。谢谢!

最佳答案

此外,根据手册,运动体通过设置它们的速度来移动,而不是通过施加力来移动。

A kinematic body moves under simulation according to its velocity. Kinematic bodies do not respond to forces. They can be moved manually by the user, but normally a kinematic body is moved by setting its velocity. A kinematic body behaves as if it has infinite mass, however, Box2D stores zero for the mass and the inverse mass. Kinematic bodies do not collide with other kinematic or static bodies.

此外,我发现使用 SetTransform(...) 移动物体效果不佳。我用它创建了一个传送门,可以将 body 从一个地方跳到另一个地方,而且效果很好。但是如果我在每个模拟周期更新它, body 就会停止与其他 body 碰撞。这只是一个警告。

这有帮助吗?

关于c++ - 连接两个 Box2d 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19902763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com