no message
This commit is contained in:
65
cocos/physics/physx/shapes/PhysXBox.cpp
Normal file
65
cocos/physics/physx/shapes/PhysXBox.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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 "physics/physx/shapes/PhysXBox.h"
|
||||
#include "physics/physx/PhysXUtils.h"
|
||||
#include "physics/physx/PhysXWorld.h"
|
||||
#include "physics/physx/shapes/PhysXShape.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
PhysXBox::PhysXBox() : _mHalfExtents(0.5){};
|
||||
|
||||
void PhysXBox::setSize(float x, float y, float z) {
|
||||
_mHalfExtents = physx::PxVec3{x / 2, y / 2, z / 2};
|
||||
updateGeometry();
|
||||
getShape().setGeometry(getPxGeometry<physx::PxBoxGeometry>());
|
||||
}
|
||||
|
||||
void PhysXBox::onComponentSet() {
|
||||
updateGeometry();
|
||||
_mShape = PxGetPhysics().createShape(getPxGeometry<physx::PxBoxGeometry>(), getDefaultMaterial(), true);
|
||||
}
|
||||
|
||||
void PhysXBox::updateScale() {
|
||||
updateGeometry();
|
||||
getShape().setGeometry(getPxGeometry<physx::PxBoxGeometry>());
|
||||
updateCenter();
|
||||
}
|
||||
|
||||
void PhysXBox::updateGeometry() {
|
||||
auto *node = getSharedBody().getNode();
|
||||
auto &geo = getPxGeometry<physx::PxBoxGeometry>();
|
||||
geo.halfExtents = _mHalfExtents;
|
||||
node->updateWorldTransform();
|
||||
geo.halfExtents *= node->getWorldScale();
|
||||
geo.halfExtents = geo.halfExtents.abs();
|
||||
if (geo.halfExtents.minElement() <= 0.0) {
|
||||
geo.halfExtents = geo.halfExtents.maximum(physx::PxVec3{PX_NORMALIZATION_EPSILON});
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
46
cocos/physics/physx/shapes/PhysXBox.h
Normal file
46
cocos/physics/physx/shapes/PhysXBox.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "physics/physx/shapes/PhysXShape.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
class PhysXBox final : public PhysXShape, public IBoxShape {
|
||||
public:
|
||||
PhysXBox();
|
||||
~PhysXBox() override = default;
|
||||
void setSize(float x, float y, float z) override;
|
||||
void updateScale() override;
|
||||
|
||||
private:
|
||||
physx::PxVec3 _mHalfExtents;
|
||||
void updateGeometry();
|
||||
void onComponentSet() override;
|
||||
};
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
95
cocos/physics/physx/shapes/PhysXCapsule.cpp
Normal file
95
cocos/physics/physx/shapes/PhysXCapsule.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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 "physics/physx/shapes/PhysXCapsule.h"
|
||||
#include "physics/physx/PhysXUtils.h"
|
||||
#include "physics/physx/PhysXWorld.h"
|
||||
#include "physics/physx/shapes/PhysXShape.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
PhysXCapsule::PhysXCapsule() : _mRadius(0.5F),
|
||||
_mCylinderHeight(1.0F),
|
||||
_mDirection(EAxisDirection::Y_AXIS){};
|
||||
|
||||
void PhysXCapsule::setRadius(float r) {
|
||||
_mRadius = r;
|
||||
updateGeometry();
|
||||
getShape().setGeometry(getPxGeometry<physx::PxCapsuleGeometry>());
|
||||
}
|
||||
|
||||
void PhysXCapsule::setCylinderHeight(float v) {
|
||||
_mCylinderHeight = v;
|
||||
updateGeometry();
|
||||
getShape().setGeometry(getPxGeometry<physx::PxCapsuleGeometry>());
|
||||
}
|
||||
|
||||
void PhysXCapsule::setDirection(EAxisDirection v) {
|
||||
_mDirection = v;
|
||||
updateGeometry();
|
||||
getShape().setGeometry(getPxGeometry<physx::PxCapsuleGeometry>());
|
||||
}
|
||||
|
||||
void PhysXCapsule::onComponentSet() {
|
||||
updateGeometry();
|
||||
_mShape = PxGetPhysics().createShape(getPxGeometry<physx::PxCapsuleGeometry>(), getDefaultMaterial(), true);
|
||||
}
|
||||
|
||||
void PhysXCapsule::updateScale() {
|
||||
updateGeometry();
|
||||
getShape().setGeometry(getPxGeometry<physx::PxCapsuleGeometry>());
|
||||
updateCenter();
|
||||
}
|
||||
|
||||
void PhysXCapsule::updateGeometry() {
|
||||
auto *node = getSharedBody().getNode();
|
||||
auto &geo = getPxGeometry<physx::PxCapsuleGeometry>();
|
||||
float rs = 1.F;
|
||||
float hs = 1.F;
|
||||
node->updateWorldTransform();
|
||||
switch (_mDirection) {
|
||||
case EAxisDirection::X_AXIS:
|
||||
hs = physx::PxAbs(node->getWorldScale().x);
|
||||
rs = pxAbsMax(node->getWorldScale().y, node->getWorldScale().z);
|
||||
_mRotation = physx::PxQuat{physx::PxIdentity};
|
||||
break;
|
||||
case EAxisDirection::Z_AXIS:
|
||||
hs = physx::PxAbs(node->getWorldScale().z);
|
||||
rs = pxAbsMax(node->getWorldScale().x, node->getWorldScale().y);
|
||||
_mRotation = physx::PxQuat(physx::PxPiDivTwo, physx::PxVec3{0.F, 1.F, 0.F});
|
||||
break;
|
||||
case EAxisDirection::Y_AXIS:
|
||||
default:
|
||||
hs = physx::PxAbs(node->getWorldScale().y);
|
||||
rs = pxAbsMax(node->getWorldScale().x, node->getWorldScale().z);
|
||||
_mRotation = physx::PxQuat(physx::PxPiDivTwo, physx::PxVec3{0.F, 0.F, 1.F});
|
||||
break;
|
||||
}
|
||||
geo.radius = physx::PxMax(physx::PxAbs(_mRadius * rs), PX_NORMALIZATION_EPSILON);
|
||||
geo.halfHeight = physx::PxMax(physx::PxAbs(_mCylinderHeight / 2 * hs), PX_NORMALIZATION_EPSILON);
|
||||
}
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
50
cocos/physics/physx/shapes/PhysXCapsule.h
Normal file
50
cocos/physics/physx/shapes/PhysXCapsule.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "physics/physx/shapes/PhysXShape.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
class PhysXCapsule final : public PhysXShape, public ICapsuleShape {
|
||||
public:
|
||||
PhysXCapsule();
|
||||
~PhysXCapsule() override = default;
|
||||
void setRadius(float r) override;
|
||||
void setCylinderHeight(float v) override;
|
||||
void setDirection(EAxisDirection v) override;
|
||||
void updateScale() override;
|
||||
|
||||
private:
|
||||
float _mRadius;
|
||||
float _mCylinderHeight;
|
||||
EAxisDirection _mDirection;
|
||||
void updateGeometry();
|
||||
void onComponentSet() override;
|
||||
};
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
102
cocos/physics/physx/shapes/PhysXCone.cpp
Normal file
102
cocos/physics/physx/shapes/PhysXCone.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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 "physics/physx/shapes/PhysXCone.h"
|
||||
#include <algorithm>
|
||||
#include "math/Quaternion.h"
|
||||
#include "physics/physx/PhysXUtils.h"
|
||||
#include "physics/physx/PhysXWorld.h"
|
||||
#include "physics/physx/shapes/PhysXShape.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
PhysXCone::PhysXCone() : _mMesh(nullptr){};
|
||||
|
||||
void PhysXCone::setConvex(uint32_t objectID) {
|
||||
uintptr_t handle = PhysXWorld::getInstance().getPXPtrWithPXObjectID(objectID);
|
||||
if (handle == 0) return;
|
||||
if (reinterpret_cast<uintptr_t>(_mMesh) == handle) return;
|
||||
_mMesh = reinterpret_cast<physx::PxConvexMesh *>(handle);
|
||||
if (_mShape) {
|
||||
}
|
||||
}
|
||||
|
||||
void PhysXCone::onComponentSet() {
|
||||
if (_mMesh) {
|
||||
physx::PxConvexMeshGeometry geom;
|
||||
geom.convexMesh = _mMesh;
|
||||
// geom.meshFlags = PxConvexMeshGeometryFlags::eTIGHT_BOUNDS;
|
||||
_mShape = PxGetPhysics().createShape(geom, getDefaultMaterial(), true);
|
||||
updateGeometry();
|
||||
}
|
||||
}
|
||||
|
||||
void PhysXCone::setCone(float r, float h, EAxisDirection d) {
|
||||
_mData.radius = r;
|
||||
_mData.height = h;
|
||||
_mData.direction = d;
|
||||
updateGeometry();
|
||||
}
|
||||
|
||||
void PhysXCone::updateGeometry() {
|
||||
if (!_mShape) return;
|
||||
static physx::PxMeshScale scale;
|
||||
auto *node = getSharedBody().getNode();
|
||||
node->updateWorldTransform();
|
||||
pxSetVec3Ext(scale.scale, node->getWorldScale());
|
||||
scale.scale.y *= std::max(0.0001F, _mData.height / 2);
|
||||
const auto radius = std::max(0.0001F, _mData.radius * 2);
|
||||
const auto xzMaxNorm = std::max(scale.scale.x, scale.scale.z);
|
||||
scale.scale.x = scale.scale.z = radius * xzMaxNorm;
|
||||
Quaternion quat;
|
||||
switch (_mData.direction) {
|
||||
case EAxisDirection::X_AXIS:
|
||||
quat.set(Vec3::UNIT_Z, physx::PxPiDivTwo);
|
||||
pxSetQuatExt(scale.rotation, quat);
|
||||
break;
|
||||
case EAxisDirection::Y_AXIS:
|
||||
default:
|
||||
scale.rotation = physx::PxQuat{physx::PxIdentity};
|
||||
break;
|
||||
case EAxisDirection::Z_AXIS:
|
||||
quat.set(Vec3::UNIT_X, physx::PxPiDivTwo);
|
||||
pxSetQuatExt(scale.rotation, quat);
|
||||
break;
|
||||
}
|
||||
physx::PxConvexMeshGeometry geom;
|
||||
if (getShape().getConvexMeshGeometry(geom)) {
|
||||
geom.scale = scale;
|
||||
getShape().setGeometry(geom);
|
||||
}
|
||||
pxSetQuatExt(_mRotation, quat);
|
||||
}
|
||||
|
||||
void PhysXCone::updateScale() {
|
||||
updateGeometry();
|
||||
updateCenter();
|
||||
}
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
54
cocos/physics/physx/shapes/PhysXCone.h
Normal file
54
cocos/physics/physx/shapes/PhysXCone.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "physics/physx/shapes/PhysXShape.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
class PhysXCone final : public PhysXShape, public IConeShape {
|
||||
public:
|
||||
PhysXCone();
|
||||
~PhysXCone() override = default;
|
||||
void setConvex(uint32_t objectID) override;
|
||||
void setCone(float r, float h, EAxisDirection d) override;
|
||||
void updateScale() override;
|
||||
|
||||
struct Cone {
|
||||
float radius;
|
||||
float height;
|
||||
EAxisDirection direction;
|
||||
};
|
||||
|
||||
private:
|
||||
physx::PxConvexMesh *_mMesh;
|
||||
Cone _mData;
|
||||
void updateGeometry();
|
||||
void onComponentSet() override;
|
||||
};
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
103
cocos/physics/physx/shapes/PhysXCylinder.cpp
Normal file
103
cocos/physics/physx/shapes/PhysXCylinder.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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 "physics/physx/shapes/PhysXCylinder.h"
|
||||
#include <algorithm>
|
||||
#include "math/Quaternion.h"
|
||||
#include "physics/physx/PhysXUtils.h"
|
||||
#include "physics/physx/PhysXWorld.h"
|
||||
#include "physics/physx/shapes/PhysXShape.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
PhysXCylinder::PhysXCylinder() : _mMesh(nullptr){};
|
||||
|
||||
void PhysXCylinder::setConvex(uint32_t objectID) {
|
||||
uintptr_t handle = PhysXWorld::getInstance().getPXPtrWithPXObjectID(objectID);
|
||||
if (handle == 0) return;
|
||||
if (reinterpret_cast<uintptr_t>(_mMesh) == handle) return;
|
||||
_mMesh = reinterpret_cast<physx::PxConvexMesh *>(handle);
|
||||
if (_mShape) {
|
||||
// TODO(Administrator): ...
|
||||
}
|
||||
}
|
||||
|
||||
void PhysXCylinder::onComponentSet() {
|
||||
if (_mMesh) {
|
||||
physx::PxConvexMeshGeometry geom;
|
||||
geom.convexMesh = _mMesh;
|
||||
// geom.meshFlags = PxConvexMeshGeometryFlags::eTIGHT_BOUNDS;
|
||||
_mShape = PxGetPhysics().createShape(geom, getDefaultMaterial(), true);
|
||||
updateGeometry();
|
||||
}
|
||||
}
|
||||
|
||||
void PhysXCylinder::setCylinder(float r, float h, EAxisDirection d) {
|
||||
_mData.radius = r;
|
||||
_mData.height = h;
|
||||
_mData.direction = d;
|
||||
updateGeometry();
|
||||
}
|
||||
|
||||
void PhysXCylinder::updateGeometry() {
|
||||
if (!_mShape) return;
|
||||
static physx::PxMeshScale scale;
|
||||
auto *node = getSharedBody().getNode();
|
||||
node->updateWorldTransform();
|
||||
pxSetVec3Ext(scale.scale, node->getWorldScale());
|
||||
scale.scale.y *= std::max(0.0001F, _mData.height / 2);
|
||||
const auto radius = std::max(0.0001F, _mData.radius * 2);
|
||||
const auto xzMaxNorm = std::max(scale.scale.x, scale.scale.z);
|
||||
scale.scale.x = scale.scale.z = radius * xzMaxNorm;
|
||||
Quaternion quat;
|
||||
switch (_mData.direction) {
|
||||
case EAxisDirection::X_AXIS:
|
||||
quat.set(Vec3::UNIT_Z, physx::PxPiDivTwo);
|
||||
pxSetQuatExt(scale.rotation, quat);
|
||||
break;
|
||||
case EAxisDirection::Y_AXIS:
|
||||
default:
|
||||
scale.rotation = physx::PxQuat{physx::PxIdentity};
|
||||
break;
|
||||
case EAxisDirection::Z_AXIS:
|
||||
quat.set(Vec3::UNIT_X, physx::PxPiDivTwo);
|
||||
pxSetQuatExt(scale.rotation, quat);
|
||||
break;
|
||||
}
|
||||
physx::PxConvexMeshGeometry geom;
|
||||
if (getShape().getConvexMeshGeometry(geom)) {
|
||||
geom.scale = scale;
|
||||
getShape().setGeometry(geom);
|
||||
}
|
||||
pxSetQuatExt(_mRotation, quat);
|
||||
}
|
||||
|
||||
void PhysXCylinder::updateScale() {
|
||||
updateGeometry();
|
||||
updateCenter();
|
||||
}
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
54
cocos/physics/physx/shapes/PhysXCylinder.h
Normal file
54
cocos/physics/physx/shapes/PhysXCylinder.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "physics/physx/shapes/PhysXShape.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
class PhysXCylinder final : public PhysXShape, public ICylinderShape {
|
||||
public:
|
||||
PhysXCylinder();
|
||||
~PhysXCylinder() override = default;
|
||||
void setConvex(uint32_t objectID) override;
|
||||
void setCylinder(float r, float h, EAxisDirection d) override;
|
||||
void updateScale() override;
|
||||
|
||||
struct Cylinder {
|
||||
float radius;
|
||||
float height;
|
||||
EAxisDirection direction;
|
||||
};
|
||||
|
||||
private:
|
||||
physx::PxConvexMesh *_mMesh;
|
||||
Cylinder _mData;
|
||||
void updateGeometry();
|
||||
void onComponentSet() override;
|
||||
};
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
65
cocos/physics/physx/shapes/PhysXPlane.cpp
Normal file
65
cocos/physics/physx/shapes/PhysXPlane.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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 "physics/physx/shapes/PhysXPlane.h"
|
||||
#include "physics/physx/PhysXUtils.h"
|
||||
#include "physics/physx/PhysXWorld.h"
|
||||
#include "physics/physx/shapes/PhysXShape.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
PhysXPlane::PhysXPlane() : _mConstant(0.F),
|
||||
_mNormal(0.F, 1.F, 0.F){};
|
||||
|
||||
void PhysXPlane::setConstant(float x) {
|
||||
_mConstant = x;
|
||||
updateCenter();
|
||||
}
|
||||
|
||||
void PhysXPlane::setNormal(float x, float y, float z) {
|
||||
_mNormal = physx::PxVec3{x, y, z};
|
||||
updateCenter();
|
||||
}
|
||||
|
||||
void PhysXPlane::onComponentSet() {
|
||||
_mShape = PxGetPhysics().createShape(getPxGeometry<physx::PxPlaneGeometry>(), getDefaultMaterial(), true);
|
||||
updateCenter();
|
||||
}
|
||||
|
||||
void PhysXPlane::updateScale() {
|
||||
updateCenter();
|
||||
}
|
||||
|
||||
void PhysXPlane::updateCenter() {
|
||||
auto* node = getSharedBody().getNode();
|
||||
auto& geo = getPxGeometry<physx::PxPlaneGeometry>();
|
||||
physx::PxTransform local;
|
||||
pxSetFromTwoVectors(local.q, physx::PxVec3{1.F, 0.F, 0.F}, _mNormal);
|
||||
local.p = _mNormal * _mConstant + _mCenter;
|
||||
getShape().setLocalPose(local);
|
||||
}
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
48
cocos/physics/physx/shapes/PhysXPlane.h
Normal file
48
cocos/physics/physx/shapes/PhysXPlane.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "physics/physx/shapes/PhysXShape.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
class PhysXPlane final : public PhysXShape, public IPlaneShape {
|
||||
public:
|
||||
PhysXPlane();
|
||||
~PhysXPlane() override = default;
|
||||
void setConstant(float x) override;
|
||||
void setNormal(float x, float y, float z) override;
|
||||
void updateScale() override;
|
||||
|
||||
private:
|
||||
physx::PxVec3 _mNormal;
|
||||
physx::PxReal _mConstant;
|
||||
void updateCenter() override;
|
||||
void onComponentSet() override;
|
||||
};
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
157
cocos/physics/physx/shapes/PhysXShape.cpp
Normal file
157
cocos/physics/physx/shapes/PhysXShape.cpp
Normal file
@@ -0,0 +1,157 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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 "physics/physx/shapes/PhysXShape.h"
|
||||
#include "base/std/container/unordered_map.h"
|
||||
#include "physics/physx/PhysXSharedBody.h"
|
||||
#include "physics/physx/PhysXUtils.h"
|
||||
#include "physics/physx/PhysXWorld.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
PhysXShape::PhysXShape() : _mCenter(physx::PxIdentity), _mRotation(physx::PxIdentity) {
|
||||
_mObjectID = PhysXWorld::getInstance().addWrapperObject(reinterpret_cast<uintptr_t>(this));
|
||||
};
|
||||
|
||||
void PhysXShape::initialize(Node *node) {
|
||||
PhysXWorld &ins = PhysXWorld::getInstance();
|
||||
_mSharedBody = ins.getSharedBody(node);
|
||||
getSharedBody().reference(true);
|
||||
onComponentSet();
|
||||
insertToShapeMap();
|
||||
}
|
||||
|
||||
void PhysXShape::onEnable() {
|
||||
_mEnabled = true;
|
||||
if (_mShape) getSharedBody().addShape(*this);
|
||||
getSharedBody().enabled(true);
|
||||
}
|
||||
|
||||
void PhysXShape::onDisable() {
|
||||
_mEnabled = false;
|
||||
if (_mShape) getSharedBody().removeShape(*this);
|
||||
getSharedBody().enabled(false);
|
||||
}
|
||||
|
||||
void PhysXShape::onDestroy() {
|
||||
getSharedBody().reference(false);
|
||||
eraseFromShapeMap();
|
||||
PhysXWorld::getInstance().removeWrapperObject(_mObjectID);
|
||||
}
|
||||
|
||||
void PhysXShape::setMaterial(uint16_t id, float f, float df, float r,
|
||||
uint8_t m0, uint8_t m1) {
|
||||
if (!_mShape) return;
|
||||
PhysXWorld::getInstance().createMaterial(id, f, df, r, m0, m1);
|
||||
auto *mat = reinterpret_cast<physx::PxMaterial *>(PhysXWorld::getInstance().getPXMaterialPtrWithMaterialID(id));
|
||||
getShape().setMaterials(&mat, 1);
|
||||
}
|
||||
|
||||
void PhysXShape::setAsTrigger(bool v) {
|
||||
if (v) {
|
||||
getShape().setFlag(physx::PxShapeFlag::eSIMULATION_SHAPE, !v);
|
||||
getShape().setFlag(physx::PxShapeFlag::eTRIGGER_SHAPE, v);
|
||||
} else {
|
||||
getShape().setFlag(physx::PxShapeFlag::eTRIGGER_SHAPE, v);
|
||||
getShape().setFlag(physx::PxShapeFlag::eSIMULATION_SHAPE, !v);
|
||||
}
|
||||
if (_mEnabled) {
|
||||
getSharedBody().removeShape(*this);
|
||||
getSharedBody().addShape(*this);
|
||||
}
|
||||
}
|
||||
|
||||
void PhysXShape::setCenter(float x, float y, float z) {
|
||||
_mCenter = physx::PxVec3{x, y, z};
|
||||
updateCenter();
|
||||
}
|
||||
|
||||
uint32_t PhysXShape::getGroup() {
|
||||
return getSharedBody().getGroup();
|
||||
}
|
||||
|
||||
void PhysXShape::setGroup(uint32_t g) {
|
||||
getSharedBody().setGroup(g);
|
||||
}
|
||||
|
||||
uint32_t PhysXShape::getMask() {
|
||||
return getSharedBody().getMask();
|
||||
}
|
||||
|
||||
void PhysXShape::setMask(uint32_t m) {
|
||||
getSharedBody().setMask(m);
|
||||
}
|
||||
|
||||
void PhysXShape::updateEventListener(EShapeFilterFlag flag) {
|
||||
}
|
||||
|
||||
geometry::AABB &PhysXShape::getAABB() {
|
||||
static IntrusivePtr<geometry::AABB> aabb; // this variable is shared with JS with refcounter
|
||||
if (!aabb) {
|
||||
aabb = new geometry::AABB;
|
||||
}
|
||||
if (_mShape) {
|
||||
auto bounds = physx::PxShapeExt::getWorldBounds(getShape(), *getSharedBody().getImpl().rigidActor);
|
||||
pxSetVec3Ext(aabb->center, (bounds.maximum + bounds.minimum) / 2);
|
||||
pxSetVec3Ext(aabb->halfExtents, (bounds.maximum - bounds.minimum) / 2);
|
||||
}
|
||||
return *aabb;
|
||||
}
|
||||
|
||||
geometry::Sphere &PhysXShape::getBoundingSphere() {
|
||||
static IntrusivePtr<geometry::Sphere> sphere; // this variable is shared with JS with refcounter
|
||||
if (!sphere) {
|
||||
sphere = new geometry::Sphere;
|
||||
}
|
||||
if (_mShape) sphere->define(getAABB());
|
||||
return *sphere;
|
||||
}
|
||||
|
||||
void PhysXShape::updateFilterData(const physx::PxFilterData &data) {
|
||||
}
|
||||
|
||||
void PhysXShape::updateCenter() {
|
||||
if (!_mShape) return;
|
||||
auto &sb = getSharedBody();
|
||||
auto *node = sb.getNode();
|
||||
node->updateWorldTransform();
|
||||
physx::PxTransform local{_mCenter * node->getWorldScale(), _mRotation};
|
||||
getShape().setLocalPose(local);
|
||||
}
|
||||
|
||||
void PhysXShape::insertToShapeMap() {
|
||||
if (_mShape) {
|
||||
getPxShapeMap().insert(std::pair<uintptr_t, uint32_t>(reinterpret_cast<uintptr_t>(&getShape()), getObjectID()));
|
||||
}
|
||||
}
|
||||
|
||||
void PhysXShape::eraseFromShapeMap() {
|
||||
if (_mShape) {
|
||||
getPxShapeMap().erase(reinterpret_cast<uintptr_t>(&getShape()));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
89
cocos/physics/physx/shapes/PhysXShape.h
Normal file
89
cocos/physics/physx/shapes/PhysXShape.h
Normal file
@@ -0,0 +1,89 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "base/Macros.h"
|
||||
#include "core/scene-graph/Node.h"
|
||||
#include "physics/physx/PhysXInc.h"
|
||||
#include "physics/spec/IShape.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
class PhysXSharedBody;
|
||||
|
||||
template <typename T>
|
||||
inline T &getPxGeometry() {
|
||||
static T geo;
|
||||
return geo;
|
||||
}
|
||||
|
||||
class PhysXShape : virtual public IBaseShape {
|
||||
PX_NOCOPY(PhysXShape)
|
||||
PhysXShape();
|
||||
|
||||
public:
|
||||
~PhysXShape() override = default;
|
||||
void initialize(Node *node) override;
|
||||
void onEnable() override;
|
||||
void onDisable() override;
|
||||
void onDestroy() override;
|
||||
void setMaterial(uint16_t id, float f, float df, float r,
|
||||
uint8_t m0, uint8_t m1) override;
|
||||
void setAsTrigger(bool v) override;
|
||||
void setCenter(float x, float y, float z) override;
|
||||
geometry::AABB &getAABB() override;
|
||||
geometry::Sphere &getBoundingSphere() override;
|
||||
void updateEventListener(EShapeFilterFlag flag) override;
|
||||
uint32_t getGroup() override;
|
||||
void setGroup(uint32_t g) override;
|
||||
uint32_t getMask() override;
|
||||
void setMask(uint32_t m) override;
|
||||
virtual void updateScale() = 0;
|
||||
inline physx::PxVec3 &getCenter() { return _mCenter; }
|
||||
inline physx::PxShape &getShape() const { return *_mShape; }
|
||||
inline PhysXSharedBody &getSharedBody() const { return *_mSharedBody; }
|
||||
inline bool isTrigger() const {
|
||||
return getShape().getFlags().isSet(physx::PxShapeFlag::eTRIGGER_SHAPE);
|
||||
}
|
||||
void updateFilterData(const physx::PxFilterData &data);
|
||||
uint32_t getObjectID() const override { return _mObjectID; };
|
||||
|
||||
protected:
|
||||
PhysXSharedBody *_mSharedBody{nullptr};
|
||||
physx::PxShape *_mShape{nullptr};
|
||||
physx::PxVec3 _mCenter;
|
||||
physx::PxQuat _mRotation;
|
||||
uint8_t _mFlag{0};
|
||||
bool _mEnabled{false};
|
||||
uint32_t _mObjectID{0};
|
||||
|
||||
virtual void updateCenter();
|
||||
virtual void onComponentSet() = 0;
|
||||
virtual void insertToShapeMap();
|
||||
virtual void eraseFromShapeMap();
|
||||
};
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
62
cocos/physics/physx/shapes/PhysXSphere.cpp
Normal file
62
cocos/physics/physx/shapes/PhysXSphere.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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 "physics/physx/shapes/PhysXSphere.h"
|
||||
#include "physics/physx/PhysXUtils.h"
|
||||
#include "physics/physx/PhysXWorld.h"
|
||||
#include "physics/physx/shapes/PhysXShape.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
PhysXSphere::PhysXSphere() : _mRadius(0.5F) {}
|
||||
|
||||
void PhysXSphere::setRadius(float r) {
|
||||
_mRadius = r;
|
||||
updateGeometry();
|
||||
getShape().setGeometry(getPxGeometry<physx::PxSphereGeometry>());
|
||||
}
|
||||
|
||||
void PhysXSphere::onComponentSet() {
|
||||
updateGeometry();
|
||||
_mShape = PxGetPhysics().createShape(getPxGeometry<physx::PxSphereGeometry>(), getDefaultMaterial(), true);
|
||||
}
|
||||
|
||||
void PhysXSphere::updateScale() {
|
||||
updateGeometry();
|
||||
getShape().setGeometry(getPxGeometry<physx::PxSphereGeometry>());
|
||||
updateCenter();
|
||||
}
|
||||
|
||||
void PhysXSphere::updateGeometry() {
|
||||
physx::PxVec3 scale;
|
||||
auto *node = getSharedBody().getNode();
|
||||
node->updateWorldTransform();
|
||||
pxSetVec3Ext(scale, node->getWorldScale());
|
||||
auto &geo = getPxGeometry<physx::PxSphereGeometry>();
|
||||
geo.radius = physx::PxMax(_mRadius * scale.abs().maxElement(), PX_NORMALIZATION_EPSILON);
|
||||
}
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
46
cocos/physics/physx/shapes/PhysXSphere.h
Normal file
46
cocos/physics/physx/shapes/PhysXSphere.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "physics/physx/shapes/PhysXShape.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
class PhysXSphere final : public PhysXShape, public ISphereShape {
|
||||
public:
|
||||
PhysXSphere();
|
||||
~PhysXSphere() override = default;
|
||||
void setRadius(float r) override;
|
||||
void updateScale() override;
|
||||
|
||||
private:
|
||||
float _mRadius;
|
||||
void updateGeometry();
|
||||
void onComponentSet() override;
|
||||
};
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
85
cocos/physics/physx/shapes/PhysXTerrain.cpp
Normal file
85
cocos/physics/physx/shapes/PhysXTerrain.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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 "physics/physx/shapes/PhysXTerrain.h"
|
||||
#include "physics/physx/PhysXUtils.h"
|
||||
#include "physics/physx/PhysXWorld.h"
|
||||
#include "physics/physx/shapes/PhysXShape.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
PhysXTerrain::PhysXTerrain() : _mTerrain(nullptr),
|
||||
_mRowScale(1.F),
|
||||
_mColScale(1.F),
|
||||
_mHeightScale(1.F),
|
||||
_mIsTrigger(false){};
|
||||
|
||||
void PhysXTerrain::setTerrain(uint32_t objectID, float rs, float cs, float hs) {
|
||||
uintptr_t handle = PhysXWorld::getInstance().getPXPtrWithPXObjectID(objectID);
|
||||
if (handle == 0) return;
|
||||
if (_mShape) return;
|
||||
if (reinterpret_cast<uintptr_t>(_mTerrain) == handle) return;
|
||||
_mTerrain = reinterpret_cast<physx::PxHeightField *>(handle);
|
||||
_mRowScale = rs;
|
||||
_mColScale = cs;
|
||||
_mHeightScale = hs;
|
||||
if (_mSharedBody && _mTerrain) {
|
||||
onComponentSet();
|
||||
insertToShapeMap();
|
||||
if (_mEnabled) getSharedBody().addShape(*this);
|
||||
setAsTrigger(_mIsTrigger);
|
||||
updateCenter();
|
||||
}
|
||||
}
|
||||
|
||||
void PhysXTerrain::onComponentSet() {
|
||||
if (_mTerrain) {
|
||||
physx::PxHeightFieldGeometry geom;
|
||||
geom.rowScale = _mRowScale;
|
||||
geom.columnScale = _mColScale;
|
||||
geom.heightScale = _mHeightScale;
|
||||
geom.heightField = _mTerrain;
|
||||
_mShape = PxGetPhysics().createShape(geom, getDefaultMaterial(), true);
|
||||
}
|
||||
}
|
||||
|
||||
void PhysXTerrain::updateScale() {
|
||||
// updateCenter(); needed?
|
||||
}
|
||||
|
||||
void PhysXTerrain::updateCenter() {
|
||||
if (!_mShape) return;
|
||||
getShape().setLocalPose(physx::PxTransform{_mCenter, _mRotation});
|
||||
}
|
||||
|
||||
void PhysXTerrain::setAsTrigger(bool v) {
|
||||
_mIsTrigger = v;
|
||||
if (_mShape) {
|
||||
PhysXShape::setAsTrigger(v);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
51
cocos/physics/physx/shapes/PhysXTerrain.h
Normal file
51
cocos/physics/physx/shapes/PhysXTerrain.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "physics/physx/shapes/PhysXShape.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
class PhysXTerrain final : public PhysXShape, public ITerrainShape {
|
||||
public:
|
||||
PhysXTerrain();
|
||||
~PhysXTerrain() override = default;
|
||||
void setTerrain(uint32_t objectID, float rs, float cs, float hs) override;
|
||||
void updateScale() override;
|
||||
void updateCenter() override;
|
||||
void setAsTrigger(bool v) override;
|
||||
|
||||
private:
|
||||
physx::PxHeightField *_mTerrain;
|
||||
float _mRowScale;
|
||||
float _mColScale;
|
||||
float _mHeightScale;
|
||||
bool _mIsTrigger;
|
||||
void onComponentSet() override;
|
||||
};
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
114
cocos/physics/physx/shapes/PhysXTrimesh.cpp
Normal file
114
cocos/physics/physx/shapes/PhysXTrimesh.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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 "physics/physx/shapes/PhysXTrimesh.h"
|
||||
#include "physics/physx/PhysXUtils.h"
|
||||
#include "physics/physx/PhysXWorld.h"
|
||||
#include "physics/physx/shapes/PhysXShape.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
PhysXTrimesh::PhysXTrimesh() : _mMeshHandle(0),
|
||||
_mConvex(false),
|
||||
_mIsTrigger(false){};
|
||||
|
||||
void PhysXTrimesh::setMesh(uint32_t objectID) {
|
||||
uintptr_t handle = PhysXWorld::getInstance().getPXPtrWithPXObjectID(objectID);
|
||||
if (handle == 0) return;
|
||||
if (_mShape) {
|
||||
getSharedBody().removeShape(*this);
|
||||
eraseFromShapeMap();
|
||||
_mShape->release();
|
||||
_mShape = nullptr;
|
||||
}
|
||||
if (_mMeshHandle == handle) return;
|
||||
_mMeshHandle = handle;
|
||||
if (_mSharedBody && _mMeshHandle) {
|
||||
onComponentSet();
|
||||
insertToShapeMap();
|
||||
if (_mEnabled) getSharedBody().addShape(*this);
|
||||
setAsTrigger(_mIsTrigger);
|
||||
updateCenter();
|
||||
}
|
||||
}
|
||||
|
||||
void PhysXTrimesh::useConvex(bool v) {
|
||||
_mConvex = v;
|
||||
}
|
||||
|
||||
void PhysXTrimesh::onComponentSet() {
|
||||
if (_mMeshHandle) {
|
||||
const auto &mat = getDefaultMaterial();
|
||||
if (_mConvex) {
|
||||
physx::PxConvexMeshGeometry geom;
|
||||
geom.convexMesh = reinterpret_cast<physx::PxConvexMesh *>(_mMeshHandle);
|
||||
// geom.meshFlags = PxConvexMeshGeometryFlags::eTIGHT_BOUNDS;
|
||||
_mShape = PxGetPhysics().createShape(geom, mat, true);
|
||||
} else {
|
||||
physx::PxTriangleMeshGeometry geom;
|
||||
geom.triangleMesh = reinterpret_cast<physx::PxTriangleMesh *>(_mMeshHandle);
|
||||
// geom.meshFlags = PxMeshGeometryFlag::eDOUBLE_SIDED;
|
||||
_mShape = PxGetPhysics().createShape(geom, mat, true);
|
||||
}
|
||||
updateGeometry();
|
||||
}
|
||||
}
|
||||
|
||||
void PhysXTrimesh::updateScale() {
|
||||
updateGeometry();
|
||||
updateCenter();
|
||||
}
|
||||
|
||||
void PhysXTrimesh::updateGeometry() {
|
||||
static physx::PxMeshScale scale;
|
||||
scale.rotation = physx::PxQuat{physx::PxIdentity};
|
||||
auto *node = getSharedBody().getNode();
|
||||
node->updateWorldTransform();
|
||||
pxSetVec3Ext(scale.scale, node->getWorldScale());
|
||||
const auto &type = _mShape->getGeometryType();
|
||||
if (type == physx::PxGeometryType::eCONVEXMESH) {
|
||||
physx::PxConvexMeshGeometry geom;
|
||||
if (getShape().getConvexMeshGeometry(geom)) {
|
||||
geom.scale = scale;
|
||||
getShape().setGeometry(geom);
|
||||
}
|
||||
} else if (type == physx::PxGeometryType::eTRIANGLEMESH) {
|
||||
physx::PxTriangleMeshGeometry geom;
|
||||
if (getShape().getTriangleMeshGeometry(geom)) {
|
||||
geom.scale = scale;
|
||||
getShape().setGeometry(geom);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PhysXTrimesh::setAsTrigger(bool v) {
|
||||
_mIsTrigger = v;
|
||||
if (_mShape) {
|
||||
PhysXShape::setAsTrigger(v);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
50
cocos/physics/physx/shapes/PhysXTrimesh.h
Normal file
50
cocos/physics/physx/shapes/PhysXTrimesh.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "physics/physx/shapes/PhysXShape.h"
|
||||
|
||||
namespace cc {
|
||||
namespace physics {
|
||||
|
||||
class PhysXTrimesh final : public PhysXShape, public ITrimeshShape {
|
||||
public:
|
||||
PhysXTrimesh();
|
||||
~PhysXTrimesh() override = default;
|
||||
void setMesh(uint32_t objectID) override;
|
||||
void useConvex(bool v) override;
|
||||
void updateScale() override;
|
||||
void setAsTrigger(bool v) override;
|
||||
|
||||
private:
|
||||
bool _mConvex;
|
||||
uintptr_t _mMeshHandle;
|
||||
bool _mIsTrigger;
|
||||
void updateGeometry();
|
||||
void onComponentSet() override;
|
||||
};
|
||||
|
||||
} // namespace physics
|
||||
} // namespace cc
|
||||
Reference in New Issue
Block a user