Skip to content

Commit

Permalink
added Box2D physics
Browse files Browse the repository at this point in the history
  • Loading branch information
haqu committed Jun 11, 2011
1 parent 5b9ec90 commit 5d57de2
Show file tree
Hide file tree
Showing 87 changed files with 16,836 additions and 95 deletions.
315 changes: 301 additions & 14 deletions Game.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions Game/AppDelegate.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
//
// Tiny Wings http://github.com/haqu/tiny-wings
//
/*
* Tiny Wings remake
* http://github.com/haqu/tiny-wings
*
* Created by Sergey Tikhonov http://haqu.net
* Released under the MIT License
*
*/

#import <UIKit/UIKit.h>

Expand Down
12 changes: 9 additions & 3 deletions Game/AppDelegate.m → Game/AppDelegate.mm
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
//
// Tiny Wings http://github.com/haqu/tiny-wings
//
/*
* Tiny Wings remake
* http://github.com/haqu/tiny-wings
*
* Created by Sergey Tikhonov http://haqu.net
* Released under the MIT License
*
*/

#import "cocos2d.h"

Expand Down Expand Up @@ -86,6 +91,7 @@ - (void) applicationDidFinishLaunching:(UIApplication*)application

[director setAnimationInterval:1.0/60];
[director setDisplayFPS:NO];
[director setDepthTest:NO];


// make the OpenGLView a child of the view controller
Expand Down
11 changes: 8 additions & 3 deletions Game/GameConfig.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
//
// Tiny Wings http://github.com/haqu/tiny-wings
//
/*
* Tiny Wings remake
* http://github.com/haqu/tiny-wings
*
* Created by Sergey Tikhonov http://haqu.net
* Released under the MIT License
*
*/

#ifndef __GAME_CONFIG_H
#define __GAME_CONFIG_H
Expand Down
17 changes: 14 additions & 3 deletions Game/HelloWorldLayer.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
//
// Tiny Wings http://github.com/haqu/tiny-wings
//
/*
* Tiny Wings remake
* http://github.com/haqu/tiny-wings
*
* Created by Sergey Tikhonov http://haqu.net
* Released under the MIT License
*
*/

// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
#import "Box2D.h"

@class Terrain;
@class Hero;

// HelloWorldLayer
@interface HelloWorldLayer : CCLayer
{
float screenW;
float screenH;
b2World *world;
CCSprite *background_;
Terrain *terrain_;
Hero *hero_;
BOOL tapDown;
}
@property (nonatomic, retain) CCSprite *background;
@property (nonatomic, retain) Terrain *terrain;
@property (nonatomic, retain) Hero *hero;

// returns a CCScene that contains the HelloWorldLayer as the only child
+ (CCScene*) scene;
Expand Down
82 changes: 70 additions & 12 deletions Game/HelloWorldLayer.m → Game/HelloWorldLayer.mm
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
//
// Tiny Wings http://github.com/haqu/tiny-wings
//
/*
* Tiny Wings remake
* http://github.com/haqu/tiny-wings
*
* Created by Sergey Tikhonov http://haqu.net
* Released under the MIT License
*
*/

// Import the interfaces
#import "HelloWorldLayer.h"
#import "Terrain.h"
#import "Hero.h"

// HelloWorldLayer implementation
@implementation HelloWorldLayer

@synthesize background = background_;
@synthesize terrain = terrain_;
@synthesize hero = hero_;

+ (CCScene*) scene
{
Expand All @@ -30,16 +37,16 @@ + (CCScene*) scene
- (CCSprite*) generateBackground {

int textureSize = 512;

ccColor3B c = (ccColor3B){140,205,221};

CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:textureSize height:textureSize];
[rt beginWithClear:(float)c.r/256.0f g:(float)c.g/256.0f b:(float)c.b/256.0f a:1];

// layer 1: gradient

float gradientAlpha = 0.5f;

glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

Expand All @@ -62,7 +69,7 @@ - (CCSprite*) generateBackground {

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);

// layer 2: noise

CCSprite *s = [CCSprite spriteWithFile:@"noise.png"];
Expand All @@ -76,28 +83,51 @@ - (CCSprite*) generateBackground {
return [CCSprite spriteWithTexture:rt.sprite.texture];
}

- (void) createBox2DWorld {

b2Vec2 gravity;
gravity.Set(0.0f, -9.8f);

world = new b2World(gravity, true);
world->SetContinuousPhysics(true);

// debugDraw = new GLESDebugDraw(PTM_RATIO);
// world->SetDebugDraw(debugDraw);

// uint32 flags = 0;
// flags += b2DebugDraw::e_shapeBit;
// debugDraw->SetFlags(flags);

}

// 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])) {

// ask director the the window size
CGSize size = [[CCDirector sharedDirector] winSize];
screenW = size.width;
screenH = size.height;

[self createBox2DWorld];

self.background = [self generateBackground];
background_.position = ccp(screenW/2,screenH/2);
ccTexParams tp = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
[background_.texture setTexParameters:&tp];
[self addChild:background_];
self.terrain = [Terrain new];

self.terrain = [Terrain terrainWithWorld:world];
[self addChild:terrain_];

self.hero = [Hero heroWithWorld:world];
[terrain_ addChild:hero_];

self.isTouchEnabled = YES;
tapDown = NO;

[self scheduleUpdate];
}
Expand All @@ -110,6 +140,9 @@ - (void) dealloc
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
delete world;
world = NULL;

