Skip to content

Commit

Permalink
UPBGE: Add utilities to convert bullet vector and matrices to moto.
Browse files Browse the repository at this point in the history
To helper function were added to convert data from bullet to moto
and the inverse, they are:

ToMoto(btVector3/btVector4/btQuaternion/btMatrix3x3)
ToBullet(MT_Vector3/MT_Vector4/MT_Quaternion/MT_Matrix3x3)

In the same time PHY_MotionState now use only Moto type instead of
float arrays or references.
  • Loading branch information
panzergame committed Jun 13, 2017
1 parent 4fd1407 commit 5e9e083
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 322 deletions.
4 changes: 1 addition & 3 deletions source/gameengine/Converter/BL_BlenderDataConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,9 +764,7 @@ static PHY_ShapeProps *CreateShapePropsFromBlenderObject(struct Object* blendero
shapeProps->m_lin_drag = 1.0f - blenderobject->damping;
shapeProps->m_ang_drag = 1.0f - blenderobject->rdamping;

shapeProps->m_friction_scaling[0] = blenderobject->anisotropicFriction[0];
shapeProps->m_friction_scaling[1] = blenderobject->anisotropicFriction[1];
shapeProps->m_friction_scaling[2] = blenderobject->anisotropicFriction[2];
shapeProps->m_friction_scaling = MT_Vector3(blenderobject->anisotropicFriction);
shapeProps->m_do_anisotropic = ((blenderobject->gameflag & OB_ANISOTROPIC_FRICTION) != 0);

shapeProps->m_do_fh = (blenderobject->gameflag & OB_DO_FH) != 0;
Expand Down
48 changes: 11 additions & 37 deletions source/gameengine/Ketsji/KX_MotionState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,66 +35,40 @@
KX_MotionState::KX_MotionState(SG_Node *node)
:m_node(node)
{

}

KX_MotionState::~KX_MotionState()
{
}

void KX_MotionState::GetWorldPosition(float& posX, float& posY, float& posZ)
{
const MT_Vector3& pos = m_node->GetWorldPosition();
posX = pos[0];
posY = pos[1];
posZ = pos[2];
}

void KX_MotionState::GetWorldScaling(float& scaleX, float& scaleY, float& scaleZ)
MT_Vector3 KX_MotionState::GetWorldPosition() const
{
const MT_Vector3& scale = m_node->GetWorldScaling();
scaleX = scale[0];
scaleY = scale[1];
scaleZ = scale[2];
return m_node->GetWorldPosition();
}

void KX_MotionState::GetWorldOrientation(float& quatIma0, float& quatIma1, float& quatIma2, float& quatReal)
MT_Vector3 KX_MotionState::GetWorldScaling() const
{
MT_Quaternion orn = m_node->GetWorldOrientation().getRotation();
quatIma0 = orn[0];
quatIma1 = orn[1];
quatIma2 = orn[2];
quatReal = orn[3];
return m_node->GetWorldScaling();
}

void KX_MotionState::GetWorldOrientation(float *ori)
MT_Matrix3x3 KX_MotionState::GetWorldOrientation() const
{
const MT_Matrix3x3& mat = m_node->GetWorldOrientation();
mat.getValue(ori);
return m_node->GetWorldOrientation();
}

void KX_MotionState::SetWorldOrientation(const float *ori)
void KX_MotionState::SetWorldOrientation(const MT_Matrix3x3& ori)
{
m_node->SetLocalOrientation(ori);
}

void KX_MotionState::SetWorldPosition(float posX, float posY, float posZ)
void KX_MotionState::SetWorldPosition(const MT_Vector3& pos)
{
m_node->SetLocalPosition(MT_Vector3(posX, posY, posZ));
//m_node->SetWorldPosition(MT_Vector3(posX,posY,posZ));
m_node->SetLocalPosition(pos);
}

void KX_MotionState::SetWorldOrientation(float quatIma0, float quatIma1, float quatIma2, float quatReal)
void KX_MotionState::SetWorldOrientation(const MT_Quaternion& quat)
{
MT_Quaternion orn;
orn[0] = quatIma0;
orn[1] = quatIma1;
orn[2] = quatIma2;
orn[3] = quatReal;

m_node->SetLocalOrientation(MT_Matrix3x3(orn));
//m_node->SetWorldOrientation(orn);

m_node->SetLocalOrientation(MT_Matrix3x3(quat));
}

void KX_MotionState::CalculateWorldTransformations()
Expand Down
14 changes: 7 additions & 7 deletions source/gameengine/Ketsji/KX_MotionState.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ class KX_MotionState : public PHY_IMotionState
KX_MotionState(SG_Node *spatial);
virtual ~KX_MotionState();

virtual void GetWorldPosition(float& posX, float& posY, float& posZ);
virtual void GetWorldScaling(float& scaleX, float& scaleY, float& scaleZ);
virtual void GetWorldOrientation(float& quatIma0, float& quatIma1, float& quatIma2, float& quatReal);
virtual void SetWorldPosition(float posX, float posY, float posZ);
virtual void SetWorldOrientation(float quatIma0, float quatIma1, float quatIma2, float quatReal);
virtual void GetWorldOrientation(float *ori);
virtual void SetWorldOrientation(const float *ori);
virtual MT_Vector3 GetWorldPosition() const;
virtual MT_Vector3 GetWorldScaling() const;
virtual MT_Matrix3x3 GetWorldOrientation() const;

virtual void SetWorldPosition(const MT_Vector3& pos);
virtual void SetWorldOrientation(const MT_Matrix3x3& ori);
virtual void SetWorldOrientation(const MT_Quaternion& quat);

virtual void CalculateWorldTransformations();
};
Expand Down
7 changes: 2 additions & 5 deletions source/gameengine/Ketsji/KX_NearSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,8 @@ void KX_NearSensor::SynchronizeTransform()
{
PHY_IMotionState* motionState = m_physCtrl->GetMotionState();
KX_GameObject* parent = ((KX_GameObject*)GetParent());
const MT_Vector3& pos = parent->NodeGetWorldPosition();
float ori[12];
parent->NodeGetWorldOrientation().getValue(ori);
motionState->SetWorldPosition(pos[0], pos[1], pos[2]);
motionState->SetWorldOrientation(ori);
motionState->SetWorldPosition(parent->NodeGetWorldPosition());
motionState->SetWorldOrientation(parent->NodeGetWorldOrientation());
m_physCtrl->WriteMotionStateToDynamics(true);
}
}
Expand Down
10 changes: 3 additions & 7 deletions source/gameengine/Ketsji/KX_RadarSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,10 @@ void KX_RadarSensor::SynchronizeTransform()
m_cone_target[2] = temp[2];


