1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
3 | #include "BsPhysXCapsuleCollider.h" |
4 | #include "BsPhysX.h" |
5 | #include "PxPhysics.h" |
6 | #include "BsFPhysXCollider.h" |
7 | |
8 | using namespace physx; |
9 | |
10 | namespace bs |
11 | { |
12 | PhysXCapsuleCollider::PhysXCapsuleCollider(PxPhysics* physx, PxScene* scene, const Vector3& position, |
13 | const Quaternion& rotation, float radius, float halfHeight) |
14 | :mRadius(radius), mHalfHeight(halfHeight) |
15 | { |
16 | PxCapsuleGeometry geometry(radius, halfHeight); |
17 | |
18 | PxShape* shape = physx->createShape(geometry, *gPhysX().getDefaultMaterial(), true); |
19 | shape->setLocalPose(toPxTransform(position, rotation)); |
20 | shape->userData = this; |
21 | |
22 | mInternal = bs_new<FPhysXCollider>(scene, shape); |
23 | applyGeometry(); |
24 | } |
25 | |
26 | PhysXCapsuleCollider::~PhysXCapsuleCollider() |
27 | { |
28 | bs_delete(mInternal); |
29 | } |
30 | |
31 | void PhysXCapsuleCollider::setScale(const Vector3& scale) |
32 | { |
33 | CapsuleCollider::setScale(scale); |
34 | applyGeometry(); |
35 | } |
36 | |
37 | void PhysXCapsuleCollider::setHalfHeight(float halfHeight) |
38 | { |
39 | mHalfHeight = halfHeight; |
40 | applyGeometry(); |
41 | } |
42 | |
43 | float PhysXCapsuleCollider::getHalfHeight() const |
44 | { |
45 | return mHalfHeight; |
46 | } |
47 | |
48 | void PhysXCapsuleCollider::setRadius(float radius) |
49 | { |
50 | mRadius = radius; |
51 | applyGeometry(); |
52 | } |
53 | |
54 | float PhysXCapsuleCollider::getRadius() const |
55 | { |
56 | return mRadius; |
57 | } |
58 | |
59 | void PhysXCapsuleCollider::applyGeometry() |
60 | { |
61 | PxCapsuleGeometry geometry(std::max(0.01f, mRadius * std::max(mScale.x, mScale.z)), |
62 | std::max(0.01f, mHalfHeight * mScale.y)); |
63 | |
64 | getInternal()->_getShape()->setGeometry(geometry); |
65 | } |
66 | |
67 | FPhysXCollider* PhysXCapsuleCollider::getInternal() const |
68 | { |
69 | return static_cast<FPhysXCollider*>(mInternal); |
70 | } |
71 | } |