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

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

Issue 12387095: [nacltoons] Use box2d sensors to detect collisions. (Closed) Base URL: https://nativeclient-sdk.googlecode.com/svn/trunk/src
Patch Set: Created 7 years, 9 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
« nacltoons/src/physics_layer.h ('K') | « nacltoons/src/physics_layer.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "physics_layer.h" 5 #include "physics_layer.h"
6 #include "gameplay_scene.h" 6 #include "gameplay_scene.h"
7 7
8 #include "physics_nodes/CCPhysicsSprite.h" 8 #include "physics_nodes/CCPhysicsSprite.h"
9 #include "CCLuaEngine.h" 9 #include "CCLuaEngine.h"
10 #include "LuaBox2D.h" 10 #include "LuaBox2D.h"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 current_instance_ = this; 48 current_instance_ = this;
49 49
50 setTouchEnabled(true); 50 setTouchEnabled(true);
51 51
52 InitPhysics(); 52 InitPhysics();
53 53
54 // load level from lua file. 54 // load level from lua file.
55 LoadLua(); 55 LoadLua();
56 56
57 // calculate brush size 57 // calculate brush size
58 brush_ = (CCSprite*)getChildByTag(TAG_BRUSH); 58 CCSpriteBatchNode* brush_batch = (CCSpriteBatchNode*)getChildByTag(TAG_BRUSH);
59 assert(brush_); 59 assert(brush_batch);
60 brush_ = CCSprite::createWithTexture(brush_batch->getTexture());
61 brush_->retain();
60 CCSize brush_size = brush_->getContentSize(); 62 CCSize brush_size = brush_->getContentSize();
61 brush_radius_ = MAX(brush_size.height/2, brush_size.width/2); 63 brush_radius_ = MAX(brush_size.height/2, brush_size.width/2);
62 64
63 // script physics updates each frame 65 // script physics updates each frame
64 schedule(schedule_selector(PhysicsLayer::UpdateWorld)); 66 schedule(schedule_selector(PhysicsLayer::UpdateWorld));
65 return true; 67 return true;
66 } 68 }
67 69
68 PhysicsLayer::PhysicsLayer() : 70 PhysicsLayer::PhysicsLayer() :
69 goal_reached_(false), 71 goal_reached_(false),
70 current_touch_id_(-1), 72 current_touch_id_(-1),
71 render_target_(NULL), 73 render_target_(NULL),
72 box2d_density_(DEFAULT_DENSITY), 74 box2d_density_(DEFAULT_DENSITY),
73 box2d_restitution_(DEFAULT_RESTITUTION), 75 box2d_restitution_(DEFAULT_RESTITUTION),
74 box2d_friction_(DEFAULT_FRICTION), 76 box2d_friction_(DEFAULT_FRICTION),
75 #ifdef COCOS2D_DEBUG 77 #ifdef COCOS2D_DEBUG
76 debug_enabled_(false) 78 debug_enabled_(false)
77 #endif 79 #endif
78 { 80 {
79 memset(stars_collected_, 0, sizeof(stars_collected_)); 81 memset(stars_collected_, 0, sizeof(stars_collected_));
80 } 82 }
81 83
82 PhysicsLayer::~PhysicsLayer() { 84 PhysicsLayer::~PhysicsLayer() {
85 brush_->release();
83 if (current_instance_ == this) 86 if (current_instance_ == this)
84 current_instance_ = NULL; 87 current_instance_ = NULL;
85 delete box2d_world_; 88 delete box2d_world_;
86 #ifdef COCOS2D_DEBUG 89 #ifdef COCOS2D_DEBUG
87 delete box2d_debug_draw_; 90 delete box2d_debug_draw_;
88 #endif 91 #endif
89 } 92 }
90 93
91 void PhysicsLayer::registerWithTouchDispatcher() 94 void PhysicsLayer::registerWithTouchDispatcher()
92 { 95 {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 b2Body* ground_body = box2d_world_->CreateBody(&ground_def); 160 b2Body* ground_body = box2d_world_->CreateBody(&ground_def);
158 161
159 CCLog("origin: %.fx%.f", origin.x, origin.y); 162 CCLog("origin: %.fx%.f", origin.x, origin.y);
160 CCLog("size: %.fx%f", visible_size.width, visible_size.height); 163 CCLog("size: %.fx%f", visible_size.width, visible_size.height);
161 164
162 // Define the ground box shape. 165 // Define the ground box shape.
163 b2EdgeShape ground_box; 166 b2EdgeShape ground_box;
164 ground_box.Set(b2Vec2(0, 0), b2Vec2(world_width, 0)); 167 ground_box.Set(b2Vec2(0, 0), b2Vec2(world_width, 0));
165 ground_body->CreateFixture(&ground_box, 0); 168 ground_body->CreateFixture(&ground_box, 0);
166 169
170 box2d_world_->SetContactListener(this);
171
167 #ifdef COCOS2D_DEBUG 172 #ifdef COCOS2D_DEBUG
168 box2d_debug_draw_ = new GLESDebugDraw(PTM_RATIO); 173 box2d_debug_draw_ = new GLESDebugDraw(PTM_RATIO);
169 box2d_world_->SetDebugDraw(box2d_debug_draw_); 174 box2d_world_->SetDebugDraw(box2d_debug_draw_);
170 175
171 uint32 flags = 0; 176 uint32 flags = 0;
172 flags += b2Draw::e_shapeBit; 177 flags += b2Draw::e_shapeBit;
173 flags += b2Draw::e_jointBit; 178 flags += b2Draw::e_jointBit;
174 flags += b2Draw::e_centerOfMassBit; 179 flags += b2Draw::e_centerOfMassBit;
175 //flags += b2Draw::e_aabbBit; 180 //flags += b2Draw::e_aabbBit;
176 //flags += b2Draw::e_pairBit; 181 //flags += b2Draw::e_pairBit;
(...skipping 21 matching lines...) Expand all
198 CCRect CalcBoundingBox(CCSprite* sprite) { 203 CCRect CalcBoundingBox(CCSprite* sprite) {
199 CCSize size = sprite->getContentSize(); 204 CCSize size = sprite->getContentSize();
200 CCPoint pos = sprite->getPosition(); 205 CCPoint pos = sprite->getPosition();
201 return CCRectMake(pos.x - size.width, pos.y - size.height, 206 return CCRectMake(pos.x - size.width, pos.y - size.height,
202 size.width, size.height/2); 207 size.width, size.height/2);
203 } 208 }
204 209
205 void PhysicsLayer::UpdateWorld(float dt) { 210 void PhysicsLayer::UpdateWorld(float dt) {
206 // update physics 211 // update physics
207 box2d_world_->Step(dt, VELOCITY_ITERATIONS, POS_ITERATIONS); 212 box2d_world_->Step(dt, VELOCITY_ITERATIONS, POS_ITERATIONS);
213 }
208 214
209 CCSprite* ball = (CCSprite*)getChildByTag(TAG_BALL); 215 void PhysicsLayer::BeginContact(b2Contact* contact) {
210 assert(ball); 216 CCPhysicsSprite* ball = (CCPhysicsSprite*)getChildByTag(TAG_BALL);
211 CCRect ball_bounds = CalcBoundingBox(ball); 217 b2Body* ball_body = ball->getB2Body();
218 b2Body* body1 = contact->GetFixtureA()->GetBody();
219 b2Body* body2 = contact->GetFixtureB()->GetBody();
220 b2Body* body_other = NULL;
212 221
213 // TODO(sbc): Investigate using box2d to do collision for us 222 if (body1 == ball_body)
214 // http://www.iforce2d.net/b2dtut/sensors 223 body_other = body2;
224 else if (body2 == ball_body)
225 body_other = body1;
226 else // we are only interesting in collitions involving the ball
binji 2013/03/04 19:31:13 sp: interested in collisions
Sam Clegg 2013/03/04 20:08:18 Done.
227 return
215 228
216 // check for stars being reached by ball 229 CCLog("BeginContact %p", body_other);
230
217 int star_tags[] = { TAG_STAR1, TAG_STAR2, TAG_STAR3 }; 231 int star_tags[] = { TAG_STAR1, TAG_STAR2, TAG_STAR3 };
218 for (uint i = 0; i < sizeof(star_tags)/sizeof(int); i++) { 232 for (uint i = 0; i < sizeof(star_tags)/sizeof(int); i++) {
219 if (stars_collected_[i]) 233 if (stars_collected_[i])
220 continue; 234 continue;
221 CCSprite* star = (CCSprite*)getChildByTag(star_tags[i]); 235 CCPhysicsSprite* star = (CCPhysicsSprite*)getChildByTag(star_tags[i]);
222 assert(star); 236 if (body_other == star->getB2Body())
223 CCRect star_bounds = CalcBoundingBox(star); 237 {
binji 2013/03/04 19:31:13 nit: { on previous line here and below
Sam Clegg 2013/03/04 20:08:18 Done.
224
225 if (ball_bounds.intersectsRect(star_bounds)) {
226 CCLog("star %d reached", i); 238 CCLog("star %d reached", i);
227 stars_collected_[i] = true; 239 stars_collected_[i] = true;
228 CCAction* action = CCFadeOut::create(0.5f); 240 CCAction* action = CCFadeOut::create(0.5f);
229 star->runAction(action); 241 star->runAction(action);
230 } 242 }
231 } 243 }
232 244
233 // check for goal being reached by ball 245 if (!goal_reached_)
234 if (!goal_reached_) { 246 {
235 CCSprite* goal = (CCSprite*)getChildByTag(TAG_GOAL); 247 CCPhysicsSprite* goal = (CCPhysicsSprite*)getChildByTag(TAG_GOAL);
236 assert(goal); 248 if (body_other == goal->getB2Body())
237 CCRect goal_bounds = CalcBoundingBox(goal); 249 {
238 if (ball_bounds.intersectsRect(goal_bounds)) {
239 CCLog("goal reached"); 250 CCLog("goal reached");
240 goal_reached_ = true; 251 goal_reached_ = true;
241 252
242 // fade out the goal and trigger gameover callback when its 253 // fade out the goal and trigger gameover callback when its
243 // done 254 // done
244 CCActionInterval* fadeout = CCFadeOut::create(0.5f); 255 CCActionInterval* fadeout = CCFadeOut::create(0.5f);
245 CCFiniteTimeAction* fadeout_done = CCCallFuncN::create(this, 256 CCFiniteTimeAction* fadeout_done = CCCallFuncN::create(this,
246 callfuncN_selector(PhysicsLayer::LevelComplete)); 257 callfuncN_selector(PhysicsLayer::LevelComplete));
247 CCSequence* seq = CCSequence::create(fadeout, fadeout_done, NULL); 258 CCSequence* seq = CCSequence::create(fadeout, fadeout_done, NULL);
248 goal->runAction(seq); 259 goal->runAction(seq);
249 } 260 }
250 } 261 }
251 } 262 }
252 263
253 void PhysicsLayer::LevelComplete(CCNode* sender) { 264 void PhysicsLayer::LevelComplete(CCNode* sender) {
254 unschedule(schedule_selector(PhysicsLayer::UpdateWorld)); 265 unschedule(schedule_selector(PhysicsLayer::UpdateWorld));
255 setTouchEnabled(false); 266 setTouchEnabled(false);
256 GameplayScene* scene = static_cast<GameplayScene*>(getParent()); 267 GameplayScene* scene = static_cast<GameplayScene*>(getParent());
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 float posx = SCREEN_TO_WORLD((sx+ex)/2) - body->GetPosition().x; 544 float posx = SCREEN_TO_WORLD((sx+ex)/2) - body->GetPosition().x;
534 float posy = SCREEN_TO_WORLD((sy+ey)/2) - body->GetPosition().y; 545 float posy = SCREEN_TO_WORLD((sy+ey)/2) - body->GetPosition().y;
535 546
536 float width = SCREEN_TO_WORLD(abs(distance)); 547 float width = SCREEN_TO_WORLD(abs(distance));
537 float height = SCREEN_TO_WORLD(brush_->boundingBox().size.height); 548 float height = SCREEN_TO_WORLD(brush_->boundingBox().size.height);
538 549
539 b2PolygonShape shape; 550 b2PolygonShape shape;
540 shape.SetAsBox(width / 2, height / 2, b2Vec2(posx, posy), angle); 551 shape.SetAsBox(width / 2, height / 2, b2Vec2(posx, posy), angle);
541 AddShapeToBody(body, &shape); 552 AddShapeToBody(body, &shape);
542 } 553 }
OLDNEW
« nacltoons/src/physics_layer.h ('K') | « nacltoons/src/physics_layer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698