Intro to Box2D with Cocos2D 2.X Tutorial: Bouncing Balls

Discussion of the official tutorials published on raywenderlich.com. Please only discuss the official tutorials here - for general questions, use the General Discussion forum instead.

Intro to Box2D with Cocos2D 2.X Tutorial: Bouncing Balls

Postby rwenderlich » Fri Jan 18, 2013 11:00 am

This is the official thread to discuss the following blog post: Intro to Box2D with Cocos2D 2.X Tutorial: Bouncing Balls
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Site Admin
 
Posts: 2269
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 28 times
Been thanked: 382 times

Re: Intro to Box2D with Cocos2D 2.X Tutorial: Bouncing Balls

Postby nicopasso » Tue Jan 29, 2013 3:24 pm

Hi, i'm new to Box2D. This is a great tutorial!!!
But I found this problem:
the article uses the lines
[self setTouchEnabled:YES]; and [self setAccelerometerEnabled:YES]
and XCode tells me tha these methods are "not found".

At the begininng I didn't understand the warnings but then i found that the correct lines for me are:
[self setIsTouchEnabled:YES]; and [self setIsAccelerometerEnabled:YES];
Now the code work properly!

I just want to share this problem with all of you!
Bye
Nic :D
nicopasso
Hacker
 
Posts: 11
Joined: Wed Jun 27, 2012 2:27 pm
Has thanked: 0 time
Been thanked: 1 time

Re: Intro to Box2D with Cocos2D 2.X Tutorial: Bouncing Balls

Postby ptc79 » Tue Feb 26, 2013 3:04 pm

Hi, I'm new to Objective-C, Xcode, Cocos2D and Box2D. My question is do we need to learn C++ in order to use Box2D. The tutorial doesn't make any mention of C++ but the Box2D part is written in C++, right? The files are .mm extension but the code just seems to be mixed into the usual Objective-C functions. So, if you want to use Box2D with an obj-c app do you have to learn C++ first?
ptc79
n00b
 
Posts: 2
Joined: Mon Feb 25, 2013 5:24 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Intro to Box2D with Cocos2D 2.X Tutorial: Bouncing Balls

Postby VitalRemains » Fri Mar 29, 2013 10:56 pm

Hello,
I'm trying to add more balls with a button click.

I wrote this:
Code: Select all
[...]
CCMenuItem *addMenuItem = [CCMenuItemImage itemWithNormalImage:@"ButtonPlus.png" selectedImage:@"ButtonPlusSel.png" target:self selector:@selector(addNewBall)];
        addMenuItem.position = ccp(winSize.width - (addMenuItem.contentSize.width/2), winSize.height - (addMenuItem.contentSize.height/2));
        CCMenu *addMenu = [CCMenu menuWithItems:addMenuItem, nil];
        addMenu.position = CGPointZero;
        [self addChild:addMenu];
[...]

- (void)addNewBall {   
    CCSprite* newBall = [CCSprite spriteWithFile:@"ball.png" rect:CGRectMake(0, 0, 52, 52)];
    newBall.position = ccp(0, 300);
    [self addChild:newBall];
   
    b2Body* newBallBody;
   
    b2BodyDef ballBodyDef;
    ballBodyDef.type = b2_dynamicBody;
    ballBodyDef.position.Set(0/PTM_RATIO, 300/PTM_RATIO);
    ballBodyDef.userData = newBall;
    newBallBody = _world->CreateBody(&ballBodyDef);
   
    b2CircleShape circle;
    circle.m_radius = 26.0/PTM_RATIO;
   
    b2FixtureDef ballShapeDef;
    ballShapeDef.shape = &circle;
    ballShapeDef.density = 1.0f;
    ballShapeDef.friction = 0.2f;
    ballShapeDef.restitution = 0.8f;
    newBallBody->CreateFixture(&ballShapeDef);
}


But it won't add any new balls if I press on the button.
Do I have to tell the program that there's a new body it has to deal with or anything if it's not created in the init method?

~VitalRemains
VitalRemains
n00b
 
Posts: 1
Joined: Fri Mar 29, 2013 10:50 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Intro to Box2D with Cocos2D 2.X Tutorial: Bouncing Balls

Postby minhtam » Tue Jul 09, 2013 4:46 pm

I'm doing the game which you tile the phone to control the ball roll on to the table. I tried to simulate the ball look like the steel ball in the real life but it wasn't successful as i expected. In the test, i changed 3 things.
1. GRAVITY - use accelerator by gravity ratio
2. LINEAR DAMPING of the ball
3. SENSITIVE for accelerator - i don't need tile 100% the phone to archive fully gravity. Instead of, just tile phone 40% (maybe not important)

Finally, i found some number to make it look like real (about 70% likely, i hope so ^^). But the problem appeared. I choose the GRAVITY about 50, Leaner Damping of the ball about 5 so that when the ball hit the wall, it isn't bounce again :(

Who can help me to fix this problem or change something to look like great ?!? I attached my project to testing in this email if anyone need that
minhtam
n00b
 
Posts: 3
Joined: Sat Jun 08, 2013 3:58 pm
Has thanked: 0 time
Been thanked: 0 time

Postby aqueelmirza » Sun Aug 04, 2013 12:35 pm

Hi
Thanks for writing such a nice tutorial. It really helped me in learning box2D. Currently I am creating a game in box2d and have come across a strange problem in it. Can you please help me in fixing it. I have posted it here http://stackoverflow.com/questions/1804 ... its-torque
Thanks and Best Regards
aqueelmirza
n00b
 
Posts: 2
Joined: Sun Aug 04, 2013 12:33 pm
Has thanked: 0 time
Been thanked: 0 time

Postby sagarsandy » Thu Oct 24, 2013 5:38 am

hi, i am new to box2d. i created a world with gravity -10. i know all the bodies by default adopt this gravity while creating their body, but i want to set gravity for a particular body to 0. how to set it?
and i gone through various blogs, found a method "SetGravity" but in my project i didn't found that method. so plz help me how to set that, i'm using cocos2d1.0..
sagarsandy
n00b
 
Posts: 4
Joined: Thu Oct 24, 2013 5:32 am
Has thanked: 0 time
Been thanked: 0 time

Postby rabio » Fri Nov 01, 2013 5:24 pm

hi, am new to BOX2D, i ve tried this tuto it looks good but i ve found a little problem
when i let the ball bounce till it stops at the bottom right corner (that was my test), then i tllt the device the ball stays fixed at this corner

can u tell me why plzzzz???
rabio
n00b
 
Posts: 1
Joined: Fri Nov 01, 2013 5:20 pm
Has thanked: 0 time
Been thanked: 0 time

how to create sprite body with edges..

Postby sagarsandy » Fri Nov 15, 2013 6:11 am

hi,

i want to create body for my sprite(50*50 size). the body should be like rectangle with top open..
i have already used "setasbox" method. but as i told it should be like a bucket, which means with top open.
so i want to create it using setasedge methods, but i'm not getting the exact dimensions for my sprite(50*50 size).
plzzz suggest me how to create it. it should be like this.. |___|
sagarsandy
n00b
 
Posts: 4
Joined: Thu Oct 24, 2013 5:32 am
Has thanked: 0 time
Been thanked: 0 time


Return to Official Tutorials

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yahoo [Bot] and 14 guests