Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.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++ Box2d SDL无法在动态实体上应用世界设置_C++_Sdl_Box2d - Fatal编程技术网

C++ Box2d SDL无法在动态实体上应用世界设置

C++ Box2d SDL无法在动态实体上应用世界设置,c++,sdl,box2d,C++,Sdl,Box2d,我充满活力的身体在创造世界后不会倒下。它不遵循重力。即使我在身体上施力,它也不起作用 b2Vec2 gravity(0.0f, -9.8f); b2World world(gravity); //PineCone class PineCone { private: //Surface box SDL_Rect box; //Real position float rX, rY; //Velocity

我充满活力的身体在创造世界后不会倒下。它不遵循重力。即使我在身体上施力,它也不起作用

b2Vec2 gravity(0.0f, -9.8f);
b2World world(gravity);

//PineCone
class PineCone
{
    private:
        //Surface box
        SDL_Rect box;

        //Real position
        float rX, rY;

        //Velocity
        float xVel, yVel;

        //Physics world
        b2World* world;

        //Body
        b2Body* pBodyPineCone;

    public:
        //Specify
        void specify(int w, int h, SDL_Surface* source, SDL_Surface* dest, b2World* world);

        //Input
        void handle_input();

        //Motion
        void updatepos();
        //void checkStanding(SDL_Rect env[], int N_SOLIDS);

        //Render
        void show();
};

void PineCone::specify(int w, int h, SDL_Surface* source, SDL_Surface* dest, b2World* world)
{

    //Source
    pPineCone = source;

    //Destination
    pScreen = dest;

    // Define the dynamic pinecone. We set its position and call the body factory.
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;  

    // Define a ball shape for our dynamic body.
    b2CircleShape dynamicPineCone;
    dynamicPineCone.m_p.Set(0, 0); //position, relative to body position
    dynamicPineCone.m_radius = 0.5; //radius

    // Define the dynamic body fixture.
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &dynamicPineCone;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.3f;

    // Add the shape to the body.
    bodyDef.position.Set(0, 20);
    pBodyPineCone = world->CreateBody(&bodyDef);
    pBodyPineCone->CreateFixture(&fixtureDef);
}

void PineCone::show()
{
    int blit = SDL_BlitSurface( pPineCone, NULL, pScreen, &box );

    if( blit < 0 )
    {
        exit(1);
    }
}
b2Vec2重力(0.0f,-9.8f);
b2世界(重力);
//松果
松果类
{
私人:
//表面盒
SDL矩形盒;
//实际位置
浮动rX,rY;
//速度
浮动xVel,yVel;
//物理世界
b2World*世界;
//身体
b2Body*pBodyPineCone;
公众:
//指明
void指定(int w、int h、SDL_曲面*源、SDL_曲面*目标、b2World*世界);
//输入
无效句柄_输入();
//动议
void updatepos();
//无效检查(SDL_Rect env[],内部实体);
//渲染
void show();
};
void PineCone::指定(int w、int h、SDL_曲面*源、SDL_曲面*目标、b2World*世界)
{
//来源
pPineCone=来源;
//目的地
psscreen=dest;
//定义动态松果体。我们设置其位置并调用实体工厂。
b2BodyDef-bodyDef;
bodyDef.type=b2_dynamicBody;
//为我们的动态身体定义一个球的形状。
b2圆形动态单体;
dynamicPineCone.m_p.Set(0,0);//位置,相对于主体位置
dynamicPineCone.m_半径=0.5;//半径
//定义动态实体夹具。
b2FixtureDef fixtureDef;
fixtureDef.shape=&dynamicPineCone;
固定件密度=1.0f;
固定件摩擦力=0.3f;
//将形状添加到主体中。
车身定义位置设置(0,20);
pBodyPineCone=world->CreateBody(&bodyDef);
pBodyPineCone->CreateFixture(&fixtureDef);
}
void PineCone::show()
{
int blit=SDL_BlitSurface(pPineOne、NULL、pScreen和box);
如果(blit<0)
{
出口(1);
}
}
我不确定我是否错过了一些主要的东西

