Category Archives: Box2D

How to find your Google Plus ID

This is so simple

1. Go to your Google + account (https://plus.google.com/).

2. Click on the Profile icon on the Left.

3. If you look at the URL in the address bar, it should look something like this:

https://plus.google.com/104653270154306099169/posts

4. The long numerical string in the URL is your Google+ ID. Here is CoderzHeaven’s from the URL above:

104653270154306099169/

Google + CoderzHeaven

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

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); //set Linear velocity for moving in a constant speed.
}

Please leave your comments on this post.

Collision detection in Box2D

Hi,

Collision can be detected simply using the following lines of code with box2D.

if((contact.fixtureA == aimFixture && contact.fixtureB == tarFixture) ||
     (contact.fixtureA == tarFixture&& contact.fixtureB == aimFixture ))
     {
        NSLog(@"Collision between aim fixture and target fixture detected!");
     }

Here aimFixture & tarFixture are two custom fixtures whose collision you want to find out.
:)

Using of CCWaves in iPhone

CCWaves is one of the interesting functionalities in Cocos2D iphone.
Check out this following snippet to see how it works.

id my_wavesAction = [CCWaves actionWithWaves:7 amplitude:25 horizontal:NO
                                                               vertical:YES grid:ccg(15,10) duration:10];
[my_sprite runAction: [CCRepeatForever actionWithAction:my_wavesAction n]];

Please leave your valuable comments on this post………………….

Simulate a Balloon in Box2D, iPhone or ANDROID

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 code.
This method of applying the force is called “The antagonist forces method”
By this method we are giving an upward force to only our balloon body irrespective of the gravitational force.
After running the code you will see your body floating in air like a balloon.

for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
   {
        if (b->GetUserData() != NULL) {
               if (b==baloonBody) {
                         b->ApplyForce( b2Vec2(0.0,9.8*b->GetMass()),b->GetWorldCenter()); // here 0.0 is x, 9.8 is y (the gravity)
               }
         }
   }

Note: Try to change the value 9.8 to higher if you have given your world’s gravity as 9.8. It will produce a balloon effect.
Please leave your comments if this post was useful……….

Creating Menu in Iphone Cocos2D or Box2D?

Use CCMenuItemSprite and CCMenu in iPhone to create menu.

-(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 *go_back2 = [CCSprite spriteWithFile:@"back2.png"];
	 go_back2.opacity = 100;

	 CCMenuItemSprite  *top_menuSprite1 = [CCMenuItemSprite itemFromNormalSprite:Home1 selectedSprite:Home2 target:self selector:@selector(goHome)];
	 CCMenuItemSprite  *top_menuSprite2 = [CCMenuItemSprite itemFromNormalSprite:Levels1 selectedSprite:Levels2 target:self selector:@selector(goToLevelSelection)];
	 CCMenuItemSprite  *top_menuSprite3 = [CCMenuItemSprite itemFromNormalSprite:Refresh1 selectedSprite:Refresh2 target:self selector:@selector(reloadGame)];
	 CCMenuItemSprite  *top_menuSprite4 = [CCMenuItemSprite itemFromNormalSprite:go_back1 selectedSprite:go_back2 target:self selector:@selector(menuGoBack)];
	 top_menu = [CCMenu menuWithItems:top_menuSprite1,top_menuSprite2, top_menuSprite3 ,top_menuSprite4, nil];
	 [top_menu  alignItemsVerticallyWithPadding:10.0f];
	 top_menu.position = ccp(240,160);

	 [self addChild:top_menu z:2];
}

call this function to set up the menu for your home page in your iPhone game or you can change this code directly to ANDROID and it will work.
Make sure you have all the images mentioned in the above example.
This menu will appear on the centre of the screen in Landscape mode.

Angry Birds Again, Angry Birds Space, the new Angry Birds coming.

We know Angry Birds have conquered almost all tablets and now the completely new edition is launching this March.
Acoording to the reports the new game will be launched on March.

Angry Birds Space already has an official website with an image of a slingshot on the Moon overlooking Earth.


The official blog spot says that

Angry Birds Space is a “completely new game with innovative new gameplay, but with some of the familiar Angry Birds elements that fans already know and love.”

Check out the official Blog site here.

