Skip to content

Commit

Permalink
add physics3d support
Browse files Browse the repository at this point in the history
  • Loading branch information
super626 committed May 8, 2015
1 parent f0e16bb commit 2391558
Show file tree
Hide file tree
Showing 54 changed files with 8,169 additions and 17 deletions.
19 changes: 19 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ endif()

option(USE_CHIPMUNK "Use chipmunk for physics library" ON)
option(USE_BOX2D "Use box2d for physics library" OFF)
option(USE_BULLET "Use bullet for physics3d library" ON)
option(USE_WEBP "Use WebP codec" ${USE_WEBP_DEFAULT})
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
option(DEBUG_MODE "Debug or release?" ON)
Expand Down Expand Up @@ -248,6 +249,24 @@ else()
add_definitions(-DCC_ENABLE_BOX2D_INTEGRATION=0)
endif(USE_BOX2D)

# Bullet (not prebuilded, exists as source)
if(USE_BULLET)
if(USE_PREBUILT_LIBS)
add_subdirectory(external/bullet)
set(BULLET_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/external/bullet)
set(BULLET_LIBRARIES bullet)
else()
cocos_find_package(bullet BULLET REQUIRED)
set(BULLET_LIBRARIES bullet)
endif()
message(STATUS "Bullet include dirs: ${BULLET_INCLUDE_DIRS}")
add_definitions(-DCC_ENABLE_BULLET_INTEGRATION=1)
add_definitions(-DCC_USE_PHYSICS=1)
else()
add_definitions(-DCC_ENABLE_BULLET_INTEGRATION=0)
add_definitions(-DCC_USE_3D_PHYSICS=0)
endif(USE_BULLET)

