Merge pull request #8 from pandamicro/physics3d

Physics3d
This commit is contained in:
jianglong0156 2015-06-18 14:54:32 +08:00
commit 85c40b6580
16 changed files with 345 additions and 71 deletions

View File

@ -401,7 +401,7 @@ void Node::updateRotation3D()
float x = _rotationQuat.x, y = _rotationQuat.y, z = _rotationQuat.z, w = _rotationQuat.w; float x = _rotationQuat.x, y = _rotationQuat.y, z = _rotationQuat.z, w = _rotationQuat.w;
_rotationX = atan2f(2.f * (w * x + y * z), 1.f - 2.f * (x * x + y * y)); _rotationX = atan2f(2.f * (w * x + y * z), 1.f - 2.f * (x * x + y * y));
_rotationY = asinf(2.f * (w * y - z * x)); _rotationY = asinf(2.f * (w * y - z * x));
_rotationZ_X = atanf(2.f * (w * z + x * y) / (1.f - 2.f * (y * y + z * z))); _rotationZ_X = atan2f(2.f * (w * z + x * y), 1.f - 2.f * (y * y + z * z));
_rotationX = CC_RADIANS_TO_DEGREES(_rotationX); _rotationX = CC_RADIANS_TO_DEGREES(_rotationX);
_rotationY = CC_RADIANS_TO_DEGREES(_rotationY); _rotationY = CC_RADIANS_TO_DEGREES(_rotationY);

View File

@ -1836,8 +1836,10 @@
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\..\physics3d\CCPhysicsSprite3D.h"> <ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\..\physics3d\CCPhysicsSprite3D.h">
<Filter>physics3d</Filter> <Filter>physics3d</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\..\..\external\clipper\clipper.hpp" />
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\CCAutoPolygon.h" /> <ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\CCAutoPolygon.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\..\..\external\clipper\clipper.hpp">
<Filter>external\clipper</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\cocos2d.cpp"> <ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\cocos2d.cpp">
@ -3501,8 +3503,10 @@
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\base\CCUserDefault-winrt.cpp"> <ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\base\CCUserDefault-winrt.cpp">
<Filter>base</Filter> <Filter>base</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\external\clipper\clipper.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\CCAutoPolygon.cpp" /> <ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\CCAutoPolygon.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\external\clipper\clipper.cpp">
<Filter>external\clipper</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Filter Include="2d"> <Filter Include="2d">

View File

