2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "Bullet3Collision/NarrowPhaseCollision/shared/b3RigidBodyData.h"
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
inline void integrateSingleTransform(__global b3RigidBodyData_t* bodies, int nodeID, float timeStep, float angularDamping, b3Float4ConstArg gravityAcceleration)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
if (bodies[nodeID].m_invMass != 0.f)
|
|
|
|
{
|
|
|
|
float BT_GPU_ANGULAR_MOTION_THRESHOLD = (0.25f * 3.14159254f);
|
|
|
|
|
|
|
|
//angular velocity
|
|
|
|
{
|
|
|
|
b3Float4 axis;
|
|
|
|
//add some hardcoded angular damping
|
|
|
|
bodies[nodeID].m_angVel.x *= angularDamping;
|
|
|
|
bodies[nodeID].m_angVel.y *= angularDamping;
|
|
|
|
bodies[nodeID].m_angVel.z *= angularDamping;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
b3Float4 angvel = bodies[nodeID].m_angVel;
|
|
|
|
|
|
|
|
float fAngle = b3Sqrt(b3Dot3F4(angvel, angvel));
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
//limit the angular motion
|
2019-01-03 13:26:51 +00:00
|
|
|
if (fAngle * timeStep > BT_GPU_ANGULAR_MOTION_THRESHOLD)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
fAngle = BT_GPU_ANGULAR_MOTION_THRESHOLD / timeStep;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
if (fAngle < 0.001f)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
// use Taylor's expansions of sync function
|
2019-01-03 13:26:51 +00:00
|
|
|
axis = angvel * (0.5f * timeStep - (timeStep * timeStep * timeStep) * 0.020833333333f * fAngle * fAngle);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// sync(fAngle) = sin(c*fAngle)/t
|
2019-01-03 13:26:51 +00:00
|
|
|
axis = angvel * (b3Sin(0.5f * fAngle * timeStep) / fAngle);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
b3Quat dorn;
|
|
|
|
dorn.x = axis.x;
|
|
|
|
dorn.y = axis.y;
|
|
|
|
dorn.z = axis.z;
|
|
|
|
dorn.w = b3Cos(fAngle * timeStep * 0.5f);
|
|
|
|
b3Quat orn0 = bodies[nodeID].m_quat;
|
|
|
|
b3Quat predictedOrn = b3QuatMul(dorn, orn0);
|
|
|
|
predictedOrn = b3QuatNormalized(predictedOrn);
|
2019-01-03 13:26:51 +00:00
|
|
|
bodies[nodeID].m_quat = predictedOrn;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
//linear velocity
|
|
|
|
bodies[nodeID].m_pos += bodies[nodeID].m_linVel * timeStep;
|
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
//apply gravity
|
|
|
|
bodies[nodeID].m_linVel += gravityAcceleration * timeStep;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
inline void b3IntegrateTransform(__global b3RigidBodyData_t* body, float timeStep, float angularDamping, b3Float4ConstArg gravityAcceleration)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
float BT_GPU_ANGULAR_MOTION_THRESHOLD = (0.25f * 3.14159254f);
|
2019-01-03 13:26:51 +00:00
|
|
|
|
|
|
|
if ((body->m_invMass != 0.f))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//angular velocity
|
|
|
|
{
|
|
|
|
b3Float4 axis;
|
|
|
|
//add some hardcoded angular damping
|
|
|
|
body->m_angVel.x *= angularDamping;
|
|
|
|
body->m_angVel.y *= angularDamping;
|
|
|
|
body->m_angVel.z *= angularDamping;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
b3Float4 angvel = body->m_angVel;
|
|
|
|
float fAngle = b3Sqrt(b3Dot3F4(angvel, angvel));
|
|
|
|
//limit the angular motion
|
2019-01-03 13:26:51 +00:00
|
|
|
if (fAngle * timeStep > BT_GPU_ANGULAR_MOTION_THRESHOLD)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
fAngle = BT_GPU_ANGULAR_MOTION_THRESHOLD / timeStep;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
if (fAngle < 0.001f)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
// use Taylor's expansions of sync function
|
2019-01-03 13:26:51 +00:00
|
|
|
axis = angvel * (0.5f * timeStep - (timeStep * timeStep * timeStep) * 0.020833333333f * fAngle * fAngle);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// sync(fAngle) = sin(c*fAngle)/t
|
2019-01-03 13:26:51 +00:00
|
|
|
axis = angvel * (b3Sin(0.5f * fAngle * timeStep) / fAngle);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
b3Quat dorn;
|
|
|
|
dorn.x = axis.x;
|
|
|
|
dorn.y = axis.y;
|
|
|
|
dorn.z = axis.z;
|
|
|
|
dorn.w = b3Cos(fAngle * timeStep * 0.5f);
|
|
|
|
b3Quat orn0 = body->m_quat;
|
|
|
|
|
|
|
|
b3Quat predictedOrn = b3QuatMul(dorn, orn0);
|
|
|
|
predictedOrn = b3QuatNormalized(predictedOrn);
|
2019-01-03 13:26:51 +00:00
|
|
|
body->m_quat = predictedOrn;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//apply gravity
|
|
|
|
body->m_linVel += gravityAcceleration * timeStep;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
//linear velocity
|
|
|
|
body->m_pos += body->m_linVel * timeStep;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|