# Tinyxml2 (not prebuilded, exists as source)
if(USE_PREBUILT_LIBS)
add_subdirectory(external/tinyxml2)
Expand Down
14 changes: 13 additions & 1 deletion build/cocos2d-win32.vc2013.sln
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 12.0.21005.1
Expand All @@ -12,6 +12,9 @@ EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lua-empty-test", "..\tests\lua-empty-test\project\proj.win32\lua-empty-test.vcxproj", "{13E55395-94A2-4CD9-BFC2-1A051F80C17D}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libcocos2d", "..\cocos\2d\libcocos2d.vcxproj", "{98A51BA8-FC3A-415B-AC8F-8C7BD464E93E}"
ProjectSection(ProjectDependencies) = postProject
{012DFF48-A13F-4F52-B07B-F8B9D21CE95B} = {012DFF48-A13F-4F52-B07B-F8B9D21CE95B}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libluacocos2d", "..\cocos\scripting\lua-bindings\proj.win32\libluacocos2d.vcxproj", "{9F2D6CE6-C893-4400-B50C-6DB70CC2562F}"
EndProject
Expand All @@ -21,6 +24,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libSpine", "..\cocos\editor
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libbox2d", "..\external\Box2D\proj.win32\libbox2d.vcxproj", "{929480E7-23C0-4DF6-8456-096D71547116}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libbullet", "..\external\bullet\proj.win32\libbullet.vcxproj", "{012DFF48-A13F-4F52-B07B-F8B9D21CE95B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM = Debug|ARM
Expand Down Expand Up @@ -77,13 +82,20 @@ Global
{929480E7-23C0-4DF6-8456-096D71547116}.Release|ARM.ActiveCfg = Release|Win32
{929480E7-23C0-4DF6-8456-096D71547116}.Release|Win32.ActiveCfg = Release|Win32
{929480E7-23C0-4DF6-8456-096D71547116}.Release|Win32.Build.0 = Release|Win32
{012DFF48-A13F-4F52-B07B-F8B9D21CE95B}.Debug|ARM.ActiveCfg = Debug|Win32
{012DFF48-A13F-4F52-B07B-F8B9D21CE95B}.Debug|Win32.ActiveCfg = Debug|Win32
{012DFF48-A13F-4F52-B07B-F8B9D21CE95B}.Debug|Win32.Build.0 = Debug|Win32
{012DFF48-A13F-4F52-B07B-F8B9D21CE95B}.Release|ARM.ActiveCfg = Release|Win32
{012DFF48-A13F-4F52-B07B-F8B9D21CE95B}.Release|Win32.ActiveCfg = Release|Win32
{012DFF48-A13F-4F52-B07B-F8B9D21CE95B}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{B7C2A162-DEC9-4418-972E-240AB3CBFCAE} = {92D54E36-7916-48EF-A951-224DD3B25442}
{929480E7-23C0-4DF6-8456-096D71547116} = {92D54E36-7916-48EF-A951-224DD3B25442}
{012DFF48-A13F-4F52-B07B-F8B9D21CE95B} = {92D54E36-7916-48EF-A951-224DD3B25442}
EndGlobalSection
GlobalSection(DPCodeReviewSolutionGUID) = preSolution
DPCodeReviewSolutionGUID = {00000000-0000-0000-0000-000000000000}
Expand Down
2,653 changes: 2,653 additions & 0 deletions build/cocos2d_libs.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions build/cocos2d_tests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,8 @@
B63993321A49359F00B07923 /* Particle3D in Resources */ = {isa = PBXBuildFile; fileRef = B63993301A49359F00B07923 /* Particle3D */; };
B6C039D919C95D83007207DC /* LightTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6C039D719C95D83007207DC /* LightTest.cpp */; };
B6C039DA19C95D83007207DC /* LightTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6C039D719C95D83007207DC /* LightTest.cpp */; };
B6CAB54E1AF9AA6C00B9B856 /* Physics3DTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6CAB54C1AF9AA6C00B9B856 /* Physics3DTest.cpp */; };
B6CAB54F1AF9AA6C00B9B856 /* Physics3DTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6CAB54C1AF9AA6C00B9B856 /* Physics3DTest.cpp */; };
C04F935A1941B05400E9FEAB /* TileMapTest2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C04F93581941B05400E9FEAB /* TileMapTest2.cpp */; };
C04F935B1941B05400E9FEAB /* TileMapTest2.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C04F93581941B05400E9FEAB /* TileMapTest2.cpp */; };
C08689C118D370C90093E810 /* background.caf in Resources */ = {isa = PBXBuildFile; fileRef = C08689C018D370C90093E810 /* background.caf */; };
Expand Down Expand Up @@ -1781,6 +1783,8 @@
B63993301A49359F00B07923 /* Particle3D */ = {isa = PBXFileReference; lastKnownFileType = folder; name = Particle3D; path = "../tests/cpp-tests/Resources/Particle3D"; sourceTree = "<group>"; };
B6C039D719C95D83007207DC /* LightTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = LightTest.cpp; path = LightTest/LightTest.cpp; sourceTree = "<group>"; };
B6C039D819C95D83007207DC /* LightTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = LightTest.h; path = LightTest/LightTest.h; sourceTree = "<group>"; };
B6CAB54C1AF9AA6C00B9B856 /* Physics3DTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Physics3DTest.cpp; path = Physics3DTest/Physics3DTest.cpp; sourceTree = "<group>"; };
B6CAB54D1AF9AA6C00B9B856 /* Physics3DTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Physics3DTest.h; path = Physics3DTest/Physics3DTest.h; sourceTree = "<group>"; };
C04F93581941B05400E9FEAB /* TileMapTest2.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TileMapTest2.cpp; sourceTree = "<group>"; };
C04F93591941B05400E9FEAB /* TileMapTest2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TileMapTest2.h; sourceTree = "<group>"; };
C08689C018D370C90093E810 /* background.caf */ = {isa = PBXFileReference; lastKnownFileType = file; name = background.caf; path = "../tests/cpp-tests/Resources/background.caf"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2199,6 +2203,7 @@
1AC3592418CECF0A00F37B72 /* Classes */ = {
isa = PBXGroup;
children = (
B6CAB54B1AF9AA4B00B9B856 /* Physics3DTest */,
5046AB471AF2A8D80060550B /* MaterialSystemTest */,
6886696E1AE8E8A000C2CFD9 /* SpritePolygonTest */,
B603F1AC1AC8EA2E00A9579C /* TerrainTest */,
Expand Down Expand Up @@ -4030,6 +4035,15 @@
name = LightTest;
sourceTree = "<group>";
};
B6CAB54B1AF9AA4B00B9B856 /* Physics3DTest */ = {
isa = PBXGroup;
children = (
B6CAB54C1AF9AA6C00B9B856 /* Physics3DTest.cpp */,
B6CAB54D1AF9AA6C00B9B856 /* Physics3DTest.h */,
);
name = Physics3DTest;
sourceTree = "<group>";
};
D0FD03611A3B543700825BB5 /* AllocatorTest */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -4881,6 +4895,7 @@
1AC35C2F18CECF0C00F37B72 /* PerformanceParticleTest.cpp in Sources */,
1AC35B4918CECF0C00F37B72 /* Bug-914.cpp in Sources */,
1AC35B6318CECF0C00F37B72 /* EffectsAdvancedTest.cpp in Sources */,
B6CAB54E1AF9AA6C00B9B856 /* Physics3DTest.cpp in Sources */,
B639932E1A490EC700B07923 /* Particle3DTest.cpp in Sources */,
1AC35C5F18CECF0C00F37B72 /* Paddle.cpp in Sources */,
1AC35BDB18CECF0C00F37B72 /* SceneEditorTest.cpp in Sources */,
Expand Down Expand Up @@ -5049,6 +5064,7 @@
1AC35BEC18CECF0C00F37B72 /* CCControlSliderTest.cpp in Sources */,
29080DB4191B595E0066F8DF /* UILayoutTest_Editor.cpp in Sources */,
1AC35C4E18CECF0C00F37B72 /* SpineTest.cpp in Sources */,
B6CAB54F1AF9AA6C00B9B856 /* Physics3DTest.cpp in Sources */,
1AC35C1E18CECF0C00F37B72 /* NewRendererTest.cpp in Sources */,
5EBEECB11995247000429821 /* DrawNode3D.cpp in Sources */,
1AC35B6818CECF0C00F37B72 /* AnimationsTestLayer.cpp in Sources */,
Expand Down Expand Up @@ -5910,6 +5926,7 @@
"COCOS2D_DEBUG=1",
USE_FILE32API,
"CC_ENABLE_CHIPMUNK_INTEGRATION=1",
"CC_ENABLE_BULLET_INTEGRATION=1",
);
GCC_SYMBOLS_PRIVATE_EXTERN = YES;
GCC_TREAT_WARNINGS_AS_ERRORS = YES;
Expand Down Expand Up @@ -5938,6 +5955,7 @@
NDEBUG,
USE_FILE32API,
"CC_ENABLE_CHIPMUNK_INTEGRATION=1",
"CC_ENABLE_BULLET_INTEGRATION=1",
);
GCC_SYMBOLS_PRIVATE_EXTERN = YES;
GCC_TREAT_WARNINGS_AS_ERRORS = YES;
Expand Down
15 changes: 15 additions & 0 deletions cocos/2d/CCComponentContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,19 @@ bool ComponentContainer::isEmpty() const
return (_components == nullptr || _components->empty());
}

