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

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: 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 Native Client 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
17 USING_NS_CC_EXT;
18
19 bool PhysicsLayer::init() {
20 if (!CCLayerColor::initWithColor(ccc4(0,0x8F,0xD8,0xD8)))
21 return false;
22
23 setTouchEnabled(true);
24 initPhysics();
25
26 CCSpriteBatchNode* batch = CCSpriteBatchNode::create("blocks.png", 100);
binji 2013/02/12 22:51:53 magic number?
Sam Clegg 2013/02/12 23:07:38 Done.
27 addChild(batch, 0, SPRITE_BATCH_NODE_TAG);
28
29 // cache texture pointer for creating sprites
30 sprite_texture_ = batch->getTexture();
31
32 // script physics updates each frame
33 schedule(schedule_selector(PhysicsLayer::updateWorld));
34 return true;
35 }
36
37
38 PhysicsLayer::~PhysicsLayer()
39 {
binji 2013/02/12 22:51:53 { on previous line
Sam Clegg 2013/02/12 23:07:38 Done.
40 delete world_;
41 }
42
43
44 bool PhysicsLayer::initPhysics()
45 {
binji 2013/02/12 22:51:53 { on previous line
Sam Clegg 2013/02/12 23:07:38 Done.
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 {
binji 2013/02/12 22:51:53 "
Sam Clegg 2013/02/12 23:07:38 Done.
85 CCTouch* touch = (CCTouch*)(*it);
86 if (!touch)
87 break;
88
89 CCPoint location = touch->getLocation();
90 //CCLOG("PhysicsLayer touch x:%.f, y:%.f", location.x, location.y);
91 addNewSpriteAtPosition(location);
92 }
93 }
94
95 void PhysicsLayer::addNewSpriteAtPosition(CCPoint p)
96 {
binji 2013/02/12 22:51:53 "
Sam Clegg 2013/02/12 23:07:38 Done.
97 CCLOG("Add sprite %0.fx%.f", p.x, p.y);
98
99 // Create a new physics body at the given position.
100 b2BodyDef body_def;
101 body_def.type = b2_dynamicBody;
102 body_def.position.Set(SCREEN_TO_WORLD(p.x), SCREEN_TO_WORLD(p.y));
103 b2Body* body = world_->CreateBody(&body_def);
104
105 b2PolygonShape dynamicBox;
106 dynamicBox.SetAsBox(.5f, .5f);
107
108 b2FixtureDef fixtureDef;
109 fixtureDef.shape = &dynamicBox;
110 fixtureDef.density = 1.0f;
111 fixtureDef.friction = 0.3f;
112 body->CreateFixture(&fixtureDef);
113
114 // Create new sprite an link it to the physics body.
115 CCPhysicsSprite* sprite;
116 sprite = CCPhysicsSprite::createWithTexture(sprite_texture_,
117 CCRectMake(0, 0, 32, 32));
118
119 // Find the sprite batch node and attach new sprite to it.
120 CCNode* parent = getChildByTag(SPRITE_BATCH_NODE_TAG);
121 parent->addChild(sprite);
122 sprite->setB2Body(body);
123 sprite->setPTMRatio(PTM_RATIO);
124 sprite->setPosition(ccp(p.x, p.y));
125 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698