Destroying Box2D Physics Bodies in Corona SDK

Hi,

For destroying physics bodies in Corona SDK, use any of the following methods.

urBody:removeSelf()

OR

urBody.parent:remove( urBody )

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 once. This situation could occur when deleting objects involved in collisions, which can potentially have many event phases before the collision is fully resolved. The solution is simple:

local function onCollision( self, event )
        if ( event.phase == "began" ) then

                -- Check if body still exists before removing!
                if ( urBody ) then
                        urBody:removeSelf()
                        urBody = nil
                end

        end
end

:)

How to Show Cocos2D Particle Effects Repeatedly ?

Hi,

For showing particle effects repeatedly in Cocos2D, use the sample code.

CCParticleSystemQuad *yourParticleEffect = [CCParticleSystemQuad particleWithFile:@"yourEffect.plist"];

[self addChild:yourParticleEffect z:5];

This will show the effect ‘yourEffect’ repeatedly in your game. :)

How to open URL from iPhone application ?

Hi,

For opening a Web Link/URL from an iPhone application, follow the two simple steps.
Step 1 :
Create an NSURL object and initialize it with your URL string

 NSURL *myURL = [NSURL URLWithString:@"http://www.coderzheaven.com/"];

;

Step 2 :
Use the openURL method by passing your NSURL object

[[UIApplication sharedApplication] openURL: myURL];

Done. :)

How to know whether your b2Body is moving or not in Box2D ?

Hi,

Some times while game building via Box2D Physics Engine, you may need to find out whether your bodies (from b2Body) are moving or not. Use the following sample of code to find out whether the given b2Body is moving or not.


b2Body *urBody;

if(urBody->IsAwake()
{
        //urBbody is moving! It's not in rest!
}

else
{
        //urBody is at rest!
}

:)

How to Fix ‘Warning : Multiple Build Commands For Output File' ?

Hi,

The following warning, ‘Multiple Build Commands For Output File’ occurs when you try to delete resource files by ‘delete reference’ other than the correct method of‘move to trash’ while deleting files from ‘Resources’ folder of your project. Therefore the Copy Bundle Resources still holds the references of the same file you have deleted and it shows the multiple build commands for the output file after you added the same files over again.

How to Fix? :

Go to the Targets->YourProject->Copy Bundle Resources

From ‘Copy Bundle Resources’ delete those files whose reference are still there or files which shows the warning.

Here ‘YourProject’ is your project name.
:)

How to remove duplicates from NSMutableArray ?

Hi,

For removing or deleting duplicates from a NSMutableArray , use the following lines of code. It will simply check for duplicates elements and get it deleted or removed.


NSArray *tempArr= [urArray copy];

NSInteger idx = [tempArr count] - 1;

for (id object in [tempArr reverseObjectEnumerator]) {

     if ([urArray indexOfObject:object inRange:NSMakeRange(0, index)] !=NSNotFound) {

           [urArray removeObjectAtIndex:idx];
     }

idx--;

}
[tempArr release];

:)

How to set iPhone/iPad table view transparent ?

Hi,

In order to simply set the iPhone/iPad table view transparent use the following line of code.

urTblView.backgroundColor = [UIColor clearColor];

Here ‘urTblView’ is the table view that you want to make transparent. Try it out. This single line will make your table view transparent and looks better.
:)

How to change z order value of a sprite in Cocos2D ?

Hi,

Sometimes during the execution of program you may need to change the z order value of sprite for better working. Use the following sample of code to change the z order value of a sprite in Cocos2D/Box2D iPhone programming.

CCSprite *urSprite = [CCSprite spriteWithFile:@"coderzheavenLogo.png"];
urSprite.position = ccp(240,160);
//First setting the z value to 1
[self addChild:urSprite z:1];
.
.
.
.
.
//After some other executions are over setting z value to 7
[self reorderChild:urSprite z:7];

:)

How to make a sprite visible and invisible in Cocos2D ?

Hi,

In order to make a sprite completely invisible , use the following code while using Cocos2D, this code will set the visibility of your sprite to zero.

urSprite.visible = 0; 

In order to make a sprite completely visible, use the following code while using Cocos2D, this code will set the visibility of the sprite back to one.

urSprite.visible = 1; 

:)