2 Copyright (c) 2013 Advanced Micro Devices, Inc.
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose,
7 including commercial applications, and to alter it and redistribute it freely,
8 subject to the following restrictions:
10 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
14 //Originally written by Erwin Coumans
16 #ifndef B3_GPU_SOLVER_BODY_H
17 #define B3_GPU_SOLVER_BODY_H
19 #include "Bullet3Common/b3Vector3.h"
20 #include "Bullet3Common/b3Matrix3x3.h"
22 #include "Bullet3Common/b3AlignedAllocator.h"
23 #include "Bullet3Common/b3TransformUtil.h"
25 ///Until we get other contributions, only use SIMD on Windows, when using Visual Studio 2008 or later, and not double precision
30 ///The b3SolverBody is an internal datastructure for the constraint solver. Only necessary data is packed to increase cache coherence/performance.
31 B3_ATTRIBUTE_ALIGNED16(struct)
34 B3_DECLARE_ALIGNED_ALLOCATOR();
35 // b3Transform m_worldTransformUnused;
36 b3Vector3 m_deltaLinearVelocity;
37 b3Vector3 m_deltaAngularVelocity;
38 b3Vector3 m_angularFactor;
39 b3Vector3 m_linearFactor;
41 b3Vector3 m_pushVelocity;
42 b3Vector3 m_turnVelocity;
43 b3Vector3 m_linearVelocity;
44 b3Vector3 m_angularVelocity;
48 int m_originalBodyIndex;
54 void setWorldTransform(const b3Transform& worldTransform)
56 m_worldTransform = worldTransform;
59 const b3Transform& getWorldTransform() const
61 return m_worldTransform;
64 B3_FORCE_INLINE void getVelocityInLocalPointObsolete(const b3Vector3& rel_pos, b3Vector3& velocity) const
67 velocity = m_linearVelocity + m_deltaLinearVelocity + (m_angularVelocity + m_deltaAngularVelocity).cross(rel_pos);
69 velocity.setValue(0, 0, 0);
72 B3_FORCE_INLINE void getAngularVelocity(b3Vector3 & angVel) const
75 angVel = m_angularVelocity + m_deltaAngularVelocity;
77 angVel.setValue(0, 0, 0);
80 //Optimization for the iterative solver: avoid calculating constant terms involving inertia, normal, relative position
81 B3_FORCE_INLINE void applyImpulse(const b3Vector3& linearComponent, const b3Vector3& angularComponent, const b3Scalar impulseMagnitude)
85 m_deltaLinearVelocity += linearComponent * impulseMagnitude * m_linearFactor;
86 m_deltaAngularVelocity += angularComponent * (impulseMagnitude * m_angularFactor);
90 B3_FORCE_INLINE void internalApplyPushImpulse(const b3Vector3& linearComponent, const b3Vector3& angularComponent, b3Scalar impulseMagnitude)
94 m_pushVelocity += linearComponent * impulseMagnitude * m_linearFactor;
95 m_turnVelocity += angularComponent * (impulseMagnitude * m_angularFactor);
99 const b3Vector3& getDeltaLinearVelocity() const
101 return m_deltaLinearVelocity;
104 const b3Vector3& getDeltaAngularVelocity() const
106 return m_deltaAngularVelocity;
109 const b3Vector3& getPushVelocity() const
111 return m_pushVelocity;
114 const b3Vector3& getTurnVelocity() const
116 return m_turnVelocity;
119 ////////////////////////////////////////////////
120 ///some internal methods, don't use them
122 b3Vector3& internalGetDeltaLinearVelocity()
124 return m_deltaLinearVelocity;
127 b3Vector3& internalGetDeltaAngularVelocity()
129 return m_deltaAngularVelocity;
132 const b3Vector3& internalGetAngularFactor() const
134 return m_angularFactor;
137 const b3Vector3& internalGetInvMass() const
142 void internalSetInvMass(const b3Vector3& invMass)
147 b3Vector3& internalGetPushVelocity()
149 return m_pushVelocity;
152 b3Vector3& internalGetTurnVelocity()
154 return m_turnVelocity;
157 B3_FORCE_INLINE void internalGetVelocityInLocalPointObsolete(const b3Vector3& rel_pos, b3Vector3& velocity) const
159 velocity = m_linearVelocity + m_deltaLinearVelocity + (m_angularVelocity + m_deltaAngularVelocity).cross(rel_pos);
162 B3_FORCE_INLINE void internalGetAngularVelocity(b3Vector3 & angVel) const
164 angVel = m_angularVelocity + m_deltaAngularVelocity;
167 //Optimization for the iterative solver: avoid calculating constant terms involving inertia, normal, relative position
168 B3_FORCE_INLINE void internalApplyImpulse(const b3Vector3& linearComponent, const b3Vector3& angularComponent, const b3Scalar impulseMagnitude)
170 //if (m_originalBody)
172 m_deltaLinearVelocity += linearComponent * impulseMagnitude * m_linearFactor;
173 m_deltaAngularVelocity += angularComponent * (impulseMagnitude * m_angularFactor);
177 void writebackVelocity()
179 //if (m_originalBody>=0)
181 m_linearVelocity += m_deltaLinearVelocity;
182 m_angularVelocity += m_deltaAngularVelocity;
184 //m_originalBody->setCompanionId(-1);
188 void writebackVelocityAndTransform(b3Scalar timeStep, b3Scalar splitImpulseTurnErp)
193 m_linearVelocity += m_deltaLinearVelocity;
194 m_angularVelocity += m_deltaAngularVelocity;
196 //correct the position/orientation based on push/turn recovery
197 b3Transform newTransform;
198 if (m_pushVelocity[0] != 0.f || m_pushVelocity[1] != 0 || m_pushVelocity[2] != 0 || m_turnVelocity[0] != 0.f || m_turnVelocity[1] != 0 || m_turnVelocity[2] != 0)
200 // b3Quaternion orn = m_worldTransform.getRotation();
201 // b3TransformUtil::integrateTransform(m_worldTransform,m_pushVelocity,m_turnVelocity*splitImpulseTurnErp,timeStep,newTransform);
202 // m_worldTransform = newTransform;
204 //m_worldTransform.setRotation(orn);
205 //m_originalBody->setCompanionId(-1);
210 #endif //B3_SOLVER_BODY_H