Tag Archives: Box2D

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 »

Collision detection in Box2D

By | April 22, 2012

Hi, Collision can be detected simply using the following lines of code with box2D. [c] if((contact.fixtureA == aimFixture && contact.fixtureB == tarFixture) || (contact.fixtureA == tarFixture&& contact.fixtureB == aimFixture )) { NSLog(@"Collision between aim fixture and target fixture detected!"); } [/c] Here aimFixture & tarFixture are two custom fixtures whose collision you want to find out. 🙂

How to add sprite to a body in Box2D in Adobe AIR, FLEX and FLASH? Or Add image/Sprite to a body in ActionScript.

By | April 12, 2012

The below code is for FlashBuilder 4, If you are using FlexBuilder3 use addChild() instead of the commented block in the following code. [java] public function addCircle(){x:Number, y: Number, radius:Number, ballcip:Ball):void{ bd:b2BodyDef = new b2BodyDef(); var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, imageFailed); var request:URLRequest = new URLRequest("ball.png"); loader.load(request); /*optionally change the position of the image if not fitting… Read More »

Creating Menu in Iphone Cocos2D or Box2D?

By | April 3, 2012

Use CCMenuItemSprite and CCMenu in iPhone to create menu. [java] -(void) setUpMenu { CCSprite *Home1 = [CCSprite spriteWithFile:@"Home1.png"]; CCSprite *Home2 = [CCSprite spriteWithFile:@"Home1.png"]; Home2.opacity = 100; CCSprite *Levels1 = [CCSprite spriteWithFile:@"levels2.png"]; CCSprite *Levels2 = [CCSprite spriteWithFile:@"levels2.png"]; Levels2.opacity = 100; CCSprite *Refresh1 = [CCSprite spriteWithFile:@"refresh.png"]; CCSprite *Refresh2 = [CCSprite spriteWithFile:@"refresh.png"]; Refresh2.opacity = 100; CCSprite *go_back1 = [CCSprite spriteWithFile:@"back2.png"]; CCSprite… Read More »

Creating Menu in Iphone Cocos2D or Box2D?

By | April 3, 2012

Use CCMenuItemSprite and CCMenu in iPhone to create menu. [java] -(void) setUpMenu { CCSprite *Home1 = [CCSprite spriteWithFile:@"Home1.png"]; CCSprite *Home2 = [CCSprite spriteWithFile:@"Home1.png"]; Home2.opacity = 100; CCSprite *Levels1 = [CCSprite spriteWithFile:@"levels2.png"]; CCSprite *Levels2 = [CCSprite spriteWithFile:@"levels2.png"]; Levels2.opacity = 100; CCSprite *Refresh1 = [CCSprite spriteWithFile:@"refresh.png"]; CCSprite *Refresh2 = [CCSprite spriteWithFile:@"refresh.png"]; Refresh2.opacity = 100; CCSprite *go_back1 = [CCSprite spriteWithFile:@"back2.png"]; CCSprite… Read More »

Creating Menu in Iphone Cocos2D or Box2D?

By | April 3, 2012

Use CCMenuItemSprite and CCMenu in iPhone to create menu. [java] -(void) setUpMenu { CCSprite *Home1 = [CCSprite spriteWithFile:@"Home1.png"]; CCSprite *Home2 = [CCSprite spriteWithFile:@"Home1.png"]; Home2.opacity = 100; CCSprite *Levels1 = [CCSprite spriteWithFile:@"levels2.png"]; CCSprite *Levels2 = [CCSprite spriteWithFile:@"levels2.png"]; Levels2.opacity = 100; CCSprite *Refresh1 = [CCSprite spriteWithFile:@"refresh.png"]; CCSprite *Refresh2 = [CCSprite spriteWithFile:@"refresh.png"]; Refresh2.opacity = 100; CCSprite *go_back1 = [CCSprite spriteWithFile:@"back2.png"]; CCSprite… Read More »

Creating Menu in Iphone Cocos2D or Box2D?

By | April 3, 2012

Use CCMenuItemSprite and CCMenu in iPhone to create menu. [java] -(void) setUpMenu { CCSprite *Home1 = [CCSprite spriteWithFile:@"Home1.png"]; CCSprite *Home2 = [CCSprite spriteWithFile:@"Home1.png"]; Home2.opacity = 100; CCSprite *Levels1 = [CCSprite spriteWithFile:@"levels2.png"]; CCSprite *Levels2 = [CCSprite spriteWithFile:@"levels2.png"]; Levels2.opacity = 100; CCSprite *Refresh1 = [CCSprite spriteWithFile:@"refresh.png"]; CCSprite *Refresh2 = [CCSprite spriteWithFile:@"refresh.png"]; Refresh2.opacity = 100; CCSprite *go_back1 = [CCSprite spriteWithFile:@"back2.png"]; CCSprite… Read More »

Destroying Box2D Physics Bodies in Corona SDK

By | August 28, 2011

Hi, For destroying physics bodies in Corona SDK, use any of the following methods. [c] urBody:removeSelf() [/c] OR [c]urBody.parent:remove( urBody )[/c] Note : While Box2D objects will be safely retained until the end of the current world step, their Lua references will be deleted immediately. Therefore, be careful not to accidentally delete the same Lua object more than… Read More »

How to detect single/double/triple taps in iPhone ?

By | April 30, 2011

Hi, How can you detect single/double/triple taps in iPhone using Objective C? Use the following code to detect single or multiple taps in iPhone/iPad/iPod. The code can also be used with Cocos2D & Box2D. [c] -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *urtouch = [touches anyObject]; NSUInteger urtapCount = [urtouch tapCount]; switch (urtapCount) { case 1: NSLog(@"Single Tap Detected!");… Read More »

Identifying sprite associated with a particular body in Box2D

By | April 23, 2011

Hi, How can you identify the sprite associated with a particular body while using box2D ? Sometimes you may need to identify the sprite associated with the body you are using. For that use the following line of code. [c]CCSprite *yourSprite= (CCSprite*)yourBody->GetUserData(); [/c] Now you got the corresponding sprite in your ‘yourSprite’ 🙂

How to detect shake in iPhone ? An example

By | April 13, 2011

Hi, Sometimes you may want to detect shake gesture in iPhone using your programs. But how can you detect shake gesture in iPhone? Shake gesture in iPhone can be detected by using the accelerometer. The following lines of code can be used for detecting the shake gesture in iPhone. You can use these lines of code in your… Read More »

How to remove box2D body from world ?

By | April 10, 2011

Hi, If you are a game developer and you are using box2D for real world simulations, you would probably need a particular body to be removed from the box2D world! But how can you remove box2D body from world ? See the following sample code which does the same. – (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { //Getting Touch Locations… Read More »

How to make gear joint in Box2D, iPhone?

By | March 7, 2011

Gear joints are one of the real specialities of Box2D. By creating gear joints between two bodies you can actually simulate a real world gear. For creating a gear joint you need two bodies connected by a revolute or prismatic joint. The below code creates two bodies in the shape of a circle which is actually attached to… Read More »

Collision Detection In Box2D – Using With Cocos2D

By | March 5, 2011

For an interactive game building using Box2D, collision detection of Box2D bodies is necessary. There is an easy way to implement collision detection in Box2D. For checking whether two bodies have collided with each other in Box2D while using it with cocos2D, then use the following lines of code where ever you want to check collision. if((contact.fixtureA ==… 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 Box2D in Adobe AIR?

By | February 4, 2011

Everyone will be fascinated how flash games are built on the web that implements the real world physics. Well for your information there are a lot of physics engines available. One of them is the Box2D. Box2D was written in C++. Then it was converted to flash. So now I am going to show you how to build… 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 »

How to shoot a bullet in the direction of touch in Box2D iphone?

By | January 30, 2011

write the below code in “ccTouchesBegan” function and you are done. Here the ballBody represents the bullet body. You can increase the power variable to increase bullet speed. [java] UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:[touch view]]; location = [[CCDirector sharedDirector] convertToGL:location]; CGPoint shootVector = ccpSub(location, ball.position); CGFloat shootAngle = ccpToAngle(shootVector); CGPoint normalizedShootVector = ccpNormalize(shootVector);… Read More »