Last active
September 6, 2022 08:02
-
-
Save takashi1975/ad8905c98490f2b8f70a to your computer and use it in GitHub Desktop.
Box2dを動かしてみた (Cocos2d-x v3.2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LOCAL_PATH := $(call my-dir) | |
include $(CLEAR_VARS) | |
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d) | |
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/external) | |
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos) | |
LOCAL_MODULE := cocos2dcpp_shared | |
LOCAL_MODULE_FILENAME := libcocos2dcpp | |
#-------------------------------------------------------- | |
#コンパイル対象 (Classesフォルダ以下の全てのcppファイルをコンパイル対象) | |
CPP_FILES := $(shell find $(LOCAL_PATH)/../../Classes -name *.cpp) | |
LOCAL_SRC_FILES := hellocpp/main.cpp | |
LOCAL_SRC_FILES += $(CPP_FILES:$(LOCAL_PATH)/%=%) | |
#-------------------------------------------------------- | |
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes | |
#個人的に GLES-Render.cpp/.h を格納してました。 | |
#LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../Classes/Box2d | |
# LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../cocos2d/external | |
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../cocos2d/external/Box2D | |
#-------------------------------------------------------- | |
LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static | |
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static | |
LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static | |
# LOCAL_WHOLE_STATIC_LIBRARIES += cocosbuilder_static | |
# LOCAL_WHOLE_STATIC_LIBRARIES += spine_static | |
# LOCAL_WHOLE_STATIC_LIBRARIES += cocostudio_static | |
# LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static | |
# LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static | |
#-------------------------------------------------------- | |
include $(BUILD_SHARED_LIBRARY) | |
#-------------------------------------------------------- | |
$(call import-module,.) | |
$(call import-module,audio/android) | |
$(call import-module,Box2D) | |
# $(call import-module,editor-support/cocosbuilder) | |
# $(call import-module,editor-support/spine) | |
# $(call import-module,editor-support/cocostudio) | |
# $(call import-module,network) | |
# $(call import-module,extensions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "HelloWorldScene.h" | |
//#include "extensions/cocos-ext.h" | |
USING_NS_CC; | |
//USING_NS_CC_EXT; | |
Scene* HelloWorld::createScene() | |
{ | |
// 'scene' is an autorelease object | |
auto scene = Scene::create(); | |
// 'layer' is an autorelease object | |
auto layer = HelloWorld::create(); | |
// add layer as a child to scene | |
scene->addChild(layer); | |
// return the scene | |
return scene; | |
} | |
// on "init" you need to initialize your instance | |
bool HelloWorld::init() | |
{ | |
bool result = false; | |
do | |
{ | |
if (!Layer::init()) | |
{ | |
break; | |
} | |
Size visibleSize = Director::sharedDirector()->getVisibleSize(); | |
this->initPhysics(); | |
{ | |
b2Body * body = NULL; | |
{ | |
b2BodyDef bodyDef; | |
bodyDef.type = b2_dynamicBody; | |
bodyDef.position.Set(visibleSize.width * 0.5f / PTM_RATIO, visibleSize.height / PTM_RATIO); | |
body = _world->CreateBody(&bodyDef); | |
} | |
if (body) | |
{ | |
b2CircleShape circle; | |
circle.m_radius = 50.0f / PTM_RATIO; | |
b2FixtureDef fixtureDef; | |
fixtureDef.shape = &circle; | |
fixtureDef.density = 0.4f; | |
fixtureDef.friction = 0.5f; | |
fixtureDef.restitution = 0.6f; | |
body->CreateFixture(&fixtureDef); | |
} | |
} | |
this->scheduleUpdate(); | |
result = true; | |
} while (0); | |
return result; | |
} | |
void HelloWorld::initPhysics() | |
{ | |
b2Vec2 gravity = b2Vec2(0.0f, -9.8f); | |
_world = new b2World(gravity); | |
_debugDraw = new GLESDebugDraw(PTM_RATIO); | |
{ | |
uint32 flags = 0; | |
flags += b2Draw::e_shapeBit; | |
// flags += b2Draw::e_jointBit; | |
// flags += b2Draw::e_aabbBit; | |
// flags += b2Draw::e_pairBit; | |
// flags += b2Draw::e_centerOfMassBit; | |
_debugDraw->SetFlags(flags); | |
} | |
_world->SetDebugDraw(_debugDraw); | |
} | |
void HelloWorld::update(float delta) | |
{ | |
int velocityIterations = 10; | |
int positionIterations = 10; | |
_world->Step(delta, velocityIterations, positionIterations); | |
} | |
void HelloWorld::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) | |
{ | |
Layer::draw(renderer, transform, flags); | |
{ | |
Director* director = Director::getInstance(); | |
GL::enableVertexAttribs( cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION ); | |
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); | |
_world->DrawDebugData(); | |
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __HELLOWORLD_SCENE_H__ | |
#define __HELLOWORLD_SCENE_H__ | |
#include "cocos2d.h" | |
#include "Box2D/Box2D.h" | |
#include "GLES-Render.h" | |
#define PTM_RATIO (32.0f) | |
class HelloWorld : public cocos2d::Layer | |
{ | |
public: | |
static cocos2d::Scene* createScene(); | |
virtual bool init(); | |
CREATE_FUNC(HelloWorld); | |
protected: | |
virtual void update(float delta); | |
virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) override; | |
private: | |
b2World * _world; | |
GLESDebugDraw * _debugDraw; | |
void initPhysics(); | |
}; | |
#endif |
Cocos2d-x ver.3.9 ... こちらで新しく試してみました。
https://gist.github.com/takashi1975/d1ea2be790035a7fdbec
↑
CC_ENABLE_BOX2D_INTEGRATION の設定は Application.mk に記述するといいみたい。
http://d.hatena.ne.jp/kimukou_26/20140411/p1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cocos2d-x ver.3.6, ver.3.7.1
プロジェクトの PreDefine で CC_ENABLE_BOX2D_INTEGRATION = 1 に設定してもコンパイルが通らない
→ ccConfig.h で以下の記述を追加した...