void ComponentContainer::onEnter()
{
for (auto iter = _components->begin(); iter != _components->end(); ++iter)
{
iter->second->onEnter();
}
}
void ComponentContainer::onExit()
{
for (auto iter = _components->begin(); iter != _components->end(); ++iter)
{
iter->second->onExit();
}
}

NS_CC_END
4 changes: 4 additions & 0 deletions cocos/2d/CCComponentContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class CC_DLL ComponentContainer
virtual bool remove(Component *com);
virtual void removeAll();
virtual void visit(float delta);

virtual void onEnter();
virtual void onExit();

public:
bool isEmpty() const;

Expand Down
71 changes: 70 additions & 1 deletion cocos/2d/CCScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,22 @@ THE SOFTWARE.
#include "physics/CCPhysicsWorld.h"
#endif

#if CC_USE_3D_PHYSICS
#include "physics3d/CCPhysics3DWorld.h"
#include "physics3d/CCPhysics3DComponent.h"
#endif

NS_CC_BEGIN

Scene::Scene()
#if CC_USE_PHYSICS
: _physicsWorld(nullptr)
#endif
{
#if CC_USE_3D_PHYSICS
_physics3DWorld = nullptr;
_physics3dDebugCamera = nullptr;
#endif
_ignoreAnchorPointForPosition = true;
setAnchorPoint(Vec2(0.5f, 0.5f));

Expand All @@ -61,6 +70,10 @@ Scene::~Scene()
{
#if CC_USE_PHYSICS
CC_SAFE_DELETE(_physicsWorld);
#endif
#if CC_USE_3D_PHYSICS
CC_SAFE_RELEASE(_physics3DWorld);
CC_SAFE_RELEASE(_physics3dDebugCamera);
#endif
Director::getInstance()->getEventDispatcher()->removeEventListener(_event);
CC_SAFE_RELEASE(_event);
Expand Down Expand Up @@ -153,10 +166,22 @@ void Scene::render(Renderer* renderer)

//visit the scene
visit(renderer, transform, 0);

renderer->render();

director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
}

#if CC_USE_3D_PHYSICS
if (_physics3DWorld && _physics3DWorld->isDebugDrawEnabled())
{
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, _physics3dDebugCamera != nullptr ? _physics3dDebugCamera->getViewProjectionMatrix() : defaultCamera->getViewProjectionMatrix());
_physics3DWorld->debugDraw(renderer);
renderer->render();
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
}
#endif