if (m_physCtrl)
{
if (m_physCtrl) {
PHY_IMotionState* motionState = m_physCtrl->GetMotionState();
const MT_Vector3& pos = trans.getOrigin();
float ori[12];
trans.getBasis().getValue(ori);
motionState->SetWorldPosition(pos[0], pos[1], pos[2]);
motionState->SetWorldOrientation(ori);
motionState->SetWorldPosition(trans.getOrigin());
motionState->SetWorldOrientation(trans.getBasis());
m_physCtrl->WriteMotionStateToDynamics(true);
}

Expand Down
11 changes: 3 additions & 8 deletions source/gameengine/Ketsji/KX_VehicleWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,9 @@ PyObject *KX_VehicleWrapper::PyGetWheelPosition(PyObject *args)

if (PyArg_ParseTuple(args,"i:getWheelPosition",&wheelIndex))
{
float position[3];
WHEEL_INDEX_CHECK_OR_RETURN(wheelIndex, "getWheelPosition");

m_vehicle->GetWheelPosition(wheelIndex,position[0],position[1],position[2]);
MT_Vector3 pos(position[0],position[1],position[2]);
return PyObjectFrom(pos);
return PyObjectFrom(m_vehicle->GetWheelPosition(wheelIndex));
}
return nullptr;
}
Expand All @@ -165,12 +162,10 @@ PyObject *KX_VehicleWrapper::PyGetWheelOrientationQuaternion(PyObject *args)
int wheelIndex;
if (PyArg_ParseTuple(args,"i:getWheelOrientationQuaternion",&wheelIndex))
{
float orn[4];
WHEEL_INDEX_CHECK_OR_RETURN(wheelIndex, "getWheelOrientationQuaternion");

m_vehicle->GetWheelOrientationQuaternion(wheelIndex,orn[0],orn[1],orn[2],orn[3]);
MT_Quaternion quatorn(orn[0],orn[1],orn[2],orn[3]);
MT_Matrix3x3 ornmat(quatorn);
const MT_Quaternion quat = m_vehicle->GetWheelOrientationQuaternion(wheelIndex);
const MT_Matrix3x3 ornmat(quat);
return PyObjectFrom(ornmat);
}
return nullptr;
Expand Down
1 change: 1 addition & 0 deletions source/gameengine/Physics/Bullet/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ set(SRC
CcdPhysicsController.cpp
CcdGraphicController.cpp

CcdMathUtils.h
CcdGraphicController.h
CcdPhysicsController.h
CcdPhysicsEnvironment.h
Expand Down
23 changes: 5 additions & 18 deletions source/gameengine/Physics/Bullet/CcdGraphicController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,16 @@ void CcdGraphicController::SetLocalAabb(const btVector3& aabbMin, const btVector

void CcdGraphicController::SetLocalAabb(const MT_Vector3& aabbMin, const MT_Vector3& aabbMax)
{
m_localAabbMin.setValue(aabbMin[0], aabbMin[1], aabbMin[2]);
m_localAabbMax.setValue(aabbMax[0], aabbMax[1], aabbMax[2]);
SetGraphicTransform();
}

void CcdGraphicController::SetLocalAabb(const float *aabbMin, const float *aabbMax)
{
m_localAabbMin.setValue(aabbMin[0], aabbMin[1], aabbMin[2]);
m_localAabbMax.setValue(aabbMax[0], aabbMax[1], aabbMax[2]);
m_localAabbMin = ToBullet(aabbMin);
m_localAabbMax = ToBullet(aabbMax);
SetGraphicTransform();
}

void CcdGraphicController::GetAabb(btVector3& aabbMin, btVector3& aabbMax)
{
btVector3 pos;
btVector3 scale;
float ori[12];
m_motionState->GetWorldPosition(pos.m_floats[0], pos.m_floats[1], pos.m_floats[2]);
m_motionState->GetWorldScaling(scale.m_floats[0], scale.m_floats[1], scale.m_floats[2]);
m_motionState->GetWorldOrientation(ori);
btMatrix3x3 rot(ori[0], ori[4], ori[8],
ori[1], ori[5], ori[9],
ori[2], ori[6], ori[10]);
const btVector3 pos = ToBullet(m_motionState->GetWorldPosition());
const btVector3 scale = ToBullet(m_motionState->GetWorldScaling());
const btMatrix3x3 rot = ToBullet(m_motionState->GetWorldOrientation());

btVector3 localAabbMin = m_localAabbMin;
btVector3 localAabbMax = m_localAabbMax;
Expand Down
1 change: 0 additions & 1 deletion source/gameengine/Physics/Bullet/CcdGraphicController.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class CcdGraphicController : public PHY_IGraphicController

void SetLocalAabb(const btVector3& aabbMin, const btVector3& aabbMax);
virtual void SetLocalAabb(const MT_Vector3& aabbMin, const MT_Vector3& aabbMax);
virtual void SetLocalAabb(const float aabbMin[3], const float aabbMax[3]);

PHY_IMotionState *GetMotionState()
{
Expand Down
57 changes: 57 additions & 0 deletions source/gameengine/Physics/Bullet/CcdMathUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#ifndef __CCD_MATH_UTILS__
#define __CCD_MATH_UTILS__

#include "MT_Vector3.h"
#include "MT_Vector4.h"
#include "MT_Quaternion.h"
#include "MT_Matrix3x3.h"

#include "LinearMath/btVector3.h"
#include "LinearMath/btQuaternion.h"
#include "LinearMath/btMatrix3x3.h"

inline MT_Vector3 ToMoto(const btVector3& vec)
{
return MT_Vector3(vec.x(), vec.y(), vec.z());
}

inline MT_Vector4 ToMoto(const btVector4& vec)
{
return MT_Vector4(vec.x(), vec.y(), vec.z(), vec.w());
}

inline MT_Matrix3x3 ToMoto(const btMatrix3x3& mat)
{
return MT_Matrix3x3(mat[0][0], mat[0][1], mat[0][2],
mat[1][0], mat[1][1], mat[1][2],
mat[2][0], mat[2][1], mat[2][2]);
}

inline MT_Quaternion ToMoto(const btQuaternion& quat)
{
return MT_Quaternion(quat.x(), quat.y(), quat.z(), quat.w());
}

inline btVector3 ToBullet(const MT_Vector3& vec)
{
return btVector3(vec.x(), vec.y(), vec.z());
}

inline btVector4 ToBullet(const MT_Vector4& vec)
{
return btVector4(vec.x(), vec.y(), vec.z(), vec.w());
}

inline btMatrix3x3 ToBullet(const MT_Matrix3x3& mat)
{
return btMatrix3x3(mat[0][0], mat[0][1], mat[0][2],
mat[1][0], mat[1][1], mat[1][2],
mat[2][0], mat[2][1], mat[2][2]);
}

inline btQuaternion ToBullet(const MT_Quaternion& quat)
{
return btQuaternion(quat.x(), quat.y(), quat.z(), quat.w());
}

#endif // __CCD_MATH_UTILS__
Loading

0 comments on commit 5e9e083

Please sign in to comment.