Hello, i am new to box2d and i want to write a little program to learn it better. The problem in this code is:
when i try to create different bodies with -(void)createTowerBox2dObject:(CCSprite*)sprite:(CGSize)size they get created(i checked the bodies attached to the world) my player can just interact with one tower(the first one created) but not with the others, they don't even get drawn by the debug method.
Thanks for help
joni8a
Code: Select all
#import "GameScene.h"
#import "Box2D.h"
#import "Player.h"
#define PTM_RATIO 32.f
@implementation GameScene
{
}
@synthesize levelMap;
@synthesize towerLayer;
@synthesize towerObject;
@synthesize towerBodys;
@synthesize world;
@synthesize heli;
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
GameScene *gameScene = [GameScene node];
// add layer as a child to scene
[scene addChild: gameScene];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init]))
{
towerBodys = [[NSMutableArray alloc]init];
levelMap = [CCTMXTiledMap tiledMapWithTMXFile:@"Level1.tmx"];
towerLayer = [levelMap layerNamed:@"Tower"];
towerObject = [levelMap objectGroupNamed:@"TowerObject"];
b2Vec2 gravity = b2Vec2(0.0f, -10.0f);
BOOL doSleep = true;
world = new b2World(gravity, doSleep);
world->SetContinuousPhysics(true);
NSLog(@"INIT");
m_debugDraw = new GLESDebugDraw( PTM_RATIO );
world->SetDebugDraw(m_debugDraw);
uint32 flags = 0;
flags += b2DebugDraw::e_shapeBit;
// flags += b2DebugDraw::e_jointBit;
// flags += b2DebugDraw::e_aabbBit;
// flags += b2DebugDraw::e_pairBit;
// flags += b2DebugDraw::e_centerOfMassBit;
m_debugDraw->SetFlags(flags);
for (NSMutableDictionary* dicTower in [towerObject objects])
{
CCSprite* tower = [[CCSprite alloc]init];
float x = [[dicTower valueForKey:@"x"]intValue];
float y =[[dicTower valueForKey:@"y"]intValue];
tower.position = ccp(x,y);
CGSize size;
size.width = [[dicTower valueForKey:@"width"]floatValue];
size.height = [[dicTower valueForKey:@"height"]floatValue];
[towerBodys addObject:tower];
[self createTowerBox2dObject:tower :size];
}
Player* player = [Player createPlayer];
player.position = ccp(100,300);
[self createPlayerBox2DObject:player];
[self addChild:levelMap z:-1];
[self addChild:player];
[self createBoxWindow:[CCDirector sharedDirector].winSize];
self.isTouchEnabled = YES;
self.isAccelerometerEnabled = YES;
}
[self schedule:@selector(update:) interval:(1/30)];
b2Body* tower;
b2BodyDef towerBodyDef;
towerBodyDef.type = b2_staticBody;
towerBodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
tower=world->CreateBody(&towerBodyDef);
b2FixtureDef towerFixtureDef;
b2PolygonShape shape;
towerFixtureDef.shape = &shape;
shape.SetAsBox(80/PTM_RATIO/2, 80/PTM_RATIO/2);
tower->CreateFixture(&towerFixtureDef);
return self;
}
-(void)createTowerBox2dObject:(CCSprite*)sprite:(CGSize)size
{
b2BodyDef towerBodyDef;
towerBodyDef.type = b2_staticBody;
towerBodyDef.position.Set(sprite.position.x+(size.width/2)/PTM_RATIO, sprite.position.y+(size.height/2)/PTM_RATIO);
towerBodyDef.userData = sprite;
b2Body* tower=world->CreateBody(&towerBodyDef);
b2FixtureDef towerFixtureDef;
b2PolygonShape shape;
towerFixtureDef.shape = &shape;
shape.SetAsBox(size.width/PTM_RATIO/2, size.height/PTM_RATIO/2);
tower->CreateFixture(&towerFixtureDef);
}
-(void)createPlayerBox2DObject:(CCSprite*)sprite
{
b2BodyDef heliBodyDef;
heliBodyDef.type = b2_dynamicBody;
heliBodyDef.position.Set(sprite.position.x/PTM_RATIO, sprite.position.y/PTM_RATIO);
heliBodyDef.userData = sprite;
heli = world->CreateBody(&heliBodyDef);
b2FixtureDef heliFixtureDef;
b2PolygonShape shape;
heliFixtureDef.shape = &shape;
shape.SetAsBox(float(sprite.contentSize.width/PTM_RATIO/2), float(sprite.contentSize.height/PTM_RATIO/2));
heliFixtureDef.restitution = 0.1f;
heli->CreateFixture(&heliFixtureDef);
}
-(void)createBoxWindow:(CGSize)winSize
{
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
b2Body *groundBody = world->CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
b2FixtureDef boxShapeDef;
boxShapeDef.shape = &groundBox;
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO),
b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO,
winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
}
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
b2Vec2 impulse = b2Vec2(0.0f, 10.0f);
heli->ApplyLinearImpulse(impulse, heli->GetWorldCenter());
}
-(void) draw
{
// Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
// Needed states: GL_VERTEX_ARRAY,
// Unneeded states: GL_TEXTURE_2D, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
world->DrawDebugData();
// restore default GL states
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
-(void)update:(ccTime)dt
{
world->Step(dt, 10, 10);
for (b2Body* body = world->GetBodyList(); body; body = body->GetNext())
{
if (body->GetUserData() != NULL)
{
CCSprite* sprite = (CCSprite*)body->GetUserData();
sprite.position = ccp(body->GetPosition().x * PTM_RATIO, body->GetPosition().y * PTM_RATIO);
}
}
}
-(void)dealloc
{
delete world;
world = NULL;
[super dealloc];
}
@end
when i try to create different bodies with -(void)createTowerBox2dObject:(CCSprite*)sprite:(CGSize)size they get created(i checked the bodies attached to the world) my player can just interact with one tower(the first one created) but not with the others, they don't even get drawn by the debug method.
Thanks for help
joni8a