Box2D对象位置不会受到速度变化的影响

时间:2018-12-15 20:13:20

标签: c++ box2d

我以前使用过Box2D,所以我正在使用的这段代码已经在另一个项目中起作用了,唯一的主要区别是世界并没有作为参数传递,而是作为全局指针给出的。

当我应用“线性速度”(我已经尝试过从1到99999,仍然没有尝试过)时,即使Obj1->Body2D->GetPosition().x;获得线性速度的位置也不会发生任何变化。

主要:

GameObject Obj1 = new GameObject(iVec2(500), iVec2(30));
void Update() {
    Obj1->b2_body->SetLinearVelocity(b2Vec2(5, 0));
    //Still same Position on Obj1
    //Camera Update
    Obj1->Render(*Renderer);
}

脚本1:

std::unique_ptr<b2World> World = std::make_unique<b2World>(b2Vec2(0.0f, 9.81f));

脚本2:

extern std::unique_ptr<b2World> World;

GameObject::GameObject(iVec2 position, iVec2 size, Texture sprite, iVec3 color)
    :Position(iVec2(position.x / 128.0f, position.y / 128.0f)), Size(iVec2(size.x / 128.0f, size.y / 128.0f)) {
        this->RenderPosition = position;
        this->RenderSize = size;

        b2BodyDef b2d_BodyDef;
        b2d_BodyDef.type = b2_dynamicBody;
        b2d_BodyDef.position.Set(Position.x, Position.y);
        b2d_Body = World->CreateBody(&b2d_BodyDef);

        b2PolygonShape b2d_BoxShape;
        b2d_BoxShape.SetAsBox(Size.x / 2, Size.y / 2);

        b2FixtureDef b2d_FixtureDef;
        b2d_FixtureDef.shape = &b2d_BoxShape;
        b2d_FixtureDef.density = 1.0f;

        b2d_Fixture = b2d_Body->CreateFixture(&b2d_FixtureDef);
    }

void GameObject::Render(Renderer2D & renderer) {
        float tempPosX = this->b2d_Body->GetPosition().x;
        float tempPosY = this->b2d_Body->GetPosition().y;
        this->RenderPosition = iVec2((tempPosX - Size.x / 2) * 128.0f, (tempPosY - Size.y / 2) * 128.0f);
        this->RenderSize = iVec2(Size.x * 128.0f, Size.y * 128.0f);
        renderer.Render(this->Sprite, this->RenderPosition, this->RenderSize, this->RealRotation, this->Color);
}

1 个答案:

答案 0 :(得分:1)

根据评论中的对话...如果未调用世界var luisrecognizer = new builder.LuisRecognizer(LuisModelUrl); var qnarecognizer = new cognitiveservices.QnAMakerRecognizer({ knowledgeBaseId: process.env.QnAKnowledgebaseId, authKey: process.env.QnAAuthKey || process.env.QnASubscriptionKey, endpointHostName: process.env.QnAEndpointHostName }); var basicQnAMakerDialog = new cognitiveservices.QnAMakerDialog({ recognizers: [qnarecognizer], defaultMessage: 'No match! Try changing the query terms!', qnaThreshold: 0.3 }); bot.recognizer(luisrecognizer); bot.recognizer(basicQnAMakerDialog); bot.dialog('/', basicQnAMakerDialog); bot.dialog('GreetingDialog',[ (session) => { session.send('You reached the Greeting intent. You said \'%s\'.', session.message.text); builder.Prompts.text(session, "What is your name?"); }, (session, results) => { session.userData.name = results.response; session.send("Glad you could make it, " + session.userData.name); builder.Prompts.text(session, "Ask me something!"); }, (session, results) => { session.conversationData.question = results.response; session.send(session.conversationData.question + " is an interesting topic!") session.endDialog(); } ]).triggerAction({ matches: 'Greeting' }) 方法,则对象位置将不会更新。

添加类似的呼叫

Step

将其循环放置,以根据对象的属性(例如速度)更新对象位置。

iforce2d制作了a tutorial on Box2D worlds(包括使用他们的step方法),我推荐给想要进一步了解Box2D World->Step(timeStep, velocityIterations, positionIterations); 类的任何人。

希望这会有所帮助!