2017-08-01 14:30:58 +02:00
|
|
|
/*
|
2022-01-06 23:37:49 +01:00
|
|
|
* Copyright (c) 2005 Erwin Coumans https://bulletphysics.org
|
2017-08-01 14:30:58 +02:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, distribute and sell this software
|
|
|
|
* and its documentation for any purpose is hereby granted without fee,
|
|
|
|
* provided that the above copyright notice appear in all copies.
|
|
|
|
* Erwin Coumans makes no representations about the suitability
|
|
|
|
* of this software for any purpose.
|
|
|
|
* It is provided "as is" without express or implied warranty.
|
|
|
|
*/
|
|
|
|
#ifndef BT_RAYCASTVEHICLE_H
|
|
|
|
#define BT_RAYCASTVEHICLE_H
|
|
|
|
|
|
|
|
#include "BulletDynamics/Dynamics/btRigidBody.h"
|
|
|
|
#include "BulletDynamics/ConstraintSolver/btTypedConstraint.h"
|
|
|
|
#include "btVehicleRaycaster.h"
|
|
|
|
class btDynamicsWorld;
|
|
|
|
#include "LinearMath/btAlignedObjectArray.h"
|
|
|
|
#include "btWheelInfo.h"
|
|
|
|
#include "BulletDynamics/Dynamics/btActionInterface.h"
|
|
|
|
|
|
|
|
//class btVehicleTuning;
|
|
|
|
|
|
|
|
///rayCast vehicle, very special constraint that turn a rigidbody into a vehicle.
|
|
|
|
class btRaycastVehicle : public btActionInterface
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btAlignedObjectArray<btVector3> m_forwardWS;
|
|
|
|
btAlignedObjectArray<btVector3> m_axle;
|
|
|
|
btAlignedObjectArray<btScalar> m_forwardImpulse;
|
|
|
|
btAlignedObjectArray<btScalar> m_sideImpulse;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
///backwards compatibility
|
|
|
|
int m_userConstraintType;
|
|
|
|
int m_userConstraintId;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
class btVehicleTuning
|
2019-01-03 14:26:51 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
btVehicleTuning()
|
|
|
|
: m_suspensionStiffness(btScalar(5.88)),
|
|
|
|
m_suspensionCompression(btScalar(0.83)),
|
|
|
|
m_suspensionDamping(btScalar(0.88)),
|
|
|
|
m_maxSuspensionTravelCm(btScalar(500.)),
|
|
|
|
m_frictionSlip(btScalar(10.5)),
|
|
|
|
m_maxSuspensionForce(btScalar(6000.))
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
|
|
|
btScalar m_suspensionStiffness;
|
|
|
|
btScalar m_suspensionCompression;
|
|
|
|
btScalar m_suspensionDamping;
|
|
|
|
btScalar m_maxSuspensionTravelCm;
|
|
|
|
btScalar m_frictionSlip;
|
|
|
|
btScalar m_maxSuspensionForce;
|
|
|
|
};
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
private:
|
|
|
|
btVehicleRaycaster* m_vehicleRaycaster;
|
|
|
|
btScalar m_pitchControl;
|
|
|
|
btScalar m_steeringValue;
|
2017-08-01 14:30:58 +02:00
|
|
|
btScalar m_currentVehicleSpeedKmHour;
|
|
|
|
|
|
|
|
btRigidBody* m_chassisBody;
|
|
|
|
|
|
|
|
int m_indexRightAxis;
|
|
|
|
int m_indexUpAxis;
|
2019-01-03 14:26:51 +01:00
|
|
|
int m_indexForwardAxis;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
void defaultInit(const btVehicleTuning& tuning);
|
|
|
|
|
|
|
|
public:
|
|
|
|
//constructor to create a car from an existing rigidbody
|
2019-01-03 14:26:51 +01:00
|
|
|
btRaycastVehicle(const btVehicleTuning& tuning, btRigidBody* chassis, btVehicleRaycaster* raycaster);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
virtual ~btRaycastVehicle();
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
///btActionInterface interface
|
2019-01-03 14:26:51 +01:00
|
|
|
virtual void updateAction(btCollisionWorld* collisionWorld, btScalar step)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
(void)collisionWorld;
|
2017-08-01 14:30:58 +02:00
|
|
|
updateVehicle(step);
|
|
|
|
}
|
|
|
|
|
|
|
|
///btActionInterface interface
|
2019-01-03 14:26:51 +01:00
|
|
|
void debugDraw(btIDebugDraw* debugDrawer);
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
const btTransform& getChassisWorldTransform() const;
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
btScalar rayCast(btWheelInfo& wheel);
|
|
|
|
|
|
|
|
virtual void updateVehicle(btScalar step);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
void resetSuspension();
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar getSteeringValue(int wheel) const;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void setSteeringValue(btScalar steering, int wheel);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void applyEngineForce(btScalar force, int wheel);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
const btTransform& getWheelTransformWS(int wheelIndex) const;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void updateWheelTransform(int wheelIndex, bool interpolatedTransform = true);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
// void setRaycastWheelInfo( int wheelIndex , bool isInContact, const btVector3& hitPoint, const btVector3& hitNormal,btScalar depth);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btWheelInfo& addWheel(const btVector3& connectionPointCS0, const btVector3& wheelDirectionCS0, const btVector3& wheelAxleCS, btScalar suspensionRestLength, btScalar wheelRadius, const btVehicleTuning& tuning, bool isFrontWheel);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
inline int getNumWheels() const
|
|
|
|
{
|
|
|
|
return int(m_wheelInfo.size());
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btAlignedObjectArray<btWheelInfo> m_wheelInfo;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
const btWheelInfo& getWheelInfo(int index) const;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btWheelInfo& getWheelInfo(int index);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void updateWheelTransformsWS(btWheelInfo& wheel, bool interpolatedTransform = true);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void setBrake(btScalar brake, int wheelIndex);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void setPitchControl(btScalar pitch)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
m_pitchControl = pitch;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void updateSuspension(btScalar deltaTime);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
virtual void updateFriction(btScalar timeStep);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
inline btRigidBody* getRigidBody()
|
|
|
|
{
|
|
|
|
return m_chassisBody;
|
|
|
|
}
|
|
|
|
|
|
|
|
const btRigidBody* getRigidBody() const
|
|
|
|
{
|
|
|
|
return m_chassisBody;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
inline int getRightAxis() const
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
return m_indexRightAxis;
|
|
|
|
}
|
|
|
|
inline int getUpAxis() const
|
|
|
|
{
|
|
|
|
return m_indexUpAxis;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int getForwardAxis() const
|
|
|
|
{
|
|
|
|
return m_indexForwardAxis;
|
|
|
|
}
|
|
|
|
|
|
|
|
///Worldspace forward vector
|
|
|
|
btVector3 getForwardVector() const
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
const btTransform& chassisTrans = getChassisWorldTransform();
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 forwardW(
|
|
|
|
chassisTrans.getBasis()[0][m_indexForwardAxis],
|
|
|
|
chassisTrans.getBasis()[1][m_indexForwardAxis],
|
|
|
|
chassisTrans.getBasis()[2][m_indexForwardAxis]);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
return forwardW;
|
|
|
|
}
|
|
|
|
|
|
|
|
///Velocity of vehicle (positive if velocity vector has same direction as foward vector)
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar getCurrentSpeedKmHour() const
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
return m_currentVehicleSpeedKmHour;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
virtual void setCoordinateSystem(int rightIndex, int upIndex, int forwardIndex)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
m_indexRightAxis = rightIndex;
|
|
|
|
m_indexUpAxis = upIndex;
|
|
|
|
m_indexForwardAxis = forwardIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
///backwards compatibility
|
|
|
|
int getUserConstraintType() const
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
return m_userConstraintType;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void setUserConstraintType(int userConstraintType)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
m_userConstraintType = userConstraintType;
|
|
|
|
};
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void setUserConstraintId(int uid)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
m_userConstraintId = uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getUserConstraintId() const
|
|
|
|
{
|
|
|
|
return m_userConstraintId;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class btDefaultVehicleRaycaster : public btVehicleRaycaster
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btDynamicsWorld* m_dynamicsWorld;
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
public:
|
|
|
|
btDefaultVehicleRaycaster(btDynamicsWorld* world)
|
2019-01-03 14:26:51 +01:00
|
|
|
: m_dynamicsWorld(world)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
virtual void* castRay(const btVector3& from, const btVector3& to, btVehicleRaycasterResult& result);
|
2017-08-01 14:30:58 +02:00
|
|
|
};
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
#endif //BT_RAYCASTVEHICLE_H
|