int main(int argc, char *argv[])
{
    B2_NOT_USED(argc);
    B2_NOT_USED(argv);

    // Prepare for simulation. Typically we use a time step of 1/60 of a
    // second (60Hz) and 10 iterations. This provides a high quality simulation
    // in most game scenarios.
    float32 timeStep = 1.0f / 60.0f;
    int32 velocityIterations = 6;
    int32 positionIterations = 2;

    // Start the timer
    g_dwStartTicks = SDL_GetTicks();


    init();
    load_files();

    //PineCone
    PineCone myPineCone;
    myPineCone.specify(g_iPineConeSize, g_iPineConeSize, pPineCone, pScreen, &world);

    //
    // The Main Game Loop
    //

    do
    {
        // Record the current time
        g_dwStartTime = SDL_GetTicks();

        //
        // Handle they keyboard events here
        //
        while (SDL_PollEvent(&event) > 0)
        {
            if (event.type == SDL_QUIT)
            {
                // Quit event! (Window close, kill signal, etc.)
                g_iLoopDone = TRUE;
            }
        }

        world.Step(timeStep, velocityIterations, positionIterations);

        myPineCone.handle_input();

        if (g_bPlaying || g_bEnd)
        {    
            //Show pinecone
            myPineCone.show();
        }

        // Update the display
        SDL_Flip(pScreen);

        g_dwEndTime = SDL_GetTicks();
        if (g_dwEndTime < g_dwStartTime + (1000 / 60))
        {
            SDL_Delay(g_dwStartTime + (1000 / 60) - g_dwEndTime);
        }
    }while (!g_iLoopDone);

    //Clean Up
    clean_up();

    return 0;
}
intmain(intargc,char*argv[])
{
B2_未使用(argc);
B2_未使用(argv);
//为模拟做准备。通常我们使用1/60秒的时间步长
//第二次(60Hz)和10次迭代。这提供了高质量的模拟
//在大多数游戏场景中。
浮动32时间步长=1.0f/60.0f;
int32速度迭代=6;
int32=2;
//启动计时器
g_dwStartTicks=SDL_GetTicks();
init();
加载_文件();
//松果
松果;
myPineCone。指定(g_iPineConeSize、g_iPineConeSize、PineCone、pScreen和world);
//
//主游戏循环
//
做
{
//记录当前时间
g_dwStartTime=SDL_GetTicks();
//
//他们在这里处理键盘事件
//
而(SDL_PollEvent(&event)>0)
{
if(event.type==SDL\u QUIT)
{
//退出事件!(窗口关闭、终止信号等)
g_iLoopDone=TRUE;
}
}
世界步(时间步、速度迭代、位置迭代);
myPineCone.handle_input();
if(g|u铺设| g|u弯曲)
{    
//展示松果
myPineCone.show();
}
//更新显示
SDL_翻转(屏幕);
g_dwEndTime=SDL_GetTicks();
if(g_dwEndTime
例如,我可以认为物理自我正在更新,而我们没有可视化
精灵(图像)不更新其物理实体的相应值。
如果图像不移动,请尝试为其指定Box2D主体位置,否则不移动
必须是物理学的更新。
试试这个:
无效设置1B2SPRITEPOSITION(点位置)
{
pBodyPineCone->SetAwake(真);
pBodyPineCone->设置位置(位置);
//更新box2d
pBodyPineCone->SetTransform(
b2Vec2(位置x/PTM_比率,位置y/PTM_比率),
_body->GetAngle());
}
void updatePhysics()
{
_世界->步骤(.1,10,10);
对于(b2Body*b=_world->GetBodyList();b;b=b->GetNext())
{
如果(b->GetUserData()!=NULL)
{
Sprite*ballData=dynamic_cast((Sprite*)b->GetUserData());
ballData->setPosition(b->GetPosition().x*PTM_比率,
b->GetPosition().y*PTM_比率);
ballData->setRotation(-1*CC_弧度到_度(b->GetAngle());
}
}
}

您需要找出问题所在。这是太多的代码了,任何人都无法为你解析。谢谢你,布兰德。它现在只关注box2d代码。你能看一下吗?再次感谢!是否在更新中更新box2d世界
I can think for example that the physical self is updating're not visualizing
what the Sprite (Image) not update the corresponding value of its physical body.
Try to give it a Box2D body position if the image does not move you do not
have to be the update of physics.

Try this:

void setL1b2SpritePosition(Point position)
{
    pBodyPineCone->SetAwake (true);
    pBodyPineCone->setPosition(position);

    //update box2d
    pBodyPineCone->SetTransform( 
            b2Vec2(position.x / PTM_RATIO, position.y/ PTM_RATIO),
            _body->GetAngle());
}


void updatePhysics()
{
    _world->Step(.1, 10, 10);
    for (b2Body *b = _world->GetBodyList(); b; b = b->GetNext())
    {
        if (b->GetUserData() != NULL)
        {
            Sprite *ballData = dynamic_cast<Sprite*> ((Sprite *) b->GetUserData());
            ballData->setPosition(b->GetPosition().x * PTM_RATIO,
                              b->GetPosition().y * PTM_RATIO);
            ballData->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));
        }
    }
}