Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 | 137 |
| 138 return true; | 138 return true; |
| 139 } | 139 } |
| 140 | 140 |
| 141 bool PhysicsLayer::InitPhysics() { | 141 bool PhysicsLayer::InitPhysics() { |
| 142 b2Vec2 gravity(0.0f, -9.8f); | 142 b2Vec2 gravity(0.0f, -9.8f); |
| 143 box2d_world_ = new b2World(gravity); | 143 box2d_world_ = new b2World(gravity); |
| 144 box2d_world_->SetAllowSleeping(true); | 144 box2d_world_->SetAllowSleeping(true); |
| 145 box2d_world_->SetContinuousPhysics(true); | 145 box2d_world_->SetContinuousPhysics(true); |
| 146 | 146 |
| 147 // Find visible rect, and convert to box2d space. | |
| 148 CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); | |
| 149 CCSize visible_size = CCDirector::sharedDirector()->getVisibleSize(); | |
| 150 float world_width = SCREEN_TO_WORLD(visible_size.width); | |
| 151 b2Vec2 world_origin(SCREEN_TO_WORLD(origin.x), SCREEN_TO_WORLD(origin.y)); | |
| 152 | |
| 147 // create the ground | 153 // create the ground |
| 148 b2BodyDef groundBodyDef; | 154 b2BodyDef groundBodyDef; |
| 149 groundBodyDef.position.Set(0, 0); | 155 groundBodyDef.position = world_origin; |
| 150 b2Body* groundBody = box2d_world_->CreateBody(&groundBodyDef); | 156 b2Body* groundBody = box2d_world_->CreateBody(&groundBodyDef); |
| 151 | 157 |
| 152 CCSize win_size = CCDirector::sharedDirector()->getWinSize(); | 158 CCLog("origin: %.fx%.f", origin.x, origin.y); |
| 153 int world_width = SCREEN_TO_WORLD(win_size.width); | 159 CCLog("size: %.fx%f", visible_size.width, visible_size.height); |
| 154 int world_height = SCREEN_TO_WORLD(win_size.height); | |
| 155 | 160 |
| 156 // Define the ground box shape. | 161 // Define the ground box shape. |
| 157 b2EdgeShape groundBox; | 162 b2EdgeShape groundBox; |
| 158 // bottom | |
| 159 groundBox.Set(b2Vec2(0, 0), b2Vec2(world_width, 0)); | 163 groundBox.Set(b2Vec2(0, 0), b2Vec2(world_width, 0)); |
| 160 groundBody->CreateFixture(&groundBox, 0); | 164 groundBody->CreateFixture(&groundBox, 0); |
| 161 // top | |
| 162 groundBox.Set(b2Vec2(0, world_height), b2Vec2(world_width, world_height)); | |
| 163 groundBody->CreateFixture(&groundBox, 0); | |
| 164 // left | |
| 165 groundBox.Set(b2Vec2(0, world_height), b2Vec2(0,0)); | |
| 166 groundBody->CreateFixture(&groundBox, 0); | |
| 167 // right | |
| 168 groundBox.Set(b2Vec2(world_width, world_height), b2Vec2(world_width, 0)); | |
| 169 groundBody->CreateFixture(&groundBox, 0); | |
| 170 | 165 |
| 171 #ifdef COCOS2D_DEBUG | 166 #ifdef COCOS2D_DEBUG |
| 172 box2d_debug_draw_ = new GLESDebugDraw(PTM_RATIO); | 167 box2d_debug_draw_ = new GLESDebugDraw(PTM_RATIO); |
| 173 box2d_world_->SetDebugDraw(box2d_debug_draw_); | 168 box2d_world_->SetDebugDraw(box2d_debug_draw_); |
| 174 | 169 |
| 175 uint32 flags = 0; | 170 uint32 flags = 0; |
| 176 flags += b2Draw::e_shapeBit; | 171 flags += b2Draw::e_shapeBit; |
| 177 flags += b2Draw::e_jointBit; | 172 flags += b2Draw::e_jointBit; |
| 178 flags += b2Draw::e_centerOfMassBit; | 173 flags += b2Draw::e_centerOfMassBit; |
| 179 //flags += b2Draw::e_aabbBit; | 174 //flags += b2Draw::e_aabbBit; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 255 } | 250 } |
| 256 | 251 |
| 257 void PhysicsLayer::LevelComplete(CCNode* sender) { | 252 void PhysicsLayer::LevelComplete(CCNode* sender) { |
| 258 unschedule(schedule_selector(PhysicsLayer::UpdateWorld)); | 253 unschedule(schedule_selector(PhysicsLayer::UpdateWorld)); |
| 259 setTouchEnabled(false); | 254 setTouchEnabled(false); |
| 260 GameplayScene* scene = static_cast<GameplayScene*>(getParent()); | 255 GameplayScene* scene = static_cast<GameplayScene*>(getParent()); |
| 261 scene->GameOver(true); | 256 scene->GameOver(true); |
| 262 } | 257 } |
| 263 | 258 |
| 264 void PhysicsLayer::DrawPoint(CCPoint& location) { | 259 void PhysicsLayer::DrawPoint(CCPoint& location) { |
| 260 ClampBrushLocation(location); | |
| 265 render_target_->begin(); | 261 render_target_->begin(); |
| 266 brush_->setPosition(ccp(location.x, location.y)); | 262 brush_->setPosition(ccp(location.x, location.y)); |
| 267 brush_->visit(); | 263 brush_->visit(); |
| 268 render_target_->end(); | 264 render_target_->end(); |
| 269 points_being_drawn_.push_back(location); | 265 points_being_drawn_.push_back(location); |
| 270 } | 266 } |
| 271 | 267 |
| 272 void PhysicsLayer::draw() { | 268 void PhysicsLayer::draw() { |
| 273 CCLayerColor::draw(); | 269 CCLayerColor::draw(); |
| 274 | 270 |
| 275 #ifdef COCOS2D_DEBUG | 271 #ifdef COCOS2D_DEBUG |
| 276 if (debug_enabled_) | 272 if (debug_enabled_) |
| 277 { | 273 { |
| 278 ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position); | 274 ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position); |
| 279 kmGLPushMatrix(); | 275 kmGLPushMatrix(); |
| 280 box2d_world_->DrawDebugData(); | 276 box2d_world_->DrawDebugData(); |
| 281 kmGLPopMatrix(); | 277 kmGLPopMatrix(); |
| 282 } | 278 } |
| 283 #endif | 279 #endif |
| 284 } | 280 } |
| 285 | 281 |
| 282 void PhysicsLayer::ClampBrushLocation(CCPoint& point) | |
| 283 { | |
|
binji
2013/02/28 23:09:11
nit: { on previous line
| |
| 284 CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); | |
| 285 CCSize visible_size = CCDirector::sharedDirector()->getVisibleSize(); | |
| 286 | |
| 287 float min_x = origin.x + brush_radius_; | |
| 288 float min_y = origin.y + brush_radius_; | |
| 289 if (point.x < min_x) point.x = min_x; | |
| 290 if (point.y < min_y) point.y = min_y; | |
| 291 | |
| 292 float max_x = origin.x + visible_size.width - brush_radius_; | |
| 293 float max_y = origin.y + visible_size.height - brush_radius_; | |
| 294 if (point.x > max_x) point.x = max_x; | |
| 295 if (point.y > max_y) point.y = max_y; | |
| 296 } | |
| 297 | |
| 286 void PhysicsLayer::DrawLine(CCPoint& start, CCPoint& end) | 298 void PhysicsLayer::DrawLine(CCPoint& start, CCPoint& end) |
| 287 { | 299 { |
| 300 ClampBrushLocation(start); | |
| 301 ClampBrushLocation(end); | |
| 302 | |
| 288 // calculate distance moved | 303 // calculate distance moved |
| 289 float distance = ccpDistance(start, end); | 304 float distance = ccpDistance(start, end); |
| 290 | 305 |
| 291 // draw the brush sprite into render texture at every point between the old | 306 // draw the brush sprite into render texture at every point between the old |
| 292 // and new cursor positions | 307 // and new cursor positions |
| 293 render_target_->begin(); | 308 render_target_->begin(); |
| 294 for (int i = 0; i < int(distance + 0.5); i++) | 309 for (int i = 0; i < int(distance + 0.5); i++) |
| 295 { | 310 { |
| 296 float difx = end.x - start.x; | 311 float difx = end.x - start.x; |
| 297 float dify = end.y - start.y; | 312 float dify = end.y - start.y; |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 514 float posx = SCREEN_TO_WORLD((sx+ex)/2) - body->GetPosition().x; | 529 float posx = SCREEN_TO_WORLD((sx+ex)/2) - body->GetPosition().x; |
| 515 float posy = SCREEN_TO_WORLD((sy+ey)/2) - body->GetPosition().y; | 530 float posy = SCREEN_TO_WORLD((sy+ey)/2) - body->GetPosition().y; |
| 516 | 531 |
| 517 float width = SCREEN_TO_WORLD(abs(distance)); | 532 float width = SCREEN_TO_WORLD(abs(distance)); |
| 518 float height = SCREEN_TO_WORLD(brush_->boundingBox().size.height); | 533 float height = SCREEN_TO_WORLD(brush_->boundingBox().size.height); |
| 519 | 534 |
| 520 b2PolygonShape shape; | 535 b2PolygonShape shape; |
| 521 shape.SetAsBox(width / 2, height / 2, b2Vec2(posx, posy), angle); | 536 shape.SetAsBox(width / 2, height / 2, b2Vec2(posx, posy), angle); |
| 522 AddShapeToBody(body, &shape); | 537 AddShapeToBody(body, &shape); |
| 523 } | 538 } |
| OLD | NEW |