#include "HelloWorldScene.h"
Scene* HelloWorld::createScene()
{
… 생략 : Box2dEx19의 코드와 같음 …
}
bool HelloWorld::init()
{
if ( !Scene::init() )
{
return false;
}
/////////////////////////////
// 윈도우 크기를 구한다.
winSize = Director::getInstance()->getWinSize();
// 이미지의 텍스쳐를 구한다.
texture = Director::getInstance()->getTextureCache()->addImage("blocks.png");
// 월드 생성
if (this->createBox2dWorld(true))
{
//월드에 충돌함수 클래스를 리스너에 추가함
_world->SetContactListener((b2ContactListener *)this);
this->setBox2dWorld();
this->schedule(schedule_selector(HelloWorld::tick));
}
return true;
}
bool HelloWorld::createBox2dWorld(bool debug)
{
… 생략 : Box2dEx19의 코드와 같음 …
}
void HelloWorld::setBox2dWorld()
{
playerIsFlying = false;
// 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);
//-------------------------------------------------------------------------------
// 새로운 물리 객체의 바디와 해당 스프라이트를 추가한다.
addNewSpriteAtPosition(Vec2(240, 100));
}
HelloWorld::~HelloWorld()
{
… 생략 : Box2dEx19의 코드와 같음 …
}
void HelloWorld::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
… 생략 : Box2dEx19의 코드와 같음 …
}
void HelloWorld::onDraw(const Mat4 &transform, uint32_t flags)
{
… 생략 : Box2dEx19의 코드와 같음 …
}
void HelloWorld::onEnter()
{
… 생략 : Box2dEx19의 코드와 같음 …
}
void HelloWorld::onExit()
{
… 생략 : Box2dEx19의 코드와 같음 …
}
void HelloWorld::tick(float dt)
{
… 생략 : Box2dEx19의 코드와 같음 …
}
void HelloWorld::addNewSpriteAtPosition(Vec2 location)
{
… 생략 : Box2dEx19의 코드와 같음 …
}
bool HelloWorld::onTouchBegan(Touch* touch, Event* event)
{
… 생략 : Box2dEx19의 코드와 같음 …
}
void HelloWorld::onTouchEnded(Touch* touch, Event* event)
{
… 생략 : Box2dEx19의 코드와 같음 …
}
void HelloWorld::BeginContact(b2Contact* contact)
{
}
void HelloWorld::PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
b2Fixture* fixtureA = contact->GetFixtureA();
b2Fixture* fixtureB = contact->GetFixtureB();
b2Body* bodyA = fixtureA->GetBody();
b2Body* bodyB = fixtureB->GetBody();
if (bodyA->GetUserData() == nullptr || bodyB->GetUserData() == nullptr) {
return;
}
Sprite* spr1 = (Sprite *)bodyA->GetUserData();
Sprite* spr2 = (Sprite *)bodyB->GetUserData();
float pos1 = spr1->getPosition().y + (spr1->getContentSize().height / 2);
//float pos2 = spr2->getPosition().y - (spr2->getContentSize().height / 2);
float pos2 = spr2->getPosition().y;
if (pos2 < pos1)
{
contact->SetEnabled(false);
}
}
void HelloWorld::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
{
}
void HelloWorld::EndContact(b2Contact* contact)
{
contact->SetEnabled(true);
}