axmol/core/physics3d/CCPhysics3DShape.cpp

224 lines
7.6 KiB
C++
Raw Normal View History

2015-05-08 15:49:33 +08:00
/****************************************************************************
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
http://www.cocos2d-x.org
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "physics3d/CCPhysics3D.h"
2015-05-08 15:49:33 +08:00
#if CC_USE_3D_PHYSICS
2021-12-25 10:04:45 +08:00
# if (CC_ENABLE_BULLET_INTEGRATION)
# include "bullet/BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h"
2015-05-08 15:49:33 +08:00
NS_CC_BEGIN
Physics3DShape::ShapeType Physics3DShape::getShapeType() const
{
return _shapeType;
}
2021-12-25 10:04:45 +08:00
Physics3DShape::Physics3DShape() : _shapeType(ShapeType::UNKNOWN)
2015-05-08 15:49:33 +08:00
{
2021-12-25 10:04:45 +08:00
# if (CC_ENABLE_BULLET_INTEGRATION)
_btShape = nullptr;
2015-05-08 15:49:33 +08:00
_heightfieldData = nullptr;
2021-12-25 10:04:45 +08:00
# endif
2015-05-08 15:49:33 +08:00
}
Physics3DShape::~Physics3DShape()
{
2021-12-25 10:04:45 +08:00
# if (CC_ENABLE_BULLET_INTEGRATION)
2015-05-08 15:49:33 +08:00
CC_SAFE_DELETE(_btShape);
CC_SAFE_DELETE_ARRAY(_heightfieldData);
2021-12-25 10:04:45 +08:00
for (auto iter : _compoundChildShapes)
{
2015-05-08 15:49:33 +08:00
CC_SAFE_RELEASE(iter);
}
_compoundChildShapes.clear();
2021-12-25 10:04:45 +08:00
# endif
2015-05-08 15:49:33 +08:00
}
Physics3DShape* Physics3DShape::createBox(const cocos2d::Vec3& extent)
{
2021-12-08 00:11:53 +08:00
auto shape = new Physics3DShape();
2015-05-08 15:49:33 +08:00
shape->initBox(extent);
shape->autorelease();
return shape;
}
Physics3DShape* Physics3DShape::createSphere(float radius)
{
2021-12-08 00:11:53 +08:00
auto shape = new Physics3DShape();
2015-05-08 15:49:33 +08:00
shape->initSphere(radius);
shape->autorelease();
return shape;
}
Physics3DShape* Physics3DShape::createCylinder(float radius, float height)
{
2021-12-08 00:11:53 +08:00
auto shape = new Physics3DShape();
2015-05-08 15:49:33 +08:00
shape->initCylinder(radius, height);
shape->autorelease();
return shape;
}
Physics3DShape* Physics3DShape::createCapsule(float radius, float height)
{
2021-12-08 00:11:53 +08:00
auto shape = new Physics3DShape();
2015-05-08 15:49:33 +08:00
shape->initCapsule(radius, height);
shape->autorelease();
return shape;
}
2021-12-25 10:04:45 +08:00
Physics3DShape* Physics3DShape::createConvexHull(const cocos2d::Vec3* points, int numPoints)
2015-05-08 15:49:33 +08:00
{
2021-12-08 00:11:53 +08:00
auto shape = new Physics3DShape();
2015-05-08 15:49:33 +08:00
shape->initConvexHull(points, numPoints);
shape->autorelease();
return shape;
}
2021-12-25 10:04:45 +08:00
Physics3DShape* Physics3DShape::createMesh(const cocos2d::Vec3* triangles, int numTriangles)
2015-05-08 15:49:33 +08:00
{
2021-12-08 00:11:53 +08:00
auto shape = new Physics3DShape();
2015-05-08 15:49:33 +08:00
shape->initMesh(triangles, numTriangles);
shape->autorelease();
return shape;
}
2021-12-25 10:04:45 +08:00
Physics3DShape* Physics3DShape::createHeightfield(int heightStickWidth,
int heightStickLength,
const void* heightfieldData,
float heightScale,
float minHeight,
float maxHeight,
bool useFloatDatam,
bool flipQuadEdges,
bool useDiamondSubdivision)
2015-05-08 15:49:33 +08:00
{
2021-12-08 00:11:53 +08:00
auto shape = new Physics3DShape();
2021-12-25 10:04:45 +08:00
shape->initHeightfield(heightStickWidth, heightStickLength, heightfieldData, heightScale, minHeight, maxHeight,
useFloatDatam, flipQuadEdges, useDiamondSubdivision);
2015-05-08 15:49:33 +08:00
shape->autorelease();
2021-12-25 10:04:45 +08:00
return shape;
2015-05-08 15:49:33 +08:00
}
2021-12-25 10:04:45 +08:00
Physics3DShape* Physics3DShape::createCompoundShape(const std::vector<std::pair<Physics3DShape*, Mat4>>& shapes)
2015-05-08 15:49:33 +08:00
{
2021-12-08 00:11:53 +08:00
auto shape = new Physics3DShape();
2015-05-08 15:49:33 +08:00
shape->initCompoundShape(shapes);
shape->autorelease();
return shape;
}
bool Physics3DShape::initBox(const cocos2d::Vec3& ext)
{
_shapeType = ShapeType::BOX;
2021-12-25 10:04:45 +08:00
_btShape = new btBoxShape(convertVec3TobtVector3(ext * 0.5f));
2015-05-08 15:49:33 +08:00
return true;
}
bool Physics3DShape::initSphere(float radius)
{
_shapeType = ShapeType::SPHERE;
2021-12-25 10:04:45 +08:00
_btShape = new btSphereShape(radius);
2015-05-08 15:49:33 +08:00
return true;
}
bool Physics3DShape::initCylinder(float radius, float height)
{
_shapeType = ShapeType::CYLINDER;
2021-12-25 10:04:45 +08:00
_btShape = new btCylinderShape(convertVec3TobtVector3(cocos2d::Vec3(radius, height, radius) * 0.5f));
2015-05-08 15:49:33 +08:00
return true;
}
bool Physics3DShape::initCapsule(float radius, float height)
{
_shapeType = ShapeType::CAPSULE;
2021-12-25 10:04:45 +08:00
_btShape = new btCapsuleShape(radius, height);
2015-05-08 15:49:33 +08:00
return true;
}
2021-12-25 10:04:45 +08:00
bool Physics3DShape::initConvexHull(const cocos2d::Vec3* points, int numPoints)
2015-05-08 15:49:33 +08:00
{
_shapeType = ShapeType::CONVEX;
2021-12-25 10:04:45 +08:00
_btShape = new btConvexHullShape((btScalar*)points, numPoints, sizeof(cocos2d::Vec3));
2015-05-08 15:49:33 +08:00
return true;
}
2021-12-25 10:04:45 +08:00
bool Physics3DShape::initMesh(const cocos2d::Vec3* triangles, int numTriangles)
2015-05-08 15:49:33 +08:00
{
_shapeType = ShapeType::MESH;
2021-12-25 10:04:45 +08:00
auto mesh = new btTriangleMesh(false);
for (int i = 0; i < numTriangles * 3; i += 3)
{
mesh->addTriangle(convertVec3TobtVector3(triangles[i]), convertVec3TobtVector3(triangles[i + 1]),
convertVec3TobtVector3(triangles[i + 2]));
2015-05-08 15:49:33 +08:00
}
_btShape = new btBvhTriangleMeshShape(mesh, true);
return true;
}
2021-12-25 10:04:45 +08:00
bool Physics3DShape::initHeightfield(int heightStickWidth,
int heightStickLength,
const void* heightfieldData,
float heightScale,
float minHeight,
float maxHeight,
bool useFloatDatam,
bool flipQuadEdges,
bool useDiamondSubdivision)
2015-05-08 15:49:33 +08:00
{
2021-12-25 10:04:45 +08:00
_shapeType = ShapeType::HEIGHT_FIELD;
PHY_ScalarType type = PHY_UCHAR;
2015-05-08 15:49:33 +08:00
unsigned int dataSizeInByte = heightStickWidth * heightStickLength;
2021-12-25 10:04:45 +08:00
if (useFloatDatam)
{
2015-05-08 15:49:33 +08:00
type = PHY_FLOAT;
dataSizeInByte *= sizeof(float);
}
_heightfieldData = new unsigned char[dataSizeInByte];
memcpy(_heightfieldData, heightfieldData, dataSizeInByte);
2021-12-25 10:04:45 +08:00
auto heightfield = new btHeightfieldTerrainShape(heightStickWidth, heightStickLength, _heightfieldData, heightScale,
minHeight, maxHeight, 1, type, flipQuadEdges);
2015-05-08 15:49:33 +08:00
heightfield->setUseDiamondSubdivision(useDiamondSubdivision);
_btShape = heightfield;
return true;
}
2021-12-25 10:04:45 +08:00
bool Physics3DShape::initCompoundShape(const std::vector<std::pair<Physics3DShape*, Mat4>>& shapes)
2015-05-08 15:49:33 +08:00
{
2021-12-25 10:04:45 +08:00
_shapeType = ShapeType::COMPOUND;
2015-05-08 15:49:33 +08:00
auto compound = new btCompoundShape;
2021-12-25 10:04:45 +08:00
for (auto iter : shapes)
{
2015-05-08 15:49:33 +08:00
compound->addChildShape(convertMat4TobtTransform(iter.second), iter.first->getbtShape());
CC_SAFE_RETAIN(iter.first);
_compoundChildShapes.push_back(iter.first);
}
_btShape = compound;
return true;
}
NS_CC_END
2021-12-25 10:04:45 +08:00
# endif // CC_ENABLE_BULLET_INTEGRATION
2015-05-08 15:49:33 +08:00
2021-12-25 10:04:45 +08:00
#endif // CC_USE_3D_PHYSICS