Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "HelloWorldLayer.h"
  2.  
  3. #define PTM_RATIO 32
  4.  
  5.  
  6. @implementation HelloWorldLayer
  7.  
  8. +(CCScene *) scene{
  9.     CCScene *scene = [CCScene node];
  10.    
  11.     HelloWorldLayer *layer = [HelloWorldLayer node];
  12.     [scene addChild: layer];
  13.    
  14.     return scene;
  15. }
  16.  
  17. -(id) init {
  18.  
  19.     if( (self=[super init])) {
  20.        
  21.         CGSize winSize = [CCDirector sharedDirector].winSize;
  22.        
  23.         self.isAccelerometerEnabled = YES;
  24.         self.isTouchEnabled = YES;
  25.        
  26.         // Create sprite and add it to the layer
  27.         _spear = [CCSprite spriteWithFile:@"SPEAR.png" rect:CGRectMake(0, 0, 100, 10)];
  28.         _spear.position = ccp(100, 100);
  29.         [self addChild:_spear];
  30.        
  31.         // Create a world
  32.         b2Vec2 gravity = b2Vec2(0.0f, -20.0f);
  33.         bool doSleep = true;
  34.         _world = new b2World(gravity, doSleep);
  35.        
  36.         // Create edges around the entire screen
  37.         b2BodyDef groundBodyDef;
  38.         groundBodyDef.position.Set(0,0);
  39.         b2Body *groundBody = _world->CreateBody(&groundBodyDef);
  40.         b2PolygonShape groundBox;
  41.         b2FixtureDef boxShapeDef;
  42.         boxShapeDef.shape = &groundBox;
  43.         groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
  44.         groundBody->CreateFixture(&boxShapeDef);
  45.         groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
  46.         groundBody->CreateFixture(&boxShapeDef);
  47.         groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
  48.         groundBody->CreateFixture(&boxShapeDef);
  49.         groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
  50.         groundBody->CreateFixture(&boxShapeDef);
  51.        
  52.        
  53.         // Create spear body and shape
  54.         b2BodyDef spearBodyDef;
  55.         spearBodyDef.type = b2_dynamicBody;
  56.         spearBodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
  57.         spearBodyDef.userData = _spear;
  58.         b2Body *_spearBody = _world->CreateBody(&spearBodyDef);
  59.        
  60.         b2PolygonShape spearShape;
  61.         spearShape.SetAsBox(100/PTM_RATIO, 10/PTM_RATIO);
  62.         b2FixtureDef spearShapeDef;
  63.         spearShapeDef.shape = &spearShape;
  64.         spearShapeDef.density = 1.0f;
  65.         spearShapeDef.friction = 0.9f;
  66.         spearShapeDef.restitution = 0.89f;
  67.        
  68.        
  69.         b2Vec2 force;
  70.         force.Set(_spearBody->GetLinearVelocity().x+5.0f, _spearBody->GetLinearVelocity().y+10.0f);
  71.         _spearBody->SetLinearVelocity(force);
  72.        
  73.         [self schedule:@selector(tick:)];
  74.     }
  75.     return self;
  76. }
  77.  
  78. - (void)tick:(ccTime) dt {
  79.    
  80.     _world->Step(dt, 10, 10);
  81.     for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {    
  82.         if (b->GetUserData() != NULL) {
  83.             CCSprite *ballData = (CCSprite *)b->GetUserData();
  84.             ballData.position = ccp(b->GetPosition().x * PTM_RATIO,
  85.                                     b->GetPosition().y * PTM_RATIO);
  86.             ballData.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
  87.         }        
  88.     }
  89. }
  90.  
  91. - (void) dealloc {
  92.     delete _world;
  93.     _world = NULL;
  94.  
  95.     [super dealloc];
  96. }
  97. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement