0
votes

I need to throw my box2d object till off screen.

For example... Rabbit is moving straight on Jungle Path (Road length around 500 metres). In between it got some power to apply, like Axe. So rabbit need to throw that object forward till the off screen. If any bouncable object (like wall and tree) in midway, thrown object need to come back, else it should go to off screen and hide.

At touch event I called below method to create body and for movement I used setlinear velocity.. but it's not moving straight and smooth; then inbetween if any objects (like wall and tree). How to bounce back (reverse travel)?

[self createbody];


-(void) createbody
{
    freeBodySprite = [CCSprite spriteWithFile:@"blocks.png"];//web_ani_6_1
    //freeBodySprite.position = ccp(100, 300);
    [self addChild:freeBodySprite z:2 tag:6];

    CGPoint startPos = CGPointMake(100, 320/1.25);

    b2BodyDef bodyDef;

    bodyDef.type = b2_staticBody;
    bodyDef.position = [self toMeters:startPos];
    bodyDef.userData = freeBodySprite;

    b2CircleShape shape;

    float radiusInMeters = ((freeBodySprite.contentSize.width * freeBodySprite.scale/PTM_RATIO) * 0.5f);
    shape.m_radius = radiusInMeters;

    b2FixtureDef fixtureDef;
    fixtureDef.shape = &shape;
    fixtureDef.density = 0.07f;
    fixtureDef.friction = 0.1f;
    fixtureDef.restitution = 0.1f;
    b2Fixture *stoneFixture;
    circularObstacleBody = world->CreateBody(&bodyDef);
    stoneFixture = circularObstacleBody->CreateFixture(&fixtureDef);
    freeBody = circularObstacleBody;

}
-(b2Vec2) toMeters:(CGPoint)point
{
    return b2Vec2(point.x / PTM_RATIO, point.y / PTM_RATIO);
}

I am bit confused. How to achieve the above requirement ?

I refer this link:- Box2d object throwing smoother and on same velocity

3

3 Answers

1
votes

First, In your Code, u didnt show your movement code(where u used that Linear Velocity)

b2vec2 *move = (30,0);
cart->SetLinearVelocity(move);

above code will move your box2d body with 30 velocity in x axis. when ever any object occurs in middle of road. u need to handle those in below methods

void LevelContactListener::BeginContact(b2Contact* contact)
void LevelContactListener::EndContact(b2Contact* contact)

u will get Fixtures from above method. based on that handle the collisons.(i.e.,) your box2d body(player power) and those road objects collided. set the power to reverse direction (-velocity).

0
votes

You can set friction to 0 and body will nicely bouncing from walls.

b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
b2Body * body = _world->CreateBody(&bodyDef);

b2CircleShape circle;
circle.m_radius = 26.0/PTM_RATIO;

b2FixtureDef ShapeDef;
ShapeDef.shape = &circle;
ShapeDef.density = 1.0f;
ShapeDef.friction = 0.f;
ShapeDef.restitution = 1.0f;
b2Fixture *bodyFixture = body->CreateFixture(&ShapeDef);

To create walls you need create ground body for example:

    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0,0);
    _groundBody = _world->CreateBody(&groundBodyDef);

    b2EdgeShape groundBox;
    b2FixtureDef groundBoxDef;
    groundBoxDef.shape = &groundBox;

    groundBox.Set(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
    _bottomFixture = _groundBody->CreateFixture(&groundBoxDef);

    groundBox.Set(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
    _groundBody->CreateFixture(&groundBoxDef);

    groundBox.Set(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 
    winSize.height/PTM_RATIO));
    _groundBody->CreateFixture(&groundBoxDef);

    groundBox.Set(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), 
    b2Vec2(winSize.width/PTM_RATIO, 0));
    _groundBody->CreateFixture(&groundBoxDef);
0
votes

You may add distance joint that will pull object back to the initial position upon its first contact with other collider (see contact listener for details).

I think it is possible to have persistent joint connected your player with throwable object. You just have to handle joint limits (distance) and behaviour (spring and damper).