@ -195,6 +195,7 @@
<ClCompile Include="..\..\..\extensions\Particle3D\PU\CCPUVortexAffectorTranslator.cpp" /> <ClCompile Include="..\..\..\extensions\Particle3D\PU\CCPUVortexAffectorTranslator.cpp" />
<ClCompile Include="..\..\..\extensions\physics-nodes\CCPhysicsDebugNode.cpp" /> <ClCompile Include="..\..\..\extensions\physics-nodes\CCPhysicsDebugNode.cpp" />
<ClCompile Include="..\..\..\extensions\physics-nodes\CCPhysicsSprite.cpp" /> <ClCompile Include="..\..\..\extensions\physics-nodes\CCPhysicsSprite.cpp" />
<ClCompile Include="..\..\..\external\clipper\clipper.cpp" />
<ClCompile Include="..\..\..\external\ConvertUTF\ConvertUTF.c"> <ClCompile Include="..\..\..\external\ConvertUTF\ConvertUTF.c">
<CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</CompileAsWinRT> <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</CompileAsWinRT>
<CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</CompileAsWinRT> <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</CompileAsWinRT>
@ -662,6 +663,7 @@
<ClCompile Include="..\CCAnimation.cpp" /> <ClCompile Include="..\CCAnimation.cpp" />
<ClCompile Include="..\CCAnimationCache.cpp" /> <ClCompile Include="..\CCAnimationCache.cpp" />
<ClCompile Include="..\CCAtlasNode.cpp" /> <ClCompile Include="..\CCAtlasNode.cpp" />
<ClCompile Include="..\CCAutoPolygon.cpp" />
<ClCompile Include="..\CCCamera.cpp" /> <ClCompile Include="..\CCCamera.cpp" />
<ClCompile Include="..\CCClippingNode.cpp" /> <ClCompile Include="..\CCClippingNode.cpp" />
<ClCompile Include="..\CCClippingRectangleNode.cpp" /> <ClCompile Include="..\CCClippingRectangleNode.cpp" />
@ -715,9 +717,6 @@
<ClCompile Include="..\CCTransitionPageTurn.cpp" /> <ClCompile Include="..\CCTransitionPageTurn.cpp" />
<ClCompile Include="..\CCTransitionProgress.cpp" /> <ClCompile Include="..\CCTransitionProgress.cpp" />
<ClCompile Include="..\CCTweenFunction.cpp" /> <ClCompile Include="..\CCTweenFunction.cpp" />
<ClCompile Include="..\MarchingSquare.cpp" />
<ClCompile Include="..\SpritePolygon.cpp" />
<ClCompile Include="..\SpritePolygonCache.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\..\extensions\cocos-ext.h" /> <ClInclude Include="..\..\..\extensions\cocos-ext.h" />
@ -892,6 +891,7 @@
<ClInclude Include="..\..\..\extensions\Particle3D\PU\CCPUVortexAffectorTranslator.h" /> <ClInclude Include="..\..\..\extensions\Particle3D\PU\CCPUVortexAffectorTranslator.h" />
<ClInclude Include="..\..\..\extensions\physics-nodes\CCPhysicsDebugNode.h" /> <ClInclude Include="..\..\..\extensions\physics-nodes\CCPhysicsDebugNode.h" />
<ClInclude Include="..\..\..\extensions\physics-nodes\CCPhysicsSprite.h" /> <ClInclude Include="..\..\..\extensions\physics-nodes\CCPhysicsSprite.h" />
<ClInclude Include="..\..\..\external\clipper\clipper.hpp" />
<ClInclude Include="..\..\..\external\ConvertUTF\ConvertUTF.h" /> <ClInclude Include="..\..\..\external\ConvertUTF\ConvertUTF.h" />
<ClInclude Include="..\..\..\external\edtaa3func\edtaa3func.h" /> <ClInclude Include="..\..\..\external\edtaa3func\edtaa3func.h" />
<ClInclude Include="..\..\..\external\flatbuffers\flatbuffers.h" /> <ClInclude Include="..\..\..\external\flatbuffers\flatbuffers.h" />
@ -1203,6 +1203,7 @@
<ClInclude Include="..\..\platform\winrt\WICImageLoader-winrt.h" /> <ClInclude Include="..\..\platform\winrt\WICImageLoader-winrt.h" />
<ClInclude Include="..\..\renderer\CCBatchCommand.h" /> <ClInclude Include="..\..\renderer\CCBatchCommand.h" />
<ClInclude Include="..\..\renderer\CCCustomCommand.h" /> <ClInclude Include="..\..\renderer\CCCustomCommand.h" />
<ClInclude Include="..\CCAutoPolygon.h" />
<ClInclude Include="..\renderer\CCFrameBuffer.h" /> <ClInclude Include="..\renderer\CCFrameBuffer.h" />
<ClInclude Include="..\..\renderer\CCGLProgram.h" /> <ClInclude Include="..\..\renderer\CCGLProgram.h" />
<ClInclude Include="..\..\renderer\CCGLProgramCache.h" /> <ClInclude Include="..\..\renderer\CCGLProgramCache.h" />
@ -1328,9 +1329,6 @@
<ClInclude Include="..\CCTransitionPageTurn.h" /> <ClInclude Include="..\CCTransitionPageTurn.h" />
<ClInclude Include="..\CCTransitionProgress.h" /> <ClInclude Include="..\CCTransitionProgress.h" />
<ClInclude Include="..\CCTweenFunction.h" /> <ClInclude Include="..\CCTweenFunction.h" />
<ClInclude Include="..\MarchingSquare.h" />
<ClInclude Include="..\SpritePolygon.h" />
<ClInclude Include="..\SpritePolygonCache.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="..\..\base\CCController-iOS.mm" /> <None Include="..\..\base\CCController-iOS.mm" />

View File

@ -251,6 +251,9 @@
<Filter Include="navmesh"> <Filter Include="navmesh">
<UniqueIdentifier>{cc4f6ca9-4231-479d-8e19-ede4dff3382e}</UniqueIdentifier> <UniqueIdentifier>{cc4f6ca9-4231-479d-8e19-ede4dff3382e}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="external\clipper">
<UniqueIdentifier>{e3a5fe25-a92e-4c98-9026-782b2b5b1388}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\cocos2d.cpp" /> <ClCompile Include="..\..\cocos2d.cpp" />
@ -1797,15 +1800,6 @@
<ClCompile Include="..\..\platform\winrt\WICImageLoader-winrt.cpp"> <ClCompile Include="..\..\platform\winrt\WICImageLoader-winrt.cpp">
<Filter>platform\winrt</Filter> <Filter>platform\winrt</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\MarchingSquare.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\SpritePolygon.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\SpritePolygonCache.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\..\..\external\poly2tri\common\shapes.cc"> <ClCompile Include="..\..\..\external\poly2tri\common\shapes.cc">
<Filter>external\poly2tri\common</Filter> <Filter>external\poly2tri\common</Filter>
</ClCompile> </ClCompile>
@ -1903,6 +1897,12 @@
<ClCompile Include="..\..\navmesh\CCNavMeshUtils.cpp"> <ClCompile Include="..\..\navmesh\CCNavMeshUtils.cpp">
<Filter>navmesh</Filter> <Filter>navmesh</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\CCAutoPolygon.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\..\..\external\clipper\clipper.cpp">
<Filter>external\clipper</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\cocos2d.h" /> <ClInclude Include="..\..\cocos2d.h" />
@ -3629,15 +3629,6 @@
<ClInclude Include="..\..\platform\winrt\WICImageLoader-winrt.h"> <ClInclude Include="..\..\platform\winrt\WICImageLoader-winrt.h">
<Filter>platform\winrt</Filter> <Filter>platform\winrt</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\MarchingSquare.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\SpritePolygon.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\SpritePolygonCache.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\..\..\external\poly2tri\poly2tri.h"> <ClInclude Include="..\..\..\external\poly2tri\poly2tri.h">
<Filter>external\poly2tri</Filter> <Filter>external\poly2tri</Filter>
</ClInclude> </ClInclude>
@ -3734,6 +3725,12 @@
<ClInclude Include="..\..\navmesh\CCNavMeshUtils.h"> <ClInclude Include="..\..\navmesh\CCNavMeshUtils.h">
<Filter>navmesh</Filter> <Filter>navmesh</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\CCAutoPolygon.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\..\..\external\clipper\clipper.hpp">
<Filter>external\clipper</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="..\..\base\CCController-iOS.mm"> <None Include="..\..\base\CCController-iOS.mm">

