-
-
Save seivan/736909 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation Game | |
@synthesize st; | |
- (id)initWithWidth:(float)width height:(float)height { | |
if (self = [super initWithWidth:width height:height]) { | |
// this is where the code of your game will start. | |
// in this sample, we add just a simple quad to see if it works. | |
st = [[SPSprite alloc] init]; | |
st.rotation = SP_D2R(90); | |
st.x = 320; | |
st.stage.width = 480; | |
st.stage.height = 320; | |
[self addChild:st]; | |
[self createPhysicsWorld]; | |
for (int i = 0; i != 100; i++) { | |
SPQuad *sq = [[[SPQuad alloc] init] autorelease]; | |
sq.height = 10.0f; | |
sq.width = 10.0f; | |
sq.x = 480/2.0f; | |
sq.y = 20.0f; | |
sq.color = 0xff0000; | |
[st addChild:sq]; | |
[self addPhysicalBodyForView:sq]; | |
} | |
tickTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES]; | |
//[self addEventListener:@selector(onEnterFrame:) atObject:self | |
// forType:SP_EVENT_TYPE_ENTER_FRAME]; | |
} | |
return self; | |
} | |
-(void)createPhysicsWorld { | |
b2Vec2 gravity; | |
gravity.Set(0.0f, -9.81f); | |
bool doSleep = true; | |
world = new b2World(gravity, doSleep); | |
world->SetContinuousPhysics(true); | |
b2BodyDef groundBodyDef; | |
groundBodyDef.position.Set(0, 0); // bottom-left corner | |
b2Body* groundBody = world->CreateBody(&groundBodyDef); | |
b2EdgeShape groundBox; | |
groundBox.Set(b2Vec2(0,0), b2Vec2(480.0f/PTM_RATIO,0)); | |
groundBody->CreateFixture(&groundBox, 0); | |
} | |
-(void)addPhysicalBodyForView:(SPDisplayObject*)physicalView | |
{ | |
b2BodyDef bodyDef; | |
bodyDef.type = b2_dynamicBody; | |
CGFloat centerX = physicalView.width/2.0 + physicalView.x; | |
CGFloat centerY = physicalView.height/2.0 + physicalView.y; | |
CGPoint boxDimensions = CGPointMake(physicalView.width/PTM_RATIO/2.0,physicalView.height/PTM_RATIO/2.0); | |
bodyDef.position.Set(centerX/PTM_RATIO, (480.0f -centerY)/PTM_RATIO); | |
bodyDef.userData = physicalView; | |
b2Body *body = world->CreateBody(&bodyDef); | |
b2PolygonShape dynamicBox; | |
dynamicBox.SetAsBox(boxDimensions.x, boxDimensions.y); | |
b2FixtureDef fixtureDef; | |
fixtureDef.shape = &dynamicBox; | |
fixtureDef.density = 3.0f; | |
fixtureDef.friction = 0.3f; | |
fixtureDef.restitution = 0.5f; // 0 is a lead ball, 1 is a super bouncy ball | |
body->CreateFixture(&fixtureDef); | |
body->SetType(b2_dynamicBody); | |
} | |
-(void) tick:(NSTimer *)timer | |
{ | |
//It is recommended that a fixed time step is used with Box2D for stability | |
//of the simulation, however, we are using a variable time step here. | |
//You need to make an informed choice, the following URL is useful | |
//<a href="http://gafferongames.com/game-physics/fix-your-timestep/">http://gafferongames.com/game-physics/fix-your-timestep/</a> | |
int32 velocityIterations = 8; | |
int32 positionIterations = 1; | |
// Instruct the world to perform a single step of simulation. It is | |
// generally best to keep the time step and iterations fixed. | |
world->Step(1.0f/60.0f, velocityIterations, positionIterations); | |
//Iterate over the bodies in the physics world | |
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) | |
{ | |
if (b->GetUserData() != NULL) | |
{ | |
SPDisplayObject *oneView = (SPDisplayObject *)b->GetUserData(); | |
// y Position subtracted because of flipped coordinate system | |
CGPoint newCenter = CGPointMake(b->GetPosition().x * PTM_RATIO, | |
self.height - b->GetPosition().y * PTM_RATIO); | |
//oneView.center = newCenter; | |
oneView.x = newCenter.x; | |
oneView.y = newCenter.y; | |
oneView.rotation = SP_D2R(- b->GetAngle()); | |
// CGAffineTransform transform = CGAffineTransformMakeRotation(- b->GetAngle()); | |
// | |
// oneView.transform = transform; | |
} | |
} | |
} | |
-(void)dealloc { | |
self.st = nil; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment