Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(849)

Side by Side Diff: nacltoons/src/PhysicsLayer.cc

Issue 12226130: [nacltoons] Add initial frontend and physics game scenes. (Closed) Base URL: https://nativeclient-sdk.googlecode.com/svn/trunk/src
Patch Set: fix style nits Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
40 PhysicsLayer::~PhysicsLayer() {
41 delete world_;
42 }
43
44
45 bool PhysicsLayer::initPhysics() {
46 b2Vec2 gravity(0.0f, -9.8f);
47 world_ = new b2World(gravity);
48 world_->SetAllowSleeping(true);
49 world_->SetContinuousPhysics(true);
50
51 // create the ground
52 b2BodyDef groundBodyDef;
53 groundBodyDef.position.Set(0, 0);
54 b2Body* groundBody = world_->CreateBody(&groundBodyDef);
55
56 CCSize win_size = CCDirector::sharedDirector()->getWinSize();
57 int world_width = SCREEN_TO_WORLD(win_size.width);
58 int world_height = SCREEN_TO_WORLD(win_size.height);
59
60 // Define the ground box shape.
61 b2EdgeShape groundBox;
62 // bottom
63 groundBox.Set(b2Vec2(0, 0), b2Vec2(world_width, 0));
64 groundBody->CreateFixture(&groundBox, 0);
65 // top
66 groundBox.Set(b2Vec2(0, world_height), b2Vec2(world_width, world_height));
67 groundBody->CreateFixture(&groundBox, 0);
68 // left
69 groundBox.Set(b2Vec2(0, world_height), b2Vec2(0,0));
70 groundBody->CreateFixture(&groundBox, 0);
71 // right
72 groundBox.Set(b2Vec2(world_width, world_height), b2Vec2(world_width, 0));
73 groundBody->CreateFixture(&groundBox, 0);
74 }
75
76
77 void PhysicsLayer::updateWorld(float dt) {
78 world_->Step(dt, VELOCITY_ITERATIONS, POS_ITERATIONS);
79 }
80
81
82 void PhysicsLayer::ccTouchesEnded(CCSet* touches, CCEvent* event) {
83 for (CCSetIterator it = touches->begin(); it != touches->end(); it++) {
84 CCTouch* touch = (CCTouch*)(*it);
85 if (!touch)
86 break;
87
88 CCPoint location = touch->getLocation();
89 //CCLOG("PhysicsLayer touch x:%.f, y:%.f", location.x, location.y);
90 addNewSpriteAtPosition(location);
91 }
92 }
93
94 void PhysicsLayer::addNewSpriteAtPosition(CCPoint p) {
95 CCLOG("Add sprite %0.fx%.f", p.x, p.y);
96
97 // Create a new physics body at the given position.
98 b2BodyDef body_def;
99 body_def.type = b2_dynamicBody;
100 body_def.position.Set(SCREEN_TO_WORLD(p.x), SCREEN_TO_WORLD(p.y));
101 b2Body* body = world_->CreateBody(&body_def);
102
103 b2PolygonShape dynamicBox;
104 dynamicBox.SetAsBox(.5f, .5f);
105
106 b2FixtureDef fixtureDef;
107 fixtureDef.shape = &dynamicBox;
108 fixtureDef.density = 1.0f;
109 fixtureDef.friction = 0.3f;
110 body->CreateFixture(&fixtureDef);
111
112 // Create new sprite an link it to the physics body.
113 CCPhysicsSprite* sprite;
114 sprite = CCPhysicsSprite::createWithTexture(sprite_texture_,
115 CCRectMake(0, 0, 32, 32));
116
117 // Find the sprite batch node and attach new sprite to it.
118 CCNode* parent = getChildByTag(SPRITE_BATCH_NODE_TAG);
119 parent->addChild(sprite);
120 sprite->setB2Body(body);
121 sprite->setPTMRatio(PTM_RATIO);
122 sprite->setPosition(ccp(p.x, p.y));
123 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698