View File

@ -613,10 +613,11 @@ void Mesh::setLightUniforms(Pass* pass, Scene* scene, const Vec4& color, unsigne
if (hasAmbient) if (hasAmbient)
{ {
ambient.x /= 255.f; ambient.y /= 255.f; ambient.z /= 255.f; ambient.x /= 255.f; ambient.y /= 255.f; ambient.z /= 255.f;
} //override the uniform value of u_color using the calculated color
glProgramState->setUniformVec4("u_color", Vec4(color.x * ambient.x, color.y * ambient.y, color.z * ambient.z, color.w)); glProgramState->setUniformVec4("u_color", Vec4(color.x * ambient.x, color.y * ambient.y, color.z * ambient.z, color.w));
} }
} }
}
void Mesh::setBlendFunc(const BlendFunc &blendFunc) void Mesh::setBlendFunc(const BlendFunc &blendFunc)
{ {

View File

@ -159,11 +159,9 @@ void Terrain::onDraw(const Mat4 &transform, uint32_t flags)
if(_isCameraViewChanged ) if(_isCameraViewChanged )
{ {
auto camPos = camera->getPosition3D(); auto m = camera->getNodeToWorldTransform();
auto camModelMat = camera->getNodeToWorldTransform();
camModelMat.transformPoint(&camPos);
//set lod //set lod
setChunksLOD(camPos); setChunksLOD(Vec3(m.m[12], m.m[13], m.m[14]));
} }
if(_isCameraViewChanged ) if(_isCameraViewChanged )
@ -265,7 +263,7 @@ void Terrain::setChunksLOD(Vec3 cameraPos)
{ {
AABB aabb = _chunkesArray[m][n]->_parent->_worldSpaceAABB; AABB aabb = _chunkesArray[m][n]->_parent->_worldSpaceAABB;
auto center = aabb.getCenter(); auto center = aabb.getCenter();
float dist = Vec3(center.x,0,center.z).distance(Vec3(cameraPos.x,0,cameraPos.z)); float dist = Vec2(center.x, center.z).distance(Vec2(cameraPos.x, cameraPos.z));
_chunkesArray[m][n]->_currentLod = 3; _chunkesArray[m][n]->_currentLod = 3;
for(int i =0;i<3;i++) for(int i =0;i<3;i++)
{ {

View File

@ -3891,6 +3891,89 @@ bool js_cocos2dx_ccmat4MultiplyVec3(JSContext *cx, uint32_t argc, jsval *vp)
return false; return false;
} }
bool js_cocos2dx_ccmat4GetInversed(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
if(argc == 1)
{
cocos2d::Mat4 arg0;
bool ok = jsval_to_matrix(cx, args.get(0), &arg0);
JSB_PRECONDITION2(ok, cx, false, "Error processing arguments");
jsval jsret = matrix_to_jsval(cx, arg0.getInversed());
args.rval().set(jsret);
return true;
}
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
return false;
}
bool js_cocos2dx_ccmat4TransformVector(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
if(argc >= 2)
{
cocos2d::Mat4 arg0;
cocos2d::Vec4 arg1;
cocos2d::Vec4 ret;
bool ok = jsval_to_matrix(cx, args.get(0), &arg0);
ok &= jsval_to_vector4(cx, args.get(1), &arg1);
JSB_PRECONDITION2(ok, cx, false, "Error processing arguments");
arg0.transformVector(arg1, &ret);
jsval jsret = vector4_to_jsval(cx, ret);
args.rval().set(jsret);
return true;
}
else if (argc >= 5)
{
cocos2d::Mat4 arg0;
double arg1 = 0.0,arg2 = 0.0,arg3 = 0.0, arg4 = 0.0;
cocos2d::Vec3 ret;
bool ok = jsval_to_matrix(cx, args.get(0), &arg0) &&
JS::ToNumber(cx, args.get(1), &arg1) &&
JS::ToNumber(cx, args.get(2), &arg2) &&
JS::ToNumber(cx, args.get(3), &arg3) &&
JS::ToNumber(cx, args.get(4), &arg4);
JSB_PRECONDITION2(ok, cx, false, "Error processing arguments");
arg0.transformVector(arg1, arg2, arg3, arg4, &ret);
jsval jsret = vector3_to_jsval(cx, ret);
args.rval().set(jsret);
return true;
}
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 3);
return false;
}
bool js_cocos2dx_ccmat4TransformPoint(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
if(argc >= 2)
{
cocos2d::Mat4 arg0;
cocos2d::Vec3 arg1;
cocos2d::Vec3 ret;
bool ok = jsval_to_matrix(cx, args.get(0), &arg0);
ok &= jsval_to_vector3(cx, args.get(1), &arg1);
JSB_PRECONDITION2(ok, cx, false, "Error processing arguments");
arg0.transformPoint(arg1, &ret);
jsval jsret = vector3_to_jsval(cx, ret);
args.rval().set(jsret);
return true;
}
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 3);
return false;
}
bool js_cocos2dx_ccquatMultiply(JSContext *cx, uint32_t argc, jsval *vp) bool js_cocos2dx_ccquatMultiply(JSContext *cx, uint32_t argc, jsval *vp)
{ {
JS::CallArgs args = JS::CallArgsFromVp(argc, vp); JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
@ -5167,15 +5250,15 @@ bool js_cocos2dx_Camera_unproject(JSContext *cx, uint32_t argc, jsval *vp)
js_proxy_t *proxy = jsb_get_js_proxy(obj); js_proxy_t *proxy = jsb_get_js_proxy(obj);
cocos2d::Camera* cobj = (cocos2d::Camera *)(proxy ? proxy->ptr : NULL); cocos2d::Camera* cobj = (cocos2d::Camera *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_Camera_unproject : Invalid Native Object"); JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_Camera_unproject : Invalid Native Object");
if (argc == 2) { if (argc >= 2) {
cocos2d::Size arg0; cocos2d::Size arg0;
cocos2d::Vec3 arg1; cocos2d::Vec3 arg1;
cocos2d::Vec3 arg2; cocos2d::Vec3 ret;
ok &= jsval_to_ccsize(cx, args.get(0), &arg0); ok &= jsval_to_ccsize(cx, args.get(0), &arg0);
ok &= jsval_to_vector3(cx, args.get(1), &arg1); ok &= jsval_to_vector3(cx, args.get(1), &arg1);
JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Camera_unproject : Error processing arguments"); JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Camera_unproject : Error processing arguments");
cobj->unproject(arg0, &arg1, &arg2); cobj->unproject(arg0, &arg1, &ret);
args.rval().set(vector3_to_jsval(cx, arg2)); args.rval().set(vector3_to_jsval(cx, ret));
return true; return true;
} }
else if (argc == 1) else if (argc == 1)
@ -5187,7 +5270,7 @@ bool js_cocos2dx_Camera_unproject(JSContext *cx, uint32_t argc, jsval *vp)
args.rval().set(vector3_to_jsval(cx, ret)); args.rval().set(vector3_to_jsval(cx, ret));
return true; return true;
} }
JS_ReportError(cx, "js_cocos2dx_Camera_unproject : wrong number of arguments: %d, was expecting %d", argc, 2); JS_ReportError(cx, "js_cocos2dx_Camera_unproject : wrong number of arguments: %d, was expecting %d", argc, 3);
return false; return false;
} }
@ -6237,6 +6320,9 @@ void register_cocos2dx_js_core(JSContext* cx, JS::HandleObject global)
JS_DefineFunction(cx, tmpObj, "mat4CreateRotation", js_cocos2dx_ccmat4CreateRotation, 1, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, tmpObj, "mat4CreateRotation", js_cocos2dx_ccmat4CreateRotation, 1, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, tmpObj, "mat4Multiply", js_cocos2dx_ccmat4Multiply, 2, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, tmpObj, "mat4Multiply", js_cocos2dx_ccmat4Multiply, 2, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, tmpObj, "mat4MultiplyVec3", js_cocos2dx_ccmat4MultiplyVec3, 2, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, tmpObj, "mat4MultiplyVec3", js_cocos2dx_ccmat4MultiplyVec3, 2, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, tmpObj, "mat4GetInversed", js_cocos2dx_ccmat4GetInversed, 1, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, tmpObj, "mat4TransformVector", js_cocos2dx_ccmat4TransformVector, 3, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, tmpObj, "mat4TransformPoint", js_cocos2dx_ccmat4TransformPoint, 3, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, tmpObj, "quatMultiply", js_cocos2dx_ccquatMultiply, 2, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, tmpObj, "quatMultiply", js_cocos2dx_ccquatMultiply, 2, JSPROP_READONLY | JSPROP_PERMANENT);
js_register_cocos2dx_EventKeyboard(cx, ccObj); js_register_cocos2dx_EventKeyboard(cx, ccObj);

