Tag Archives: GetUserData

How to move a body manually in Box2D? or Give a force to a body in Box2D, iPhone.

By | April 28, 2012

This is a sample code to move a body in Box2D . First you have to make a body with variable name “moving_rec” and call the below function in a schedular at regular intervals. -(void) moveBody{ b2Vec2 force = b2Vec2(0,0); force = b2Vec2(0,3); //Giving the x an y to negative will move the body in opposite direction. moving_rec->SetLinearVelocity(force);… Read More »

Simulate a Balloon in Box2D, iPhone or ANDROID

By | April 12, 2012

Hello……… Sometimes you may want your body to act against the gravity of the world. One method to do this is described below. First what you have to do is to make a body in the shape of a circle and give it the image of a balloon as the userdata. Then in the tick function write this… Read More »

Create a polygon in Flash or Adobe AIR using Box2D.

By | February 5, 2011

The below function creates a polygon in the shape of a triangle. Please call the debugdraw function inside your update method to view the results. Make sure you import the Box2D classes into your file. public function init():void { debug_draw(); addEventListener(Event.ENTER_FRAME, update); createTriangle(); } public function createTriangle():void { var fd : b2FixtureDef = new b2FixtureDef(); var initVel :… Read More »

How to implement a bomb explosion in Box2D iphone?

By | January 30, 2011

Below code is a simplest way to do that. [java] -(void) explodeBomb { b2Vec2 force = b2Vec2(0.25,0.70); // give the direction for the force. bombBody->ApplyLinearImpulse(force, bombBodyDef.position); CCSprite *sp = (CCSprite *) bombBody->GetUserData(); sp.opacity = 0; // setting the sprite in the body opacity to zero. [self schedule:@selector(destroyBomb) interval:2]; //destroying bombBody after 2 sec. } -(void) destroyBomb { CCLOG(@"Destroying… Read More »

How to check whether you have touched a body in Box2D iPhone?

By | January 30, 2011

You can check it by using the TestPoint function available for the Fixtures. [xml] – (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *myTouch = [touches anyObject]; CGPoint location = [myTouch locationInView:[myTouch view]]; location = [[CCDirector sharedDirector] convertToGL:location]; b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO); for (b2Body *b = world->GetBodyList(); b; b = b->GetNext()) { b2Fixture *f = b->GetFixtureList(); CCSprite *sprite =(CCSprite… Read More »