self.background = nil;
self.terrain = nil;

Expand All @@ -124,13 +157,38 @@ - (void) registerWithTouchDispatcher {
- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];

// [terrain_ toggleScrolling];

[terrain_ toggleScrolling];
tapDown = YES;

return YES;
}

- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];

tapDown = NO;
}

- (void) update:(ccTime)dt {

if (tapDown) {
[hero_ run];
} else {
[hero_ walk];
}

int32 velocityIterations = 8;
int32 positionIterations = 1;

world->Step(dt, velocityIterations, positionIterations);

[hero_ updatePosition];

terrain_.offsetX = hero_.position.x - screenW/4;

CGSize size = background_.textureRect.size;
background_.textureRect = CGRectMake(terrain_.offsetX*0.2f, 0, size.width, size.height);
}
Expand Down
27 changes: 27 additions & 0 deletions Game/Hero.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Tiny Wings remake
* http://github.com/haqu/tiny-wings
*
* Created by Sergey Tikhonov http://haqu.net
* Released under the MIT License
*
*/

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Box2D.h"

@interface Hero : CCNode {
b2World *world;
b2Body *body;
float radius;
}

+ (id) heroWithWorld:(b2World*)w;
- (id) initWithWorld:(b2World*)w;

- (void) updatePosition;
- (void) walk;
- (void) run;

@end
68 changes: 68 additions & 0 deletions Game/Hero.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Tiny Wings remake
* http://github.com/haqu/tiny-wings
*
* Created by Sergey Tikhonov http://haqu.net
* Released under the MIT License
*
*/

#import "Hero.h"
#import "Box2D.h"

@implementation Hero

+ (id) heroWithWorld:(b2World*)w {
return [[[self alloc] initWithWorld:w] autorelease];
}

- (id) initWithWorld:(b2World*)w {
if ((self = [super init])) {
world = w;
radius = 16.0f;

// create box2d body

CGSize size = [[CCDirector sharedDirector] winSize];
int screenW = size.width;
int screenH = size.height;

b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position.Set(screenW/4/PTM_RATIO, screenH/2/PTM_RATIO);
body = world->CreateBody(&bd);

b2CircleShape shape;
shape.m_radius = radius/PTM_RATIO;

b2FixtureDef fd;
fd.shape = &shape;
fd.density = 1.0f;
fd.restitution = 0; // 0 - no bounce, 1 - perfect bounce
fd.friction = 1000.0f;

body->CreateFixture(&fd);

}
return self;
}

- (void) draw {
glColor4f(0.25f, 0.25f, 1.0f, 1.0f);
glLineWidth(2);
ccDrawCircle(ccp(0,0), radius, body->GetAngle(), 16, YES);
}

- (void) updatePosition {
self.position = ccp(body->GetPosition().x*PTM_RATIO, body->GetPosition().y*PTM_RATIO);
}

- (void) walk {
body->ApplyTorque(-1);
}

- (void) run {
body->ApplyTorque(-7);
}

@end
19 changes: 19 additions & 0 deletions Game/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2011 Sergey Tikhonov http://haqu.net

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
11 changes: 8 additions & 3 deletions Game/RootViewController.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
//
// Tiny Wings http://github.com/haqu/tiny-wings
//
/*
* Tiny Wings remake
* http://github.com/haqu/tiny-wings
*
* Created by Sergey Tikhonov http://haqu.net
* Released under the MIT License
*
*/

#import <UIKit/UIKit.h>

Expand Down
11 changes: 8 additions & 3 deletions Game/RootViewController.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
//
// Tiny Wings http://github.com/haqu/tiny-wings
//
/*
* Tiny Wings remake
* http://github.com/haqu/tiny-wings
*
* Created by Sergey Tikhonov http://haqu.net
* Released under the MIT License
*
*/

//
// RootViewController + iAd
Expand Down
Loading

0 comments on commit 5d57de2

Please sign in to comment.