2012年1月31日火曜日

Box2Dの作り方

作り方
Worldをつくる。 (パラメータ:重力,スリープ)
スプライトをつくる
BodyDefを設定。(パラメータ:タイプ(静的か動的か),位置 ,スプライト)
Bodyをつくる。 Body = world->CreateBody(&BodyDef);
Shapeの設定。 円m_radios 線SetAsEdge 箱SetAsBox
FixtureDefの設定。 (パラメータ:Shape, 密度(質量), 跳ね返り, 摩擦)
Fixtureを作る。 Fixture = Body->CreateFixture(&ShapeDef);
単位換算
リリース



#import "Box2D.h"
b2World *_world;
b2Body *_groundBody;
b2Fixture *_bottomFixture;
b2Fixture *_ballFixture;

#define PTM_RATIO 32

CGSize winSize = [CCDirector sharedDirector].winSize;

// Create a world
b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
bool doSleep = true;
_world = new b2World(gravity, doSleep);

//ルームの作成
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
_groundBody = _world->CreateBody(&groundBodyDef);

b2PolygonShape groundBox;
b2FixtureDef groundBoxDef;
groundBoxDef.shape = &groundBox;

//下面
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
_bottomFixture = _groundBody->CreateFixture(&groundBoxDef);
//左面
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
_groundBody->CreateFixture(&groundBoxDef);
//上面
groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO,
winSize.height/PTM_RATIO));
_groundBody->CreateFixture(&groundBoxDef);
//右面
groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO),
b2Vec2(winSize.width/PTM_RATIO, 0));
_groundBody->CreateFixture(&groundBoxDef);

//スプライト作成
CCSprite *ball = [CCSprite spriteWithFile:@"Ball.jpg"
rect:CGRectMake(0, 0, 52, 52)];
ball.position = ccp(100, 100);
ball.tag = 1;
[self addChild:ball];

// ボディの作成 (スプライトを載せる→ワールドへ実装)
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
ballBodyDef.userData = ball;
b2Body * ballBody = _world->CreateBody(&ballBodyDef);

//形の作成
b2CircleShape circle;
circle.m_radius = 26.0/PTM_RATIO;

//Fixtureの作成(ボディーと形を統合して載せる)
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);

//cocos2dとbox2dの単位換算
[self schedule:@selector(tick:)];
- (void)tick:(ccTime) dt {
_world->Step(dt, 10, 10);
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *sprite = (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)dealloc {

delete _world;
_groundBody = NULL;
[super dealloc];

}

2011年12月30日金曜日

スクリーンのサイズを取得

CGSize size=[[CCDirector shareDirector]winSize];

タイルマップ、オブジェクトの作り方


CCTMXObjectGroup *ob =[ map objectGroupNamed:@"oj"];
NSMutableDictionary *sp =[ob objectNamed:@"StartPoint"];
int x = [[sp valueForKey:@"x"]intValue];
int y = [[sp valueForKey:@"y"]intValue];

あとはスプライトにccp(x,y)を指定するだけ

タイルマップの作り方

CCTMXTiledMap *map;
CCTMXLayer *bgLayer;

@property (nonatomic,retain)CCTMXTiledMap *map;
@property (nonatomic,retain)CCTMXLayer *bgLayer;

@synthesize map;
@synthesize bgLayer;

self.map = [CCTMXTiledMap tiledMapWithTMXFile:@"theMap.tmx"];
self.bgLayer = [map layerNameed:@"bg"];

[self addChilde:map z:-1];




self.map = nil;
self.bgLayer = nil;

ラベルの作り方

CCLabel *label
label=[CCLabel labelWithString:@"HelloWorld" forName:@"Marker Felt" fontSize:24];
[self add:label];

[label setString:@"Hello world"];
label.color = ccc3(0,0,0);

衝突判定

アップデートのクラスの中で
if(CGRectIntersectsRect([sprite1 boundingBox],[sprite2 boundingBox])){

}

データの保存

書き込み
NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
[ud setObject:data forKey:@"dataKey"];
[ud synchronize];

読み込み
NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
NSString *theString=[ud objectForKey:@"theStringKey"];