Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 翻转后,动态实体卡在静态实体中_C++_Box2d_Game Physics - Fatal编程技术网

C++ 翻转后,动态实体卡在静态实体中

C++ 翻转后,动态实体卡在静态实体中,c++,box2d,game-physics,C++,Box2d,Game Physics,我有一个动态的身体和许多多边形形状为我的游戏角色。为了返回游戏角色,我使用以下代码翻转顶点: void Box2dManager::flipFixtures(bool horizzontally, b2Body* physBody) { b2Fixture* fix = physBody->GetFixtureList(); while(fix) { b2Shape* shape = fix->GetShape(); if(s

我有一个动态的身体和许多多边形形状为我的游戏角色。为了返回游戏角色,我使用以下代码翻转顶点:

void Box2dManager::flipFixtures(bool horizzontally, b2Body* physBody)
{
    b2Fixture* fix = physBody->GetFixtureList();

    while(fix)
    {
        b2Shape* shape = fix->GetShape();
        if(shape->GetType()== b2Shape::e_polygon)
        {   
            // flipping x or y coordinates
            b2PolygonShape* ps = (b2PolygonShape*)shape;
            for(int i=0; i < ps->GetVertexCount(); i++)
                horizzontally ? ps->m_vertices[i].x *= -1 : ps->m_vertices[i].y *= -1;

            // revert the vertices (no need after Box2D 2.3.0 as polygon creation computes the convex hull)
            b2Vec2* reVert = new b2Vec2[ps->GetVertexCount()];
            int j = ps->GetVertexCount() -1;
            for(int i=0; i<ps->GetVertexCount();i++)
                reVert[i] = ps->m_vertices[j--];

            ps->Set(&reVert[0], ps->GetVertexCount());
        }
        fix = fix->GetNext();
    }
}

我也有静态的边缘形状作为墙。当我翻转角色时,有时同一个多边形的顶点出现在同一个静态边形状的不同边上。因此,我的角色粘在墙上,它被困在静态边缘形状中。我应该如何处理这种情况?

非常确定,不支持或至少不建议动态更改形状顶点。尝试删除旧形状,然后使用翻转的顶点创建新形状,并将其指定给实体。这可能会使Box2D意识到这一变化,并希望它通过将主体移动到碰撞之外来解决碰撞。ps->Set&reVert[0],ps->GetVertexCount;与删除所有形状相同,创建具有给定顶点的新形状。我试过使用矩形,而不是边缘形状。如果顶点在身体内部,box2d确实会通过将身体移动到碰撞外部来解决碰撞,但如果顶点位于不同的侧面,则情况也是如此。@LearnCos2D我同意动态更改顶点不是box2d的最佳选择,而是在2D游戏中,有时,你需要角色能够返回。我们应该怎么做?应该有一个共同的方法,对吧?嗯,共同的方法是使两个方向的碰撞形状相同,并且在不可能的情况下,为玩家提供两个形状,甚至两个身体,其中仅为玩家面对的方向启用一个,当转身时,禁用它并激活另一个形状或身体。@learnCos2D如果你说一个长尾巴的恐龙头朝上,尾巴朝下,你不能使两个方向的碰撞形状相同。另一方面,激活或禁用对撞机与移除旧对撞机并创建新对撞机之间有什么区别?