View File

@ -2717,6 +2717,22 @@ jsval vector3_to_jsval(JSContext *cx, const cocos2d::Vec3& v)
return JSVAL_NULL; return JSVAL_NULL;
} }
jsval vector4_to_jsval(JSContext *cx, const cocos2d::Vec4& v)
{
JS::RootedObject proto(cx);
JS::RootedObject parent(cx);
JS::RootedObject tmp(cx, JS_NewObject(cx, NULL, proto, parent));
if (!tmp) return JSVAL_NULL;
bool ok = JS_DefineProperty(cx, tmp, "x", v.x, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
JS_DefineProperty(cx, tmp, "y", v.y, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
JS_DefineProperty(cx, tmp, "z", v.z, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
JS_DefineProperty(cx, tmp, "w", v.z, JSPROP_ENUMERATE | JSPROP_PERMANENT);
if (ok) {
return OBJECT_TO_JSVAL(tmp);
}
return JSVAL_NULL;
}
jsval blendfunc_to_jsval(JSContext *cx, const cocos2d::BlendFunc& v) jsval blendfunc_to_jsval(JSContext *cx, const cocos2d::BlendFunc& v)
{ {
JS::RootedObject proto(cx); JS::RootedObject proto(cx);

View File

@ -337,6 +337,7 @@ jsval std_vector_float_to_jsval( JSContext *cx, const std::vector<float>& v);
jsval matrix_to_jsval(JSContext *cx, const cocos2d::Mat4& v); jsval matrix_to_jsval(JSContext *cx, const cocos2d::Mat4& v);
jsval vector2_to_jsval(JSContext *cx, const cocos2d::Vec2& v); jsval vector2_to_jsval(JSContext *cx, const cocos2d::Vec2& v);
jsval vector3_to_jsval(JSContext *cx, const cocos2d::Vec3& v); jsval vector3_to_jsval(JSContext *cx, const cocos2d::Vec3& v);
jsval vector4_to_jsval(JSContext *cx, const cocos2d::Vec4& v);
jsval blendfunc_to_jsval(JSContext *cx, const cocos2d::BlendFunc& v); jsval blendfunc_to_jsval(JSContext *cx, const cocos2d::BlendFunc& v);
jsval vector_vec2_to_jsval(JSContext *cx, const std::vector<cocos2d::Vec2>& v); jsval vector_vec2_to_jsval(JSContext *cx, const std::vector<cocos2d::Vec2>& v);

View File

@ -61,6 +61,10 @@ bool jsval_to_physics3DRigidBodyDes(JSContext* cx, JS::HandleValue v, Physics3DR
jsval_to_matrix(cx, tmp, &m4); jsval_to_matrix(cx, tmp, &m4);
des->originalTransform = m4; des->originalTransform = m4;
} }
if(JS_GetProperty(cx, jsobj, "disableSleep", &tmp))
{
des->disableSleep = tmp.toBoolean();
}
return true; return true;
} }
@ -299,6 +303,90 @@ bool js_cocos2dx_physics3d_Physics3dShape_createHeightfield(JSContext *cx, uint3
return false; return false;
} }
bool jsval_to_Physics3DWorld_HitResult(JSContext *cx, JS::HandleValue v, cocos2d::Physics3DWorld::HitResult* ret)
{
JS::RootedObject tmp(cx);
JS::RootedValue jshitPosition(cx);
JS::RootedValue jshitNormal(cx);
JS::RootedValue jshitObject(cx);
bool ok = v.isObject() &&
JS_ValueToObject(cx, v, &tmp) &&
JS_GetProperty(cx, tmp, "hitPosition", &jshitPosition) &&
JS_GetProperty(cx, tmp, "hitNormal", &jshitNormal) &&
JS_GetProperty(cx, tmp, "hitObj", &jshitObject) &&
jsval_to_vector3(cx, jshitPosition, &(ret->hitPosition)) &&
jsval_to_vector3(cx, jshitNormal, &(ret->hitNormal));
JSB_PRECONDITION2(ok, cx, false, "jsval_to_Physics3DWorld_HitResult : Error processing arguments");
js_proxy_t *proxy = jsb_get_js_proxy(jshitObject.toObjectOrNull());
ret->hitObj = (cocos2d::Physics3DObject *)(proxy ? proxy->ptr : nullptr);
return true;
}
jsval Physics3DWorld_HitResult_to_jsval(JSContext *cx, const cocos2d::Physics3DWorld::HitResult& v)
{
JS::RootedObject proto(cx);
JS::RootedObject parent(cx);
JS::RootedObject tmp(cx, JS_NewObject(cx, nullptr, proto, parent));
if (!tmp) return JSVAL_NULL;
JS::RootedValue hitPosition(cx, vector3_to_jsval(cx, v.hitPosition));
JS::RootedValue hitNormal(cx, vector3_to_jsval(cx, v.hitNormal));
JS::RootedValue hitobject(cx);
if (v.hitObj)
{
js_proxy_t *jsProxy = js_get_or_create_proxy<cocos2d::Physics3DObject>(cx, (cocos2d::Physics3DObject*)v.hitObj);
hitobject.set(OBJECT_TO_JSVAL(jsProxy->obj));
}
bool ok = JS_DefineProperty(cx, tmp, "hitPosition", hitPosition, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
JS_DefineProperty(cx, tmp, "hitNormal", hitNormal, JSPROP_ENUMERATE | JSPROP_PERMANENT) &&
JS_DefineProperty(cx, tmp, "hitObj", hitobject, JSPROP_ENUMERATE | JSPROP_PERMANENT);
if (ok) {
return OBJECT_TO_JSVAL(tmp);
}
return JSVAL_NULL;
}
bool js_cocos2dx_physics3d_Physics3DWorld_rayCast(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
bool ok = true;
JS::RootedObject obj(cx, args.thisv().toObjectOrNull());
js_proxy_t *proxy = jsb_get_js_proxy(obj);
cocos2d::Physics3DWorld* cobj = (cocos2d::Physics3DWorld *)(proxy ? proxy->ptr : NULL);
JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_physics3d_Physics3DWorld_rayCast : Invalid Native Object");
if (argc >= 2) {
cocos2d::Vec3 arg0;
cocos2d::Vec3 arg1;
cocos2d::Physics3DWorld::HitResult ret;
ok &= jsval_to_vector3(cx, args.get(0), &arg0);
ok &= jsval_to_vector3(cx, args.get(1), &arg1);
JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_physics3d_Physics3DWorld_rayCast : Error processing arguments");
bool succeed = cobj->rayCast(arg0, arg1, &ret);
if (succeed)
{
jsval jsret = JSVAL_NULL;
jsret = Physics3DWorld_HitResult_to_jsval(cx, ret);
args.rval().set(jsret);
}
else
{
args.rval().set(JSVAL_FALSE);
}
return true;
}
JS_ReportError(cx, "js_cocos2dx_physics3d_Physics3DWorld_rayCast : wrong number of arguments: %d, was expecting %d", argc, 3);
return false;
}
void register_all_cocos2dx_physics3d_manual(JSContext *cx, JS::HandleObject global) void register_all_cocos2dx_physics3d_manual(JSContext *cx, JS::HandleObject global)
{ {
JS::RootedObject ccObj(cx); JS::RootedObject ccObj(cx);
@ -319,6 +407,8 @@ void register_all_cocos2dx_physics3d_manual(JSContext *cx, JS::HandleObject glob
JS_DefineFunction(cx, tmpObj, "createHeightfield", js_cocos2dx_physics3d_Physics3dShape_createHeightfield, 8, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, tmpObj, "createHeightfield", js_cocos2dx_physics3d_Physics3dShape_createHeightfield, 8, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, JS::RootedObject(cx, jsb_cocos2d_Physics3DObject_prototype), "setCollisionCallback", jsb_cocos2d_Physics3DObject_setCollisionCallback, 2, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, JS::RootedObject(cx, jsb_cocos2d_Physics3DObject_prototype), "setCollisionCallback", jsb_cocos2d_Physics3DObject_setCollisionCallback, 2, JSPROP_READONLY | JSPROP_PERMANENT);
JS_DefineFunction(cx, JS::RootedObject(cx, jsb_cocos2d_Physics3DWorld_prototype), "rayCast", js_cocos2dx_physics3d_Physics3DWorld_rayCast, 2, JSPROP_READONLY | JSPROP_PERMANENT);
} }
#endif //CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION #endif //CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION

View File

@ -41,3 +41,18 @@ cc.Physics3DComponent.PhysicsSyncFlag = {
PHYSICS_TO_NODE : 2, // align physics transform to the node PHYSICS_TO_NODE : 2, // align physics transform to the node
NODE_AND_NODE : 1 | 2, //pre simulation, align the physics object to the node and align the node transform according to physics object after simulation NODE_AND_NODE : 1 | 2, //pre simulation, align the physics object to the node and align the node transform according to physics object after simulation
}; };
cc.HitResult = function(){
this.hitPosition = cc.math.vec3();
this.hitNormal = cc.math.vec3();
this.hitObj = null;
}
cc.hitResult = function(){
return new cc.HitResult();
};
cc.Physics3DObject.PhysicsObjType = {
UNKNOWN : 0,
RIGID_BODY : 1,
};

View File

@ -41,6 +41,7 @@ var Physics3DTestDemo = cc.Layer.extend({
_camera:null, _camera:null,
_angle:0.0, _angle:0.0,
_needShootBox:false, _needShootBox:false,
_listener: null,
ctor:function () { ctor:function () {
this._super(); this._super();
@ -52,7 +53,7 @@ var Physics3DTestDemo = cc.Layer.extend({
this._camera.setCameraFlag(cc.CameraFlag.USER1); this._camera.setCameraFlag(cc.CameraFlag.USER1);
this.addChild(this._camera); this.addChild(this._camera);
cc.eventManager.addListener({ this._listener = cc.eventManager.addListener({
event:cc.EventListener.TOUCH_ALL_AT_ONCE, event:cc.EventListener.TOUCH_ALL_AT_ONCE,
onTouchesBegan:this.onTouchesBegan.bind(this), onTouchesBegan:this.onTouchesBegan.bind(this),
onTouchesMoved:this.onTouchesMoved.bind(this), onTouchesMoved:this.onTouchesMoved.bind(this),
@ -285,10 +286,16 @@ var BasicPhysics3DDemo = Physics3DTestDemo.extend({
var Physics3DConstraintDemo = Physics3DTestDemo.extend({ var Physics3DConstraintDemo = Physics3DTestDemo.extend({
_subtitle:"Physics3D Constraint", _subtitle:"Physics3D Constraint",
_constraint: null,
_world: null,
ctor:function(){ ctor:function(){
this._super(); this._super();
this._listener.onTouchesBegan = this.onTouchesBegan.bind(this);
this._listener.onTouchesMoved = this.onTouchesMoved.bind(this);
this._listener.onTouchesEnded = this.onTouchesEnded.bind(this);
//PhysicsSprite3d = Sprite3D + Physics3DComponent //PhysicsSprite3d = Sprite3D + Physics3DComponent
var rbDes = cc.physics3DRigidBodyDes(); var rbDes = cc.physics3DRigidBodyDes();
rbDes.disableSleep = true; rbDes.disableSleep = true;
@ -309,10 +316,11 @@ var Physics3DConstraintDemo = Physics3DTestDemo.extend({
component.setSyncFlag(cc.Physics3DComponent.PhysicsSyncFlag.PHYSICS_TO_NODE); component.setSyncFlag(cc.Physics3DComponent.PhysicsSyncFlag.PHYSICS_TO_NODE);
physicsScene.setPhysics3DDebugCamera(this._camera); physicsScene.setPhysics3DDebugCamera(this._camera);
this._world = physicsScene.getPhysics3DWorld();
//create point to point constraint //create point to point constraint
var constraint = cc.Physics3DPointToPointConstraint.create(rigidBody, cc.math.vec3(2.5, 2.5, 2.5)); var constraint = cc.Physics3DPointToPointConstraint.create(rigidBody, cc.math.vec3(2.5, 2.5, 2.5));
physicsScene.getPhysics3DWorld().addPhysics3DConstraint(constraint); this._world.addPhysics3DConstraint(constraint);
//create hinge constraint //create hinge constraint
rbDes.mass = 1; rbDes.mass = 1;
@ -322,7 +330,7 @@ var Physics3DConstraintDemo = Physics3DTestDemo.extend({
sprite = new jsb.Sprite3D("Sprite3DTest/box.c3t"); sprite = new jsb.Sprite3D("Sprite3DTest/box.c3t");
sprite.setTexture("Sprite3DTest/plane.png"); sprite.setTexture("Sprite3DTest/plane.png");
sprite.setScaleX(8); sprite.setScaleX(8);
sprite.setScaleZ(8); sprite.setScaleY(8);
sprite.setPosition3D(cc.math.vec3(5, 0, 0)); sprite.setPosition3D(cc.math.vec3(5, 0, 0));
sprite.addComponent(component); sprite.addComponent(component);
sprite.setCameraMask(cc.CameraFlag.USER1); sprite.setCameraMask(cc.CameraFlag.USER1);
@ -330,7 +338,7 @@ var Physics3DConstraintDemo = Physics3DTestDemo.extend({
component.syncNodeToPhysics(); component.syncNodeToPhysics();
rigidBody.setAngularVelocity(cc.math.vec3(0, 3, 0)); rigidBody.setAngularVelocity(cc.math.vec3(0, 3, 0));
constraint = cc.Physics3DHingeConstraint.create(rigidBody, cc.math.vec3(4, 4, 0.5), cc.math.vec3(0, 1, 0)); constraint = cc.Physics3DHingeConstraint.create(rigidBody, cc.math.vec3(4, 4, 0.5), cc.math.vec3(0, 1, 0));
physicsScene.getPhysics3DWorld().addPhysics3DConstraint(constraint); this._world.addPhysics3DConstraint(constraint);
//create slider constraint //create slider constraint
rbDes.mass = 1; rbDes.mass = 1;
@ -361,10 +369,10 @@ var Physics3DConstraintDemo = Physics3DTestDemo.extend({
this.addChild(sprite); this.addChild(sprite);
component.syncNodeToPhysics(); component.syncNodeToPhysics();
var frameInA = [-4.37114e-08, 1, 0, 0, -1, -4.37114e-08, 0, 0, 0, 0, 1, 0, 0, -5, 0, 1]; var frameInA = [-4.37114e-8, 1, 0, 0, -1, -4.37114e-8, 0, 0, 0, 0, 1, 0, 0, -5, 0, 1];
var frameInB = [-4.37114e-08, 1, 0, 0, -1, -4.37114e-08, 0, 0, 0, 0, 1, 0, 0, 5, 0, 1]; var frameInB = [-4.37114e-8, 1, 0, 0, -1, -4.37114e-8, 0, 0, 0, 0, 1, 0, 0, 5, 0, 1];
constraint = cc.Physics3DSliderConstraint.create(rigidBody, rigidBodyB, frameInA, frameInB, false); constraint = cc.Physics3DSliderConstraint.create(rigidBody, rigidBodyB, frameInA, frameInB, false);
physicsScene.getPhysics3DWorld().addPhysics3DConstraint(constraint); this._world.addPhysics3DConstraint(constraint);
constraint.setLowerLinLimit(-5); constraint.setLowerLinLimit(-5);
constraint.setUpperLinLimit(5); constraint.setUpperLinLimit(5);
@ -382,9 +390,9 @@ var Physics3DConstraintDemo = Physics3DTestDemo.extend({
this.addChild(sprite); this.addChild(sprite);
component.syncNodeToPhysics(); component.syncNodeToPhysics();
frameInA = [-4.37114e-08, 1, 0, 0, -1, -4.37114e-08, 0, 0, 0, 0, 1, 0, 0, -10, 0, 1]; frameInA = [-4.37114e-8, 1, 0, 0, -1, -4.37114e-8, 0, 0, 0, 0, 1, 0, 0, -10, 0, 1];
constraint = cc.Physics3DConeTwistConstraint.create(rigidBody, frameInA); constraint = cc.Physics3DConeTwistConstraint.create(rigidBody, frameInA);
physicsScene.getPhysics3DWorld().addPhysics3DConstraint(constraint, true); this._world.addPhysics3DConstraint(constraint, true);
constraint.setLimit(cc.degreesToRadians(10), cc.degreesToRadians(10), cc.degreesToRadians(40)); constraint.setLimit(cc.degreesToRadians(10), cc.degreesToRadians(10), cc.degreesToRadians(40));
//create 6 dof constraint //create 6 dof constraint
@ -403,11 +411,68 @@ var Physics3DConstraintDemo = Physics3DTestDemo.extend({
frameInA = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]; frameInA = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
constraint = cc.Physics3D6DofConstraint.create(rigidBody, frameInA, false); constraint = cc.Physics3D6DofConstraint.create(rigidBody, frameInA, false);
physicsScene.getPhysics3DWorld().addPhysics3DConstraint(constraint); this._world.addPhysics3DConstraint(constraint);
constraint.setAngularLowerLimit(cc.math.vec3(0, 0, 0)); constraint.setAngularLowerLimit(cc.math.vec3(0, 0, 0));
constraint.setAngularUpperLimit(cc.math.vec3(0, 0, 0)); constraint.setAngularUpperLimit(cc.math.vec3(0, 0, 0));
constraint.setLinearLowerLimit(cc.math.vec3(-10, 0, 0)); constraint.setLinearLowerLimit(cc.math.vec3(-10, 0, 0));
constraint.setLinearUpperLimit(cc.math.vec3(10, 0, 0)); constraint.setLinearUpperLimit(cc.math.vec3(10, 0, 0));
},
onTouchesBegan:function(touches, event){
if(this._camera){
cc.log("STARTED " + touches.length);
var touch = touches[0];
var location = touch.getLocationInView();
var nearP = cc.math.vec3(location.x, location.y, 0);
var farP = cc.math.vec3(location.x, location.y, 1);
var size = director.getWinSize();
nearP = this._camera.unproject(size, nearP);
farP = this._camera.unproject(size, farP);
var result = this._world.rayCast(nearP, farP);
if(result !== false && result.hitObj.getObjType() == cc.Physics3DObject.PhysicsObjType.RIGID_BODY)
{
var mat = cc.math.mat4GetInversed(result.hitObj.getWorldTransform());
var position = cc.math.mat4TransformPoint(mat, result.hitPosition);
this._constraint = cc.Physics3DPointToPointConstraint.create(result.hitObj, position);
this._world.addPhysics3DConstraint(this._constraint, true);
this._pickingDistance = cc.math.vec3Length(cc.math.vec3Sub(result.hitPosition, nearP));
return;
}
}
Physics3DTestDemo.prototype.onTouchesBegan.call(this, touches, event);
this._needShootBox = false;
},
onTouchesMoved:function(touches, event){
if(this._constraint){
var touch = touches[0];
var location = touch.getLocationInView();
var nearP = cc.math.vec3(location.x, location.y, 0);
var farP = cc.math.vec3(location.x, location.y, 1);
var size = director.getWinSize();
nearP = this._camera.unproject(size, nearP);
farP = this._camera.unproject(size, farP);
var dir = cc.math.vec3Normalize(cc.math.vec3Sub(farP, nearP));
this._constraint.setPivotPointInB(cc.math.vec3Add(nearP, cc.math.vec3(dir.x * this._pickingDistance, dir.y * this._pickingDistance, dir.z * this._pickingDistance)));
return;
}
Physics3DTestDemo.prototype.onTouchesMoved.call(this, touches, event);
},
onTouchesEnded:function(touches, event){
if(this._constraint){
this._world.removePhysics3DConstraint(this._constraint);
this._constraint = null;
return;
}
Physics3DTestDemo.prototype.onTouchesEnded.call(this, touches, event);
} }
}); });

View File

@ -391,7 +391,10 @@ function Physics3DConstraintDemo:extend()
component:syncNodeToPhysics() component:syncNodeToPhysics()
local frameInA = cc.mat4.createRotationZ(cc.mat4.createIdentity(), math.pi / 2) local frameInA = cc.mat4.createRotationZ(cc.mat4.createIdentity(), math.pi / 2)
local frameInB = frameInA local frameInB = {}
for i=1,16 do
frameInB[i] = frameInA[i]
end
frameInA[14] = -5.0 frameInA[14] = -5.0
frameInB[14] = 5.0 frameInB[14] = 5.0
constraint = cc.Physics3DSliderConstraint:create(rigidBody, rigidBodyB, frameInA, frameInB, false) constraint = cc.Physics3DSliderConstraint:create(rigidBody, rigidBodyB, frameInA, frameInB, false)

View File

@ -42,7 +42,7 @@ classes = Physics3DWorld Physics3DShape PhysicsSprite3D Physics3DObject Physics3
skip = Physics3DShape::[createCompoundShape createMesh createHeightfield], skip = Physics3DShape::[createCompoundShape createMesh createHeightfield],
Physics3DObject::[setCollisionCallback], Physics3DObject::[setCollisionCallback],
Physics3DWorld::[getPhysicsObjects], Physics3DWorld::[getPhysicsObjects rayCast],
Physics3DConeTwistConstraint::[setMotorTarget setMotorTargetInConstraintSpace], Physics3DConeTwistConstraint::[setMotorTarget setMotorTargetInConstraintSpace],
PhysicsSprite3D::[create], PhysicsSprite3D::[create],
Physics3DRigidBody::[create] Physics3DRigidBody::[create]