开发者

simply apply gravity again and again

开发者 https://www.devze.com 2023-03-14 18:04 出处:网络
my code applying gravity on any body in my world, and i have a ball , that after it falls down by gravity, i need it to come up again and fall by gravity again.

my code applying gravity on any body in my world, and i have a ball , that after it falls down by gravity, i need it to come up again and fall by gravity again. so it falls down , BUT when i put it back up , it wont fall down again. WHY ??

I call this every frame ::

-(void)thick:(ccTime) dt
{


    world->Step(dt,10,10);
    for(b2Body *b=world->GetBodyList(); b; b=b->GetNext()) 
    {
        if(b->GetUserData() !=NULL )
           {
            开发者_C百科   CCSprite *bondanind1=(CCSprite *) b->GetUserData();
               bondanind1.position=ccp( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO  ) ;
               //bondanind.rotation=-1*CC_RADIANS_TO_DEGREES(b->GetAngle());
           }

    }

    if(bondanind.position.y<0 )
    bondanind.position=ccp(300,300);


}

so the ball comes up and stay there . doesnt the gravity works on any body, all time, every frame ????

thanks a lot


It's obvious. You should change the position of the body in your condition, not of sprite. And change the condition too look like this:

if (body->GetPosition().y < 0)
{
    b2Vec2 newPos(...); //put your pos
    body->SetTransform(newPos, body->GetAngle());
}

Now your body just continues falling after it's y position value is less then zero.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号