OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 #include "PhysicsLayer.h" |
| 5 #include "physics_nodes/CCPhysicsSprite.h" |
| 6 |
| 7 // Pixels-to-meters ratio for converting screen coordinates |
| 8 // to Box2D "meters". |
| 9 #define PTM_RATIO 32 |
| 10 #define SCREEN_TO_WORLD(n) ((n) / PTM_RATIO) |
| 11 #define WORLD_TO_SCREEN(n) ((n) * PTM_RATIO) |
| 12 #define VELOCITY_ITERATIONS 8 |
| 13 #define POS_ITERATIONS 1 |
| 14 |
| 15 #define SPRITE_BATCH_NODE_TAG 99 |
| 16 #define MAX_SPRITES 100 |
| 17 |
| 18 USING_NS_CC_EXT; |
| 19 |
| 20 bool PhysicsLayer::init() { |
| 21 if (!CCLayerColor::initWithColor(ccc4(0,0x8F,0xD8,0xD8))) |
| 22 return false; |
| 23 |
| 24 setTouchEnabled(true); |
| 25 InitPhysics(); |
| 26 |
| 27 CCSpriteBatchNode* batch; |
| 28 batch = CCSpriteBatchNode::create("blocks.png", MAX_SPRITES); |
| 29 addChild(batch, 0, SPRITE_BATCH_NODE_TAG); |
| 30 |
| 31 // cache texture pointer for creating sprites |
| 32 sprite_texture_ = batch->getTexture(); |
| 33 |
| 34 // script physics updates each frame |
| 35 schedule(schedule_selector(PhysicsLayer::UpdateWorld)); |
| 36 return true; |
| 37 } |
| 38 |
| 39 PhysicsLayer::~PhysicsLayer() { |
| 40 delete world_; |
| 41 } |
| 42 |
| 43 bool PhysicsLayer::InitPhysics() { |
| 44 b2Vec2 gravity(0.0f, -9.8f); |
| 45 world_ = new b2World(gravity); |
| 46 world_->SetAllowSleeping(true); |
| 47 world_->SetContinuousPhysics(true); |
| 48 |
| 49 // create the ground |
| 50 b2BodyDef groundBodyDef; |
| 51 groundBodyDef.position.Set(0, 0); |
| 52 b2Body* groundBody = world_->CreateBody(&groundBodyDef); |
| 53 |
| 54 CCSize win_size = CCDirector::sharedDirector()->getWinSize(); |
| 55 int world_width = SCREEN_TO_WORLD(win_size.width); |
| 56 int world_height = SCREEN_TO_WORLD(win_size.height); |
| 57 |
| 58 // Define the ground box shape. |
| 59 b2EdgeShape groundBox; |
| 60 // bottom |
| 61 groundBox.Set(b2Vec2(0, 0), b2Vec2(world_width, 0)); |
| 62 groundBody->CreateFixture(&groundBox, 0); |
| 63 // top |
| 64 groundBox.Set(b2Vec2(0, world_height), b2Vec2(world_width, world_height)); |
| 65 groundBody->CreateFixture(&groundBox, 0); |
| 66 // left |
| 67 groundBox.Set(b2Vec2(0, world_height), b2Vec2(0,0)); |
| 68 groundBody->CreateFixture(&groundBox, 0); |
| 69 // right |
| 70 groundBox.Set(b2Vec2(world_width, world_height), b2Vec2(world_width, 0)); |
| 71 groundBody->CreateFixture(&groundBox, 0); |
| 72 } |
| 73 |
| 74 void PhysicsLayer::UpdateWorld(float dt) { |
| 75 world_->Step(dt, VELOCITY_ITERATIONS, POS_ITERATIONS); |
| 76 } |
| 77 |
| 78 void PhysicsLayer::ccTouchesEnded(CCSet* touches, CCEvent* event) { |
| 79 for (CCSetIterator it = touches->begin(); it != touches->end(); it++) { |
| 80 CCTouch* touch = (CCTouch*)(*it); |
| 81 if (!touch) |
| 82 break; |
| 83 |
| 84 CCPoint location = touch->getLocation(); |
| 85 //CCLOG("PhysicsLayer touch x:%.f, y:%.f", location.x, location.y); |
| 86 AddNewSpriteAtPosition(location); |
| 87 } |
| 88 } |
| 89 |
| 90 void PhysicsLayer::AddNewSpriteAtPosition(CCPoint p) { |
| 91 CCLOG("Add sprite %0.fx%.f", p.x, p.y); |
| 92 |
| 93 // Create a new physics body at the given position. |
| 94 b2BodyDef body_def; |
| 95 body_def.type = b2_dynamicBody; |
| 96 body_def.position.Set(SCREEN_TO_WORLD(p.x), SCREEN_TO_WORLD(p.y)); |
| 97 b2Body* body = world_->CreateBody(&body_def); |
| 98 |
| 99 b2PolygonShape dynamicBox; |
| 100 dynamicBox.SetAsBox(.5f, .5f); |
| 101 |
| 102 b2FixtureDef fixtureDef; |
| 103 fixtureDef.shape = &dynamicBox; |
| 104 fixtureDef.density = 1.0f; |
| 105 fixtureDef.friction = 0.3f; |
| 106 body->CreateFixture(&fixtureDef); |
| 107 |
| 108 // Create new sprite an link it to the physics body. |
| 109 CCPhysicsSprite* sprite; |
| 110 sprite = CCPhysicsSprite::createWithTexture(sprite_texture_, |
| 111 CCRectMake(0, 0, 32, 32)); |
| 112 |
| 113 // Find the sprite batch node and attach new sprite to it. |
| 114 CCNode* parent = getChildByTag(SPRITE_BATCH_NODE_TAG); |
| 115 parent->addChild(sprite); |
| 116 sprite->setB2Body(body); |
| 117 sprite->setPTMRatio(PTM_RATIO); |
| 118 sprite->setPosition(ccp(p.x, p.y)); |
| 119 } |
OLD | NEW |