开发者

box2d:Disable bounce on collision

开发者 https://www.devze.com 2023-01-19 08:32 出处:网络
I have some box,creating using box2d,which restitution\'s are set to zero.but when they fall over one another there appear bounce event.but i don\'t want that...i want them not move when fall over ano

I have some box,creating using box2d,which restitution's are set to zero.but when they fall over one another there appear bounce event.but i don't want that...i want them not move when fall over another.it can be done if i switch off gravity.but i also want gravity.here is my code

UIImage *imageOfSnowV1 = [ UIImage imageNamed:[NSString stringWithFormat:@"Object%d.png",currentlySelected]];
开发者_运维百科    CCTexture2D  *texOfSnowV1 = [[ [CCTexture2D alloc] initWithImage:imageOfSnowV1 ] autorelease];
    CCSprite *sprite = [CCSprite spriteWithTexture:texOfSnowV1  rect:CGRectMake(0, 0, 32, 32)];
    [self addChild:sprite];
    sprite.position = ccp(p.x, p.y);
    sprite.tag=[temp intValue];


    // Define the dynamic body.
    //Set up a 1m squared box in the physics world

    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;

    bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
    bodyDef.userData = sprite;
    b2Body *bodyS = world->CreateBody(&bodyDef);

    // Define another box shape for our dynamic body.

    b2PolygonShape dynamicBox;
    dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box




    b2MassData massData;
    massData.mass = 0.1f;
        bodyS->SetMassData(&massData);


    // Define the dynamic body fixture.
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &dynamicBox; 
    fixtureDef.density = 50.0f;
    fixtureDef.restitution=0.0f;
    fixtureDef.friction = 0.01f;


    bodyS->CreateFixture(&fixtureDef);

can anyone help me?


As I remember box2d by default uses maximal restitution of colliding objects so even if you have dynamic body restitution set to 0 if the static body have any larger than 0 then that restitution will be used for collision, you could modify b2MixRestitution function to meet your needs.

Cheers, Krzysztof Zabłocki


I've been having the same problem lately. My solution, just zero-out the Y coordinate when you detect a new collision in your contact listener. It does the trick perfectly here.


you have to increase velocityIterations and positionIterations. If the bodies are fast they will overlap. so you need better calculation. depends on bodies count you might get perfomance issues, just play with this values.

int32 velocityIterations = 10;
int32 positionIterations = 8;
world->Step( timeStep, velocityIterations, positionIterations ); 
0

精彩评论

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