| 213 | } |
| 214 | |
| 215 | void Physics3DComponent::syncNodeToPhysics() |
| 216 | { |
| 217 | if (_physics3DObj->getObjType() == Physics3DObject::PhysicsObjType::RIGID_BODY || |
| 218 | _physics3DObj->getObjType() == Physics3DObject::PhysicsObjType::COLLIDER) |
| 219 | { |
| 220 | auto mat = _owner->getNodeToWorldTransform(); |
| 221 | // remove scale, no scale support for physics |
| 222 | float oneOverLen = 1.f / sqrtf(mat.m[0] * mat.m[0] + mat.m[1] * mat.m[1] + mat.m[2] * mat.m[2]); |
| 223 | mat.m[0] *= oneOverLen; |
| 224 | mat.m[1] *= oneOverLen; |
| 225 | mat.m[2] *= oneOverLen; |
| 226 | oneOverLen = 1.f / sqrtf(mat.m[4] * mat.m[4] + mat.m[5] * mat.m[5] + mat.m[6] * mat.m[6]); |
| 227 | mat.m[4] *= oneOverLen; |
| 228 | mat.m[5] *= oneOverLen; |
| 229 | mat.m[6] *= oneOverLen; |
| 230 | oneOverLen = 1.f / sqrtf(mat.m[8] * mat.m[8] + mat.m[9] * mat.m[9] + mat.m[10] * mat.m[10]); |
| 231 | mat.m[8] *= oneOverLen; |
| 232 | mat.m[9] *= oneOverLen; |
| 233 | mat.m[10] *= oneOverLen; |
| 234 | |
| 235 | mat *= _invTransformInPhysics; |
| 236 | if (_physics3DObj->getObjType() == Physics3DObject::PhysicsObjType::RIGID_BODY) |
| 237 | { |
| 238 | auto body = static_cast<Physics3DRigidBody*>(_physics3DObj)->getRigidBody(); |
| 239 | auto motionState = body->getMotionState(); |
| 240 | motionState->setWorldTransform(convertMat4TobtTransform(mat)); |
| 241 | body->setMotionState(motionState); |
| 242 | } |
| 243 | else if (_physics3DObj->getObjType() == Physics3DObject::PhysicsObjType::COLLIDER) |
| 244 | { |
| 245 | auto object = static_cast<Physics3DCollider*>(_physics3DObj)->getGhostObject(); |
| 246 | object->setWorldTransform(convertMat4TobtTransform(mat)); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | } |
| 252 | |