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 "BsPhysXSphericalJoint.h" |
4 | #include "BsFPhysXJoint.h" |
5 | #include "BsPhysXRigidbody.h" |
6 | #include "PxRigidDynamic.h" |
7 | |
8 | using namespace physx; |
9 | |
10 | namespace bs |
11 | { |
12 | PxSphericalJointFlag::Enum toPxFlag(SphericalJointFlag flag) |
13 | { |
14 | switch (flag) |
15 | { |
16 | default: |
17 | case SphericalJointFlag::Limit: |
18 | return PxSphericalJointFlag::eLIMIT_ENABLED; |
19 | } |
20 | } |
21 | |
22 | PhysXSphericalJoint::PhysXSphericalJoint(PxPhysics* physx, const SPHERICAL_JOINT_DESC& desc) |
23 | :SphericalJoint(desc) |
24 | { |
25 | PxRigidActor* actor0 = nullptr; |
26 | if (desc.bodies[0].body != nullptr) |
27 | actor0 = static_cast<PhysXRigidbody*>(desc.bodies[0].body)->_getInternal(); |
28 | |
29 | PxRigidActor* actor1 = nullptr; |
30 | if (desc.bodies[1].body != nullptr) |
31 | actor1 = static_cast<PhysXRigidbody*>(desc.bodies[1].body)->_getInternal(); |
32 | |
33 | PxTransform tfrm0 = toPxTransform(desc.bodies[0].position, desc.bodies[0].rotation); |
34 | PxTransform tfrm1 = toPxTransform(desc.bodies[1].position, desc.bodies[1].rotation); |
35 | |
36 | PxSphericalJoint* joint = PxSphericalJointCreate(*physx, actor0, tfrm0, actor1, tfrm1); |
37 | joint->userData = this; |
38 | |
39 | mInternal = bs_new<FPhysXJoint>(joint, desc); |
40 | |
41 | PxSphericalJointFlags flags; |
42 | |
43 | if (((UINT32)desc.flag & (UINT32)SphericalJointFlag::Limit) != 0) |
44 | flags |= PxSphericalJointFlag::eLIMIT_ENABLED; |
45 | |
46 | joint->setSphericalJointFlags(flags); |
47 | |
48 | // Calls to virtual methods are okay here |
49 | setLimit(desc.limit); |
50 | } |
51 | |
52 | PhysXSphericalJoint::~PhysXSphericalJoint() |
53 | { |
54 | bs_delete(mInternal); |
55 | } |
56 | |
57 | LimitConeRange PhysXSphericalJoint::getLimit() const |
58 | { |
59 | PxJointLimitCone pxLimit = getInternal()->getLimitCone(); |
60 | |
61 | LimitConeRange limit; |
62 | limit.yLimitAngle = pxLimit.yAngle; |
63 | limit.zLimitAngle = pxLimit.zAngle; |
64 | limit.contactDist = pxLimit.contactDistance; |
65 | limit.restitution = pxLimit.restitution; |
66 | limit.spring.stiffness = pxLimit.stiffness; |
67 | limit.spring.damping = pxLimit.damping; |
68 | |
69 | return limit; |
70 | } |
71 | |
72 | void PhysXSphericalJoint::setLimit(const LimitConeRange& limit) |
73 | { |
74 | PxJointLimitCone pxLimit(limit.yLimitAngle.valueRadians(), limit.zLimitAngle.valueRadians(), limit.contactDist); |
75 | pxLimit.stiffness = limit.spring.stiffness; |
76 | pxLimit.damping = limit.spring.damping; |
77 | pxLimit.restitution = limit.restitution; |
78 | |
79 | getInternal()->setLimitCone(pxLimit); |
80 | } |
81 | |
82 | void PhysXSphericalJoint::setFlag(SphericalJointFlag flag, bool enabled) |
83 | { |
84 | getInternal()->setSphericalJointFlag(toPxFlag(flag), enabled); |
85 | } |
86 | |
87 | bool PhysXSphericalJoint::hasFlag(SphericalJointFlag flag) const |
88 | { |
89 | return getInternal()->getSphericalJointFlags() & toPxFlag(flag); |
90 | } |
91 | |
92 | PxSphericalJoint* PhysXSphericalJoint::getInternal() const |
93 | { |
94 | FPhysXJoint* internal = static_cast<FPhysXJoint*>(mInternal); |
95 | |
96 | return static_cast<PxSphericalJoint*>(internal->_getInternal()); |
97 | } |
98 | } |