Camera::_visitingCamera = nullptr;
}
Expand All @@ -175,7 +200,16 @@ void Scene::removeAllChildren()
}
}

#if CC_USE_PHYSICS
#if CC_USE_3D_PHYSICS
void Scene::setPhysics3DDebugCamera(Camera* camera)
{
CC_SAFE_RETAIN(camera);
CC_SAFE_RELEASE(_physics3dDebugCamera);
_physics3dDebugCamera = camera;
}
#endif

#if (CC_USE_PHYSICS || CC_USE_3D_PHYSICS)
void Scene::addChild(Node* child, int zOrder, int tag)
{
Node::addChild(child, zOrder, tag);
Expand Down Expand Up @@ -212,7 +246,17 @@ bool Scene::initWithPhysics()
CC_BREAK_IF( ! (director = Director::getInstance()) );

this->setContentSize(director->getWinSize());
#if CC_USE_PHYSICS
CC_BREAK_IF(! (_physicsWorld = PhysicsWorld::construct(*this)));
#endif

#if CC_USE_3D_PHYSICS
Physics3DWorldDes info;
//TODO: FIX ME
//info.isDebugDrawEnabled = true;
CC_BREAK_IF(! (_physics3DWorld = Physics3DWorld::create(&info)));
_physics3DWorld->retain();
#endif

// success
ret = true;
Expand All @@ -222,6 +266,7 @@ bool Scene::initWithPhysics()

void Scene::addChildToPhysicsWorld(Node* child)
{
#if CC_USE_PHYSICS
if (_physicsWorld)
{
std::function<void(Node*)> addToPhysicsWorldFunc = nullptr;
Expand All @@ -242,6 +287,30 @@ void Scene::addChildToPhysicsWorld(Node* child)

addToPhysicsWorldFunc(child);
}
#endif

#if CC_USE_3D_PHYSICS
if (_physics3DWorld)
{
std::function<void(Node*)> addToPhysicsWorldFunc = nullptr;
addToPhysicsWorldFunc = [this, &addToPhysicsWorldFunc](Node* node) -> void
{
static std::string comName = Physics3DComponent::getPhysics3DComponentName();
auto com = static_cast<Physics3DComponent*>(node->getComponent(comName));
if (com)
{
com->addToPhysicsWorld(_physics3DWorld);
}

auto& children = node->getChildren();
for( const auto &n : children) {
addToPhysicsWorldFunc(n);
}
};

addToPhysicsWorldFunc(child);
}
#endif
}

#endif
Expand Down
30 changes: 28 additions & 2 deletions cocos/2d/CCScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class EventCustom;
#if CC_USE_PHYSICS
class PhysicsWorld;
#endif
#if CC_USE_3D_PHYSICS
class Physics3DWorld;
#endif
/**
* @addtogroup _2d
* @{
Expand Down Expand Up @@ -136,15 +139,31 @@ class CC_DLL Scene : public Node
private:
CC_DISALLOW_COPY_AND_ASSIGN(Scene);

#if CC_USE_PHYSICS
#if (CC_USE_PHYSICS || CC_USE_3D_PHYSICS)
public:
virtual void addChild(Node* child, int zOrder, int tag) override;
virtual void addChild(Node* child, int zOrder, const std::string &name) override;

#if CC_USE_PHYSICS
/** Get the physics world of the scene.
* @return The physics world of the scene.
* @js NA
*/
inline PhysicsWorld* getPhysicsWorld() { return _physicsWorld; }
#endif

#if CC_USE_3D_PHYSICS
/** Get the 3d physics world of the scene.
* @return The 3d physics world of the scene.
* @js NA
*/
inline Physics3DWorld* getPhysics3DWorld() { return _physics3DWorld; }

/**
* Set Physics3D debug draw camera.
*/
void setPhysics3DDebugCamera(Camera* camera);
#endif

/** Create a scene with physics.
* @return An autoreleased Scene object with physics.
Expand All @@ -158,8 +177,15 @@ class CC_DLL Scene : public Node
protected:
void addChildToPhysicsWorld(Node* child);

#if CC_USE_PHYSICS
PhysicsWorld* _physicsWorld;
#endif // CC_USE_PHYSICS
#endif

#if CC_USE_3D_PHYSICS
Physics3DWorld* _physics3DWorld;
Camera* _physics3dDebugCamera; //
#endif
#endif // (CC_USE_PHYSICS || CC_USE_3D_PHYSICS)
};

// end of _2d group
Expand Down
Loading

0 comments on commit 2391558

Please sign in to comment.