How To Create A Breakout Game with Box2D and Cocos2D 2.X Tutorial: Part 1
5 posts
• Page 1 of 1
How To Create A Breakout Game with Box2D and Cocos2D 2.X Tutorial: Part 1
This is the official thread to discuss the following blog post: How To Create A Breakout Game with Box2D and Cocos2D 2.X Tutorial: Part 1
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
-

rwenderlich - Site Admin
- Posts: 1776
- Joined: Thu Dec 23, 2010 4:14 pm
- Has thanked: 26 times
- Been thanked: 187 times
Re: How To Create A Breakout Game with Box2D and Cocos2D 2.X
Dear Sir,
Thanks for your nice tutorials,
I am working on this breakout with ARC enabled i faced a few problems I solved some but when it gets to touches began
The project runs fine but as soon I click it gives error and takes me direct to GetMass
It highlights the (return m_mass) in b2Body header file
If you can help me with this
Thanks for your nice tutorials,
I am working on this breakout with ARC enabled i faced a few problems I solved some but when it gets to touches began
The project runs fine but as soon I click it gives error and takes me direct to GetMass
It highlights the (return m_mass) in b2Body header file
If you can help me with this
- khalid
- n00b
- Posts: 2
- Joined: Sun Jan 13, 2013 6:28 pm
- Has thanked: 0 time
- Been thanked: 0 time
Re: How To Create A Breakout Game with Box2D and Cocos2D 2.X
I'll admit this is not something I looked into when updating the tutorials. I know that there are some issues with ARC and Cocos2d, and there is a tutorial on getting Cocos to work with ARC here http://www.raywenderlich.com/23854/arc-and-cocos2d-v2-x. You might want to start there if you haven't already.
I'm not sure if Box2D being in c++ causes any issues with ARC.
I'm not sure if Box2D being in c++ causes any issues with ARC.
- bcbroom
- iOS Tutorial Team Member
- Posts: 6
- Joined: Wed May 16, 2012 1:00 am
- Has thanked: 0 time
- Been thanked: 1 time
Re: How To Create A Breakout Game with Box2D and Cocos2D 2.X
here i am going to paste all the code which is in Hello world layer some i do have if you please have a quick look at this. i wanted to upload the project but the max size for the file which is allowed is 256KB so i was not able to upload the project.
but if you want to have a quick look to the project i can email it to you but i don't have your email address here is my email
( afridikhalid@yahoo.com )
// Import the interfaces
#import "HelloWorldLayer.h"
// Needed to obtain the Navigation Controller
#import "AppDelegate.h"
#import "PhysicsSprite.h"
#pragma mark - HelloWorldLayer
@interface HelloWorldLayer()
@end
@implementation HelloWorldLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id) init
{
if( (self=[super init])) {
// enable events
CGSize winSize = [CCDirector sharedDirector].winSize;
self.isTouchEnabled = YES;
// Create a world
b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
_world = new b2World(gravity);
// Create edges around the entire screen
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);
// Create sprite and add it to the layer
CCSprite *ball = [CCSprite spriteWithFile:@"ball.png"];
ball.position = ccp(100, 100);
ball.tag = 1;
[self addChild:ball];
// Create ball body
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
b2Body *ballBody = _world->CreateBody(&ballBodyDef);
ballBody->SetUserData((__bridge void*)ball);
// Create circle shape
b2CircleShape circle;
circle.m_radius = 26.0 /PTM_RATIO;
// Create shape definition and add to body
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &circle;
ballShapeDef.density = 1.0f;
ballShapeDef.friction = 0.f;
ballShapeDef.restitution = 1.0f;
_ballFixture = ballBody->CreateFixture(&ballShapeDef);
b2Vec2 force = b2Vec2(10,10);
ballBody->ApplyLinearImpulse(force, ballBodyDef.position);
// Create paddle and add it to the layer
CCSprite *paddle = [CCSprite spriteWithFile:@"paddle.png"];
paddle.position = ccp(winSize.width /2, 50);
[self addChild:paddle];
// Create paddle body
b2BodyDef paddleBodyDef;
paddleBodyDef.type = b2_dynamicBody;
paddleBodyDef.position.Set(winSize.width/2/PTM_RATIO, 50/PTM_RATIO);
b2Body *paddleBody = _world->CreateBody(&paddleBodyDef);
paddleBody->SetUserData((__bridge void*)paddle);
// Create paddle shape
b2PolygonShape paddleShape;
paddleShape.SetAsBox(paddle.contentSize.width/PTM_RATIO/2, paddle.contentSize.height/PTM_RATIO/2);
// Create shape definition and add to body
b2FixtureDef paddleShapeDef;
paddleShapeDef.shape = &paddleShape;
paddleShapeDef.density = 10.0f;
paddleShapeDef.friction = 0.4f;
paddleShapeDef.restitution = 0.1f;
_paddleFixture = paddleBody->CreateFixture(&paddleShapeDef);
[self schedule:@selector(tick:)];
}
return self;
}
-(void) dealloc
{
delete _world;
_world = NULL;
}
-(void)tick:(ccTime) dt {
_world->Step(dt, 10, 10);
for (b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *sprite = (__bridge CCSprite *)b->GetUserData();
sprite.position = ccp(b->GetPosition().x * PTM_RATIO, b->GetPosition().y *PTM_RATIO);
sprite.rotation = -1 *CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_mouseJoint != NULL) return;
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);
if (_paddleFixture->TestPoint(locationWorld)) {
b2MouseJointDef md;
md.bodyA = _groundBody;
md.bodyB = _paddleBody;
md.target = locationWorld;
md.collideConnected = true;
md.maxForce = 1000.0f *_paddleBody->GetMass();
_mouseJoint = (b2MouseJoint*)_world->CreateJoint(&md);
_paddleBody->SetAwake(true);
}
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_mouseJoint == NULL) return;
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);
_mouseJoint->SetTarget(locationWorld);
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_mouseJoint) {
_world->DestroyJoint(_mouseJoint);
_mouseJoint = NULL;
}
}
-(void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_mouseJoint) {
_world->DestroyJoint(_mouseJoint);
_mouseJoint = NULL;
}
}
but if you want to have a quick look to the project i can email it to you but i don't have your email address here is my email
( afridikhalid@yahoo.com )
// Import the interfaces
#import "HelloWorldLayer.h"
// Needed to obtain the Navigation Controller
#import "AppDelegate.h"
#import "PhysicsSprite.h"
#pragma mark - HelloWorldLayer
@interface HelloWorldLayer()
@end
@implementation HelloWorldLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id) init
{
if( (self=[super init])) {
// enable events
CGSize winSize = [CCDirector sharedDirector].winSize;
self.isTouchEnabled = YES;
// Create a world
b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
_world = new b2World(gravity);
// Create edges around the entire screen
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);
// Create sprite and add it to the layer
CCSprite *ball = [CCSprite spriteWithFile:@"ball.png"];
ball.position = ccp(100, 100);
ball.tag = 1;
[self addChild:ball];
// Create ball body
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
b2Body *ballBody = _world->CreateBody(&ballBodyDef);
ballBody->SetUserData((__bridge void*)ball);
// Create circle shape
b2CircleShape circle;
circle.m_radius = 26.0 /PTM_RATIO;
// Create shape definition and add to body
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &circle;
ballShapeDef.density = 1.0f;
ballShapeDef.friction = 0.f;
ballShapeDef.restitution = 1.0f;
_ballFixture = ballBody->CreateFixture(&ballShapeDef);
b2Vec2 force = b2Vec2(10,10);
ballBody->ApplyLinearImpulse(force, ballBodyDef.position);
// Create paddle and add it to the layer
CCSprite *paddle = [CCSprite spriteWithFile:@"paddle.png"];
paddle.position = ccp(winSize.width /2, 50);
[self addChild:paddle];
// Create paddle body
b2BodyDef paddleBodyDef;
paddleBodyDef.type = b2_dynamicBody;
paddleBodyDef.position.Set(winSize.width/2/PTM_RATIO, 50/PTM_RATIO);
b2Body *paddleBody = _world->CreateBody(&paddleBodyDef);
paddleBody->SetUserData((__bridge void*)paddle);
// Create paddle shape
b2PolygonShape paddleShape;
paddleShape.SetAsBox(paddle.contentSize.width/PTM_RATIO/2, paddle.contentSize.height/PTM_RATIO/2);
// Create shape definition and add to body
b2FixtureDef paddleShapeDef;
paddleShapeDef.shape = &paddleShape;
paddleShapeDef.density = 10.0f;
paddleShapeDef.friction = 0.4f;
paddleShapeDef.restitution = 0.1f;
_paddleFixture = paddleBody->CreateFixture(&paddleShapeDef);
[self schedule:@selector(tick:)];
}
return self;
}
-(void) dealloc
{
delete _world;
_world = NULL;
}
-(void)tick:(ccTime) dt {
_world->Step(dt, 10, 10);
for (b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *sprite = (__bridge CCSprite *)b->GetUserData();
sprite.position = ccp(b->GetPosition().x * PTM_RATIO, b->GetPosition().y *PTM_RATIO);
sprite.rotation = -1 *CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_mouseJoint != NULL) return;
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);
if (_paddleFixture->TestPoint(locationWorld)) {
b2MouseJointDef md;
md.bodyA = _groundBody;
md.bodyB = _paddleBody;
md.target = locationWorld;
md.collideConnected = true;
md.maxForce = 1000.0f *_paddleBody->GetMass();
_mouseJoint = (b2MouseJoint*)_world->CreateJoint(&md);
_paddleBody->SetAwake(true);
}
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_mouseJoint == NULL) return;
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);
_mouseJoint->SetTarget(locationWorld);
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_mouseJoint) {
_world->DestroyJoint(_mouseJoint);
_mouseJoint = NULL;
}
}
-(void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_mouseJoint) {
_world->DestroyJoint(_mouseJoint);
_mouseJoint = NULL;
}
}
- khalid
- n00b
- Posts: 2
- Joined: Sun Jan 13, 2013 6:28 pm
- Has thanked: 0 time
- Been thanked: 0 time
Re: How To Create A Breakout Game with Box2D and Cocos2D 2.X
Hi, nice tutorial!! The Box2D world is amazing!!!
But I've a problem
in ccTouchesBegan method the _paddleFixture->TestPoint check returns always false...
I don't understand why...can you help me? thanks
EDIT:
I'm an idiot...
I wrote the code of ccTouchesMoved in ccTouhcesCancelled and viceversa.
Now all works perfect!!!
But I've a problem
in ccTouchesBegan method the _paddleFixture->TestPoint check returns always false...
I don't understand why...can you help me? thanks
EDIT:
I'm an idiot...
I wrote the code of ccTouchesMoved in ccTouhcesCancelled and viceversa.
Now all works perfect!!!
5 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 5 guests