#include "HelloWorldScene.h"
Scene* HelloWorld::createScene()
{
return HelloWorld::create();
}
bool HelloWorld::init()
{
if ( !Scene::init() )
{
return false;
}
/////////////////////////////
// 윈도우 크기를 구한다.
winSize = Director::getInstance()->getWinSize();
// 이미지의 텍스쳐를 구한다.
texture = Director::getInstance()->getTextureCache()->addImage("blocks.png");
// 월드 생성
if (this->createBox2dWorld(true))
{
srand((int)time(nullptr));
this->setBox2dWorld();
this->schedule(schedule_selector(HelloWorld::tick));
}
return true;
}
bool HelloWorld::createBox2dWorld(bool debug)
{
… 생략 : Box2dEx02의 코드와 같음 …
}
void HelloWorld::setBox2dWorld()
{
// staticBody 스프라이트를 추가한다.
Sprite* pSprite1 = Sprite::createWithTexture(texture, Rect(0, 0, 64, 64));
pSprite1->setPosition(Vec2(winSize.width / 2, winSize.height / 2));
this->addChild(pSprite1);
// 바디데프 만들고 속성들을 지정한다.
b2BodyDef bodyDef1;
bodyDef1.type = b2_staticBody;
bodyDef1.position.Set(winSize.width / 2 / PTM_RATIO,
winSize.height / 2 / PTM_RATIO);
bodyDef1.userData = pSprite1;
b2Body* body1 = _world->CreateBody(&bodyDef1);
// 바디에 적용할 물리 속성용 바디의 모양을 만든다.
b2PolygonShape staticBox;
// 바디의 크기 지정 - 상자의 크기에서 가운데 위치를 지정한다.
staticBox.SetAsBox((pSprite1->getContentSize().width / 2) / PTM_RATIO,
(pSprite1->getContentSize().height / 2) / PTM_RATIO);
b2FixtureDef fixtureDef1;
fixtureDef1.shape = &staticBox;
fixtureDef1.density = 1.0f;
body1->CreateFixture(&fixtureDef1);
// kinematicBody 스프라이트를 추가한다.
Sprite* pSprite2 = Sprite::createWithTexture(texture, Rect(0, 0, 64, 32));
pSprite2->setPosition(Vec2(0, 100));
this->addChild(pSprite2);
// 바디데프 만들고 속성들을 지정한다.
b2BodyDef bodyDef2;
bodyDef2.type = b2_kinematicBody;
bodyDef2.position.Set(0, 100.0f / PTM_RATIO);
bodyDef2.linearVelocity = b2Vec2(1.0f, 0);
bodyDef2.userData = pSprite2;
b2Body* body2 = _world->CreateBody(&bodyDef2);
// 바디에 적용할 물리 속성용 바디의 모양을 만든다.
b2PolygonShape kinematicBox;
// 바디의 크기 지정 - 상자의 크기에서 가운데 위치를 지정한다.
kinematicBox.SetAsBox((pSprite2->getContentSize().width / 2) / PTM_RATIO,
(pSprite2->getContentSize().height / 2) / PTM_RATIO);
b2FixtureDef fixtureDef2;
fixtureDef2.shape = &kinematicBox;
fixtureDef2.density = 1.0f;
body2->CreateFixture(&fixtureDef2);
}
HelloWorld::~HelloWorld()
{
… 생략 : Box2dEx02의 코드와 같음 …
}
void HelloWorld::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
… 생략 : Box2dEx02의 코드와 같음 …
}
void HelloWorld::onDraw(const Mat4 &transform, uint32_t flags)
{
… 생략 : Box2dEx02의 코드와 같음 …
}
void HelloWorld::onEnter()
{
… 생략 : Box2dEx02의 코드와 같음 …
}
void HelloWorld::onExit()
{
… 생략 : Box2dEx02의 코드와 같음 …
}
void HelloWorld::tick(float dt)
{
int velocityIterations = 8;
int positionIterations = 3;
// Step : 물리 세계를 시뮬레이션한다.
_world->Step(dt, velocityIterations, positionIterations);
// 만들어진 객체 만큼 루프를 돌리면서 바디에 붙인 스프라이트를 여기서 제어한다.
for (b2Body* b = _world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != nullptr) {
Sprite* spriteData = (Sprite *)b->GetUserData();
spriteData->setPosition(Vec2(b->GetPosition().x * PTM_RATIO,
b->GetPosition().y * PTM_RATIO));
spriteData->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));
}
// --------------------------------------------------------------------
// 키네마틱 바디 좌우로 이동시키기
if (b->GetType() == b2_kinematicBody) {
b2Vec2 v = b->GetPosition();
if (v.x*PTM_RATIO > winSize.width) {
// 왼쪽으로 이동
b->SetLinearVelocity(b2Vec2(-1.0f, 0));
}
else if (v.x < 0) {
// 오른쪽으로 이동
b->SetLinearVelocity(b2Vec2(1.0f, 0));
}
}
}
}
bool HelloWorld::onTouchBegan(Touch* touch, Event* event)
{
auto touchPoint = touch->getLocation();
// 터치된 지점에 새로운 물리 객체의 바디와 해당 스프라이트를 추가한다.
addNewSpriteAtPosition2(touchPoint);
return true;
}
void HelloWorld::addNewSpriteAtPosition2(Vec2 location)
{
int nNum = rand() % 3;
// 바디데프 만들고 속성들을 지정한다.
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(location.x / PTM_RATIO, location.y / PTM_RATIO);
bodyDef.userData = nullptr;
// 월드에 바디데프의 정보로 바디를 만든다.
b2Body* body = _world->CreateBody(&bodyDef);
// 바디에 적용할 물리 속성용 바디의 모양을 만든다.
if (nNum == 0) {
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(0.8f, 0.8f);
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
fixtureDef.restitution = 0.0f;
body->CreateFixture(&fixtureDef);
}
else if (nNum == 1) {
b2PolygonShape dynamicBox;
b2Vec2 tri[3];
tri[0].x = -.5;
tri[0].y = 0.0;
tri[1].x = .5;
tri[1].y = 0.0;
tri[2].x = 0;
tri[2].y = 1.0;
dynamicBox.Set(tri, 3);
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
fixtureDef.restitution = 1.0f;
body->CreateFixture(&fixtureDef);
}
else {
b2CircleShape dynamicCircle;
dynamicCircle.m_radius = 1.0;
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicCircle;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.2f;
fixtureDef.restitution = 1.0f;
body->CreateFixture(&fixtureDef);
}
}