2017-08-01 14:30:58 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2005 Erwin Coumans http://continuousphysics.com/Bullet/
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "LinearMath/btVector3.h"
|
|
|
|
#include "btRaycastVehicle.h"
|
|
|
|
|
|
|
|
#include "BulletDynamics/ConstraintSolver/btSolve2LinearConstraint.h"
|
|
|
|
#include "BulletDynamics/ConstraintSolver/btJacobianEntry.h"
|
|
|
|
#include "LinearMath/btQuaternion.h"
|
|
|
|
#include "BulletDynamics/Dynamics/btDynamicsWorld.h"
|
|
|
|
#include "btVehicleRaycaster.h"
|
|
|
|
#include "btWheelInfo.h"
|
|
|
|
#include "LinearMath/btMinMax.h"
|
|
|
|
#include "LinearMath/btIDebugDraw.h"
|
|
|
|
#include "BulletDynamics/ConstraintSolver/btContactConstraint.h"
|
|
|
|
|
|
|
|
#define ROLLING_INFLUENCE_FIX
|
|
|
|
|
|
|
|
btRigidBody& btActionInterface::getFixedBody()
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
static btRigidBody s_fixed(0, 0, 0);
|
|
|
|
s_fixed.setMassProps(btScalar(0.), btVector3(btScalar(0.), btScalar(0.), btScalar(0.)));
|
2017-08-01 14:30:58 +02:00
|
|
|
return s_fixed;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btRaycastVehicle::btRaycastVehicle(const btVehicleTuning& tuning, btRigidBody* chassis, btVehicleRaycaster* raycaster)
|
|
|
|
: m_vehicleRaycaster(raycaster),
|
|
|
|
m_pitchControl(btScalar(0.))
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
m_chassisBody = chassis;
|
|
|
|
m_indexRightAxis = 0;
|
|
|
|
m_indexUpAxis = 2;
|
|
|
|
m_indexForwardAxis = 1;
|
|
|
|
defaultInit(tuning);
|
|
|
|
}
|
|
|
|
|
|
|
|
void btRaycastVehicle::defaultInit(const btVehicleTuning& tuning)
|
|
|
|
{
|
|
|
|
(void)tuning;
|
|
|
|
m_currentVehicleSpeedKmHour = btScalar(0.);
|
|
|
|
m_steeringValue = btScalar(0.);
|
|
|
|
}
|
|
|
|
|
|
|
|
btRaycastVehicle::~btRaycastVehicle()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// basically most of the code is general for 2 or 4 wheel vehicles, but some of it needs to be reviewed
|
|
|
|
//
|
2019-01-03 14:26:51 +01:00
|
|
|
btWheelInfo& btRaycastVehicle::addWheel(const btVector3& connectionPointCS, const btVector3& wheelDirectionCS0, const btVector3& wheelAxleCS, btScalar suspensionRestLength, btScalar wheelRadius, const btVehicleTuning& tuning, bool isFrontWheel)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btWheelInfoConstructionInfo ci;
|
|
|
|
|
|
|
|
ci.m_chassisConnectionCS = connectionPointCS;
|
|
|
|
ci.m_wheelDirectionCS = wheelDirectionCS0;
|
|
|
|
ci.m_wheelAxleCS = wheelAxleCS;
|
|
|
|
ci.m_suspensionRestLength = suspensionRestLength;
|
|
|
|
ci.m_wheelRadius = wheelRadius;
|
|
|
|
ci.m_suspensionStiffness = tuning.m_suspensionStiffness;
|
|
|
|
ci.m_wheelsDampingCompression = tuning.m_suspensionCompression;
|
|
|
|
ci.m_wheelsDampingRelaxation = tuning.m_suspensionDamping;
|
|
|
|
ci.m_frictionSlip = tuning.m_frictionSlip;
|
|
|
|
ci.m_bIsFrontWheel = isFrontWheel;
|
|
|
|
ci.m_maxSuspensionTravelCm = tuning.m_maxSuspensionTravelCm;
|
|
|
|
ci.m_maxSuspensionForce = tuning.m_maxSuspensionForce;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
m_wheelInfo.push_back(btWheelInfo(ci));
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btWheelInfo& wheel = m_wheelInfo[getNumWheels() - 1];
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
updateWheelTransformsWS(wheel, false);
|
|
|
|
updateWheelTransform(getNumWheels() - 1, false);
|
|
|
|
return wheel;
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
const btTransform& btRaycastVehicle::getWheelTransformWS(int wheelIndex) const
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btAssert(wheelIndex < getNumWheels());
|
|
|
|
const btWheelInfo& wheel = m_wheelInfo[wheelIndex];
|
|
|
|
return wheel.m_worldTransform;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btRaycastVehicle::updateWheelTransform(int wheelIndex, bool interpolatedTransform)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btWheelInfo& wheel = m_wheelInfo[wheelIndex];
|
|
|
|
updateWheelTransformsWS(wheel, interpolatedTransform);
|
2017-08-01 14:30:58 +02:00
|
|
|
btVector3 up = -wheel.m_raycastInfo.m_wheelDirectionWS;
|
|
|
|
const btVector3& right = wheel.m_raycastInfo.m_wheelAxleWS;
|
|
|
|
btVector3 fwd = up.cross(right);
|
|
|
|
fwd = fwd.normalize();
|
2019-01-03 14:26:51 +01:00
|
|
|
// up = right.cross(fwd);
|
|
|
|
// up.normalize();
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
//rotate around steering over de wheelAxleWS
|
|
|
|
btScalar steering = wheel.m_steering;
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
btQuaternion steeringOrn(up, steering); //wheel.m_steering);
|
2017-08-01 14:30:58 +02:00
|
|
|
btMatrix3x3 steeringMat(steeringOrn);
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btQuaternion rotatingOrn(right, -wheel.m_rotation);
|
2017-08-01 14:30:58 +02:00
|
|
|
btMatrix3x3 rotatingMat(rotatingOrn);
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btMatrix3x3 basis2;
|
|
|
|
basis2[0][m_indexRightAxis] = -right[0];
|
|
|
|
basis2[1][m_indexRightAxis] = -right[1];
|
|
|
|
basis2[2][m_indexRightAxis] = -right[2];
|
2018-09-07 16:11:04 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
basis2[0][m_indexUpAxis] = up[0];
|
|
|
|
basis2[1][m_indexUpAxis] = up[1];
|
|
|
|
basis2[2][m_indexUpAxis] = up[2];
|
2018-09-07 16:11:04 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
basis2[0][m_indexForwardAxis] = fwd[0];
|
|
|
|
basis2[1][m_indexForwardAxis] = fwd[1];
|
|
|
|
basis2[2][m_indexForwardAxis] = fwd[2];
|
2018-09-07 16:11:04 +02:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
wheel.m_worldTransform.setBasis(steeringMat * rotatingMat * basis2);
|
|
|
|
wheel.m_worldTransform.setOrigin(
|
2019-01-03 14:26:51 +01:00
|
|
|
wheel.m_raycastInfo.m_hardPointWS + wheel.m_raycastInfo.m_wheelDirectionWS * wheel.m_raycastInfo.m_suspensionLength);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void btRaycastVehicle::resetSuspension()
|
|
|
|
{
|
|
|
|
int i;
|
2019-01-03 14:26:51 +01:00
|
|
|
for (i = 0; i < m_wheelInfo.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btWheelInfo& wheel = m_wheelInfo[i];
|
|
|
|
wheel.m_raycastInfo.m_suspensionLength = wheel.getSuspensionRestLength();
|
|
|
|
wheel.m_suspensionRelativeVelocity = btScalar(0.0);
|
|
|
|
|
|
|
|
wheel.m_raycastInfo.m_contactNormalWS = -wheel.m_raycastInfo.m_wheelDirectionWS;
|
|
|
|
//wheel_info.setContactFriction(btScalar(0.0));
|
|
|
|
wheel.m_clippedInvContactDotSuspension = btScalar(1.0);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btRaycastVehicle::updateWheelTransformsWS(btWheelInfo& wheel, bool interpolatedTransform)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
wheel.m_raycastInfo.m_isInContact = false;
|
|
|
|
|
|
|
|
btTransform chassisTrans = getChassisWorldTransform();
|
|
|
|
if (interpolatedTransform && (getRigidBody()->getMotionState()))
|
|
|
|
{
|
|
|
|
getRigidBody()->getMotionState()->getWorldTransform(chassisTrans);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
wheel.m_raycastInfo.m_hardPointWS = chassisTrans(wheel.m_chassisConnectionPointCS);
|
|
|
|
wheel.m_raycastInfo.m_wheelDirectionWS = chassisTrans.getBasis() * wheel.m_wheelDirectionCS;
|
2017-08-01 14:30:58 +02:00
|
|
|
wheel.m_raycastInfo.m_wheelAxleWS = chassisTrans.getBasis() * wheel.m_wheelAxleCS;
|
|
|
|
}
|
|
|
|
|
|
|
|
btScalar btRaycastVehicle::rayCast(btWheelInfo& wheel)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
updateWheelTransformsWS(wheel, false);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
btScalar depth = -1;
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
btScalar raylen = wheel.getSuspensionRestLength() + wheel.m_wheelsRadius;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
btVector3 rayvector = wheel.m_raycastInfo.m_wheelDirectionWS * (raylen);
|
|
|
|
const btVector3& source = wheel.m_raycastInfo.m_hardPointWS;
|
|
|
|
wheel.m_raycastInfo.m_contactPointWS = source + rayvector;
|
|
|
|
const btVector3& target = wheel.m_raycastInfo.m_contactPointWS;
|
|
|
|
|
|
|
|
btScalar param = btScalar(0.);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
btVehicleRaycaster::btVehicleRaycasterResult rayResults;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
btAssert(m_vehicleRaycaster);
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void* object = m_vehicleRaycaster->castRay(source, target, rayResults);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
wheel.m_raycastInfo.m_groundObject = 0;
|
|
|
|
|
|
|
|
if (object)
|
|
|
|
{
|
|
|
|
param = rayResults.m_distFraction;
|
|
|
|
depth = raylen * rayResults.m_distFraction;
|
2019-01-03 14:26:51 +01:00
|
|
|
wheel.m_raycastInfo.m_contactNormalWS = rayResults.m_hitNormalInWorld;
|
2017-08-01 14:30:58 +02:00
|
|
|
wheel.m_raycastInfo.m_isInContact = true;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
wheel.m_raycastInfo.m_groundObject = &getFixedBody(); ///@todo for driving on dynamic/movable objects!;
|
|
|
|
//wheel.m_raycastInfo.m_groundObject = object;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar hitDistance = param * raylen;
|
2017-08-01 14:30:58 +02:00
|
|
|
wheel.m_raycastInfo.m_suspensionLength = hitDistance - wheel.m_wheelsRadius;
|
|
|
|
//clamp on max suspension travel
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar minSuspensionLength = wheel.getSuspensionRestLength() - wheel.m_maxSuspensionTravelCm * btScalar(0.01);
|
|
|
|
btScalar maxSuspensionLength = wheel.getSuspensionRestLength() + wheel.m_maxSuspensionTravelCm * btScalar(0.01);
|
2017-08-01 14:30:58 +02:00
|
|
|
if (wheel.m_raycastInfo.m_suspensionLength < minSuspensionLength)
|
|
|
|
{
|
|
|
|
wheel.m_raycastInfo.m_suspensionLength = minSuspensionLength;
|
|
|
|
}
|
|
|
|
if (wheel.m_raycastInfo.m_suspensionLength > maxSuspensionLength)
|
|
|
|
{
|
|
|
|
wheel.m_raycastInfo.m_suspensionLength = maxSuspensionLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
wheel.m_raycastInfo.m_contactPointWS = rayResults.m_hitPointInWorld;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar denominator = wheel.m_raycastInfo.m_contactNormalWS.dot(wheel.m_raycastInfo.m_wheelDirectionWS);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
btVector3 chassis_velocity_at_contactPoint;
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 relpos = wheel.m_raycastInfo.m_contactPointWS - getRigidBody()->getCenterOfMassPosition();
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
chassis_velocity_at_contactPoint = getRigidBody()->getVelocityInLocalPoint(relpos);
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar projVel = wheel.m_raycastInfo.m_contactNormalWS.dot(chassis_velocity_at_contactPoint);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (denominator >= btScalar(-0.1))
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
wheel.m_suspensionRelativeVelocity = btScalar(0.0);
|
|
|
|
wheel.m_clippedInvContactDotSuspension = btScalar(1.0) / btScalar(0.1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
btScalar inv = btScalar(-1.) / denominator;
|
|
|
|
wheel.m_suspensionRelativeVelocity = projVel * inv;
|
|
|
|
wheel.m_clippedInvContactDotSuspension = inv;
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
//put wheel info as in rest position
|
|
|
|
wheel.m_raycastInfo.m_suspensionLength = wheel.getSuspensionRestLength();
|
|
|
|
wheel.m_suspensionRelativeVelocity = btScalar(0.0);
|
2019-01-03 14:26:51 +01:00
|
|
|
wheel.m_raycastInfo.m_contactNormalWS = -wheel.m_raycastInfo.m_wheelDirectionWS;
|
2017-08-01 14:30:58 +02:00
|
|
|
wheel.m_clippedInvContactDotSuspension = btScalar(1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
const btTransform& btRaycastVehicle::getChassisWorldTransform() const
|
|
|
|
{
|
|
|
|
/*if (getRigidBody()->getMotionState())
|
|
|
|
{
|
|
|
|
btTransform chassisWorldTrans;
|
|
|
|
getRigidBody()->getMotionState()->getWorldTransform(chassisWorldTrans);
|
|
|
|
return chassisWorldTrans;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
return getRigidBody()->getCenterOfMassTransform();
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btRaycastVehicle::updateVehicle(btScalar step)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < getNumWheels(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
updateWheelTransform(i, false);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_currentVehicleSpeedKmHour = btScalar(3.6) * getRigidBody()->getLinearVelocity().length();
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
const btTransform& chassisTrans = getChassisWorldTransform();
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 forwardW(
|
2017-08-01 14:30:58 +02:00
|
|
|
chassisTrans.getBasis()[0][m_indexForwardAxis],
|
|
|
|
chassisTrans.getBasis()[1][m_indexForwardAxis],
|
|
|
|
chassisTrans.getBasis()[2][m_indexForwardAxis]);
|
|
|
|
|
|
|
|
if (forwardW.dot(getRigidBody()->getLinearVelocity()) < btScalar(0.))
|
|
|
|
{
|
|
|
|
m_currentVehicleSpeedKmHour *= btScalar(-1.);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// simulate suspension
|
|
|
|
//
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < m_wheelInfo.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
//btScalar depth;
|
|
|
|
//depth =
|
|
|
|
rayCast(m_wheelInfo[i]);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
updateSuspension(step);
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (i = 0; i < m_wheelInfo.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
//apply suspension force
|
|
|
|
btWheelInfo& wheel = m_wheelInfo[i];
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
btScalar suspensionForce = wheel.m_wheelsSuspensionForce;
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
if (suspensionForce > wheel.m_maxSuspensionForce)
|
|
|
|
{
|
|
|
|
suspensionForce = wheel.m_maxSuspensionForce;
|
|
|
|
}
|
|
|
|
btVector3 impulse = wheel.m_raycastInfo.m_contactNormalWS * suspensionForce * step;
|
|
|
|
btVector3 relpos = wheel.m_raycastInfo.m_contactPointWS - getRigidBody()->getCenterOfMassPosition();
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
getRigidBody()->applyImpulse(impulse, relpos);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
updateFriction(step);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (i = 0; i < m_wheelInfo.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btWheelInfo& wheel = m_wheelInfo[i];
|
|
|
|
btVector3 relpos = wheel.m_raycastInfo.m_hardPointWS - getRigidBody()->getCenterOfMassPosition();
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 vel = getRigidBody()->getVelocityInLocalPoint(relpos);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
if (wheel.m_raycastInfo.m_isInContact)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
const btTransform& chassisWorldTransform = getChassisWorldTransform();
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 fwd(
|
2017-08-01 14:30:58 +02:00
|
|
|
chassisWorldTransform.getBasis()[0][m_indexForwardAxis],
|
|
|
|
chassisWorldTransform.getBasis()[1][m_indexForwardAxis],
|
|
|
|
chassisWorldTransform.getBasis()[2][m_indexForwardAxis]);
|
|
|
|
|
|
|
|
btScalar proj = fwd.dot(wheel.m_raycastInfo.m_contactNormalWS);
|
|
|
|
fwd -= wheel.m_raycastInfo.m_contactNormalWS * proj;
|
|
|
|
|
|
|
|
btScalar proj2 = fwd.dot(vel);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
wheel.m_deltaRotation = (proj2 * step) / (wheel.m_wheelsRadius);
|
|
|
|
wheel.m_rotation += wheel.m_deltaRotation;
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
wheel.m_rotation += wheel.m_deltaRotation;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
wheel.m_deltaRotation *= btScalar(0.99); //damping of rotation when not in contact
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btRaycastVehicle::setSteeringValue(btScalar steering, int wheel)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btAssert(wheel >= 0 && wheel < getNumWheels());
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
btWheelInfo& wheelInfo = getWheelInfo(wheel);
|
|
|
|
wheelInfo.m_steering = steering;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar btRaycastVehicle::getSteeringValue(int wheel) const
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
return getWheelInfo(wheel).m_steering;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btRaycastVehicle::applyEngineForce(btScalar force, int wheel)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btAssert(wheel >= 0 && wheel < getNumWheels());
|
2017-08-01 14:30:58 +02:00
|
|
|
btWheelInfo& wheelInfo = getWheelInfo(wheel);
|
|
|
|
wheelInfo.m_engineForce = force;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
const btWheelInfo& btRaycastVehicle::getWheelInfo(int index) const
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btAssert((index >= 0) && (index < getNumWheels()));
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_wheelInfo[index];
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btWheelInfo& btRaycastVehicle::getWheelInfo(int index)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btAssert((index >= 0) && (index < getNumWheels()));
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_wheelInfo[index];
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btRaycastVehicle::setBrake(btScalar brake, int wheelIndex)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btAssert((wheelIndex >= 0) && (wheelIndex < getNumWheels()));
|
2017-08-01 14:30:58 +02:00
|
|
|
getWheelInfo(wheelIndex).m_brake = brake;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btRaycastVehicle::updateSuspension(btScalar deltaTime)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
(void)deltaTime;
|
|
|
|
|
|
|
|
btScalar chassisMass = btScalar(1.) / m_chassisBody->getInvMass();
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
for (int w_it = 0; w_it < getNumWheels(); w_it++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btWheelInfo& wheel_info = m_wheelInfo[w_it];
|
|
|
|
|
|
|
|
if (wheel_info.m_raycastInfo.m_isInContact)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btScalar force;
|
|
|
|
// Spring
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar susp_length = wheel_info.getSuspensionRestLength();
|
|
|
|
btScalar current_length = wheel_info.m_raycastInfo.m_suspensionLength;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
btScalar length_diff = (susp_length - current_length);
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
force = wheel_info.m_suspensionStiffness * length_diff * wheel_info.m_clippedInvContactDotSuspension;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
// Damper
|
|
|
|
{
|
|
|
|
btScalar projected_rel_vel = wheel_info.m_suspensionRelativeVelocity;
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar susp_damping;
|
|
|
|
if (projected_rel_vel < btScalar(0.0))
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
susp_damping = wheel_info.m_wheelsDampingCompression;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
susp_damping = wheel_info.m_wheelsDampingRelaxation;
|
|
|
|
}
|
|
|
|
force -= susp_damping * projected_rel_vel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RESULT
|
|
|
|
wheel_info.m_wheelsSuspensionForce = force * chassisMass;
|
|
|
|
if (wheel_info.m_wheelsSuspensionForce < btScalar(0.))
|
|
|
|
{
|
|
|
|
wheel_info.m_wheelsSuspensionForce = btScalar(0.);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wheel_info.m_wheelsSuspensionForce = btScalar(0.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct btWheelContactPoint
|
|
|
|
{
|
|
|
|
btRigidBody* m_body0;
|
|
|
|
btRigidBody* m_body1;
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 m_frictionPositionWorld;
|
|
|
|
btVector3 m_frictionDirectionWorld;
|
|
|
|
btScalar m_jacDiagABInv;
|
|
|
|
btScalar m_maxImpulse;
|
|
|
|
|
|
|
|
btWheelContactPoint(btRigidBody* body0, btRigidBody* body1, const btVector3& frictionPosWorld, const btVector3& frictionDirectionWorld, btScalar maxImpulse)
|
|
|
|
: m_body0(body0),
|
|
|
|
m_body1(body1),
|
|
|
|
m_frictionPositionWorld(frictionPosWorld),
|
|
|
|
m_frictionDirectionWorld(frictionDirectionWorld),
|
|
|
|
m_maxImpulse(maxImpulse)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar denom0 = body0->computeImpulseDenominator(frictionPosWorld, frictionDirectionWorld);
|
|
|
|
btScalar denom1 = body1->computeImpulseDenominator(frictionPosWorld, frictionDirectionWorld);
|
|
|
|
btScalar relaxation = 1.f;
|
|
|
|
m_jacDiagABInv = relaxation / (denom0 + denom1);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-07 16:11:04 +02:00
|
|
|
btScalar calcRollingFriction(btWheelContactPoint& contactPoint, int numWheelsOnGround);
|
|
|
|
btScalar calcRollingFriction(btWheelContactPoint& contactPoint, int numWheelsOnGround)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar j1 = 0.f;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
const btVector3& contactPosWorld = contactPoint.m_frictionPositionWorld;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 rel_pos1 = contactPosWorld - contactPoint.m_body0->getCenterOfMassPosition();
|
2017-08-01 14:30:58 +02:00
|
|
|
btVector3 rel_pos2 = contactPosWorld - contactPoint.m_body1->getCenterOfMassPosition();
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
btScalar maxImpulse = contactPoint.m_maxImpulse;
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
btVector3 vel1 = contactPoint.m_body0->getVelocityInLocalPoint(rel_pos1);
|
|
|
|
btVector3 vel2 = contactPoint.m_body1->getVelocityInLocalPoint(rel_pos2);
|
|
|
|
btVector3 vel = vel1 - vel2;
|
|
|
|
|
|
|
|
btScalar vrel = contactPoint.m_frictionDirectionWorld.dot(vel);
|
|
|
|
|
|
|
|
// calculate j that moves us to zero relative velocity
|
2019-01-03 14:26:51 +01:00
|
|
|
j1 = -vrel * contactPoint.m_jacDiagABInv / btScalar(numWheelsOnGround);
|
2017-08-01 14:30:58 +02:00
|
|
|
btSetMin(j1, maxImpulse);
|
|
|
|
btSetMax(j1, -maxImpulse);
|
|
|
|
|
|
|
|
return j1;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar sideFrictionStiffness2 = btScalar(1.0);
|
|
|
|
void btRaycastVehicle::updateFriction(btScalar timeStep)
|
|
|
|
{
|
|
|
|
//calculate the impulse, so that the wheels don't move sidewards
|
|
|
|
int numWheel = getNumWheels();
|
|
|
|
if (!numWheel)
|
|
|
|
return;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
m_forwardWS.resize(numWheel);
|
|
|
|
m_axle.resize(numWheel);
|
|
|
|
m_forwardImpulse.resize(numWheel);
|
|
|
|
m_sideImpulse.resize(numWheel);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int numWheelsOnGround = 0;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
//collapse all those loops into one!
|
|
|
|
for (int i = 0; i < getNumWheels(); i++)
|
|
|
|
{
|
|
|
|
btWheelInfo& wheelInfo = m_wheelInfo[i];
|
|
|
|
class btRigidBody* groundObject = (class btRigidBody*)wheelInfo.m_raycastInfo.m_groundObject;
|
|
|
|
if (groundObject)
|
|
|
|
numWheelsOnGround++;
|
|
|
|
m_sideImpulse[i] = btScalar(0.);
|
|
|
|
m_forwardImpulse[i] = btScalar(0.);
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
{
|
|
|
|
for (int i = 0; i < getNumWheels(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btWheelInfo& wheelInfo = m_wheelInfo[i];
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
class btRigidBody* groundObject = (class btRigidBody*)wheelInfo.m_raycastInfo.m_groundObject;
|
|
|
|
|
|
|
|
if (groundObject)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
const btTransform& wheelTrans = getWheelTransformWS(i);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btMatrix3x3 wheelBasis0 = wheelTrans.getBasis();
|
|
|
|
m_axle[i] = -btVector3(
|
|
|
|
wheelBasis0[0][m_indexRightAxis],
|
|
|
|
wheelBasis0[1][m_indexRightAxis],
|
|
|
|
wheelBasis0[2][m_indexRightAxis]);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
const btVector3& surfNormalWS = wheelInfo.m_raycastInfo.m_contactNormalWS;
|
|
|
|
btScalar proj = m_axle[i].dot(surfNormalWS);
|
|
|
|
m_axle[i] -= surfNormalWS * proj;
|
|
|
|
m_axle[i] = m_axle[i].normalize();
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
m_forwardWS[i] = surfNormalWS.cross(m_axle[i]);
|
|
|
|
m_forwardWS[i].normalize();
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
resolveSingleBilateral(*m_chassisBody, wheelInfo.m_raycastInfo.m_contactPointWS,
|
|
|
|
*groundObject, wheelInfo.m_raycastInfo.m_contactPointWS,
|
|
|
|
btScalar(0.), m_axle[i], m_sideImpulse[i], timeStep);
|
|
|
|
|
|
|
|
m_sideImpulse[i] *= sideFrictionStiffness2;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
btScalar sideFactor = btScalar(1.);
|
|
|
|
btScalar fwdFactor = 0.5;
|
|
|
|
|
|
|
|
bool sliding = false;
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int wheel = 0; wheel < getNumWheels(); wheel++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btWheelInfo& wheelInfo = m_wheelInfo[wheel];
|
2019-01-03 14:26:51 +01:00
|
|
|
class btRigidBody* groundObject = (class btRigidBody*)wheelInfo.m_raycastInfo.m_groundObject;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar rollingFriction = 0.f;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
if (groundObject)
|
|
|
|
{
|
|
|
|
if (wheelInfo.m_engineForce != 0.f)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
rollingFriction = wheelInfo.m_engineForce * timeStep;
|
|
|
|
}
|
|
|
|
else
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btScalar defaultRollingFrictionImpulse = 0.f;
|
|
|
|
btScalar maxImpulse = wheelInfo.m_brake ? wheelInfo.m_brake : defaultRollingFrictionImpulse;
|
2019-01-03 14:26:51 +01:00
|
|
|
btWheelContactPoint contactPt(m_chassisBody, groundObject, wheelInfo.m_raycastInfo.m_contactPointWS, m_forwardWS[wheel], maxImpulse);
|
2018-09-07 16:11:04 +02:00
|
|
|
btAssert(numWheelsOnGround > 0);
|
|
|
|
rollingFriction = calcRollingFriction(contactPt, numWheelsOnGround);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//switch between active rolling (throttle), braking and non-active rolling friction (no throttle/break)
|
|
|
|
|
|
|
|
m_forwardImpulse[wheel] = btScalar(0.);
|
2019-01-03 14:26:51 +01:00
|
|
|
m_wheelInfo[wheel].m_skidInfo = btScalar(1.);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
if (groundObject)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
m_wheelInfo[wheel].m_skidInfo = btScalar(1.);
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
btScalar maximp = wheelInfo.m_wheelsSuspensionForce * timeStep * wheelInfo.m_frictionSlip;
|
|
|
|
btScalar maximpSide = maximp;
|
|
|
|
|
|
|
|
btScalar maximpSquared = maximp * maximpSide;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
m_forwardImpulse[wheel] = rollingFriction; //wheelInfo.m_engineForce* timeStep;
|
|
|
|
|
|
|
|
btScalar x = (m_forwardImpulse[wheel]) * fwdFactor;
|
|
|
|
btScalar y = (m_sideImpulse[wheel]) * sideFactor;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar impulseSquared = (x * x + y * y);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
if (impulseSquared > maximpSquared)
|
|
|
|
{
|
|
|
|
sliding = true;
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
btScalar factor = maximp / btSqrt(impulseSquared);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
m_wheelInfo[wheel].m_skidInfo *= factor;
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (sliding)
|
|
|
|
{
|
|
|
|
for (int wheel = 0; wheel < getNumWheels(); wheel++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (m_sideImpulse[wheel] != btScalar(0.))
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (m_wheelInfo[wheel].m_skidInfo < btScalar(1.))
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
m_forwardImpulse[wheel] *= m_wheelInfo[wheel].m_skidInfo;
|
|
|
|
m_sideImpulse[wheel] *= m_wheelInfo[wheel].m_skidInfo;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
// apply the impulses
|
|
|
|
{
|
|
|
|
for (int wheel = 0; wheel < getNumWheels(); wheel++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btWheelInfo& wheelInfo = m_wheelInfo[wheel];
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 rel_pos = wheelInfo.m_raycastInfo.m_contactPointWS -
|
|
|
|
m_chassisBody->getCenterOfMassPosition();
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (m_forwardImpulse[wheel] != btScalar(0.))
|
|
|
|
{
|
|
|
|
m_chassisBody->applyImpulse(m_forwardWS[wheel] * (m_forwardImpulse[wheel]), rel_pos);
|
|
|
|
}
|
|
|
|
if (m_sideImpulse[wheel] != btScalar(0.))
|
|
|
|
{
|
|
|
|
class btRigidBody* groundObject = (class btRigidBody*)m_wheelInfo[wheel].m_raycastInfo.m_groundObject;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 rel_pos2 = wheelInfo.m_raycastInfo.m_contactPointWS -
|
|
|
|
groundObject->getCenterOfMassPosition();
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 sideImp = m_axle[wheel] * m_sideImpulse[wheel];
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
#if defined ROLLING_INFLUENCE_FIX // fix. It only worked if car's up was along Y - VT.
|
|
|
|
btVector3 vChassisWorldUp = getRigidBody()->getCenterOfMassTransform().getBasis().getColumn(m_indexUpAxis);
|
|
|
|
rel_pos -= vChassisWorldUp * (vChassisWorldUp.dot(rel_pos) * (1.f - wheelInfo.m_rollInfluence));
|
2017-08-01 14:30:58 +02:00
|
|
|
#else
|
2019-01-03 14:26:51 +01:00
|
|
|
rel_pos[m_indexUpAxis] *= wheelInfo.m_rollInfluence;
|
2017-08-01 14:30:58 +02:00
|
|
|
#endif
|
2019-01-03 14:26:51 +01:00
|
|
|
m_chassisBody->applyImpulse(sideImp, rel_pos);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
//apply friction impulse on the ground
|
|
|
|
groundObject->applyImpulse(-sideImp, rel_pos2);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btRaycastVehicle::debugDraw(btIDebugDraw* debugDrawer)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int v = 0; v < this->getNumWheels(); v++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 wheelColor(0, 1, 1);
|
2017-08-01 14:30:58 +02:00
|
|
|
if (getWheelInfo(v).m_raycastInfo.m_isInContact)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
wheelColor.setValue(0, 0, 1);
|
|
|
|
}
|
|
|
|
else
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
wheelColor.setValue(1, 0, 1);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
btVector3 wheelPosWS = getWheelInfo(v).m_worldTransform.getOrigin();
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 axle = btVector3(
|
2017-08-01 14:30:58 +02:00
|
|
|
getWheelInfo(v).m_worldTransform.getBasis()[0][getRightAxis()],
|
|
|
|
getWheelInfo(v).m_worldTransform.getBasis()[1][getRightAxis()],
|
|
|
|
getWheelInfo(v).m_worldTransform.getBasis()[2][getRightAxis()]);
|
|
|
|
|
|
|
|
//debug wheels (cylinders)
|
2019-01-03 14:26:51 +01:00
|
|
|
debugDrawer->drawLine(wheelPosWS, wheelPosWS + axle, wheelColor);
|
|
|
|
debugDrawer->drawLine(wheelPosWS, getWheelInfo(v).m_raycastInfo.m_contactPointWS, wheelColor);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void* btDefaultVehicleRaycaster::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
|
|
|
// RayResultCallback& resultCallback;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btCollisionWorld::ClosestRayResultCallback rayCallback(from, to);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
m_dynamicsWorld->rayTest(from, to, rayCallback);
|
|
|
|
|
|
|
|
if (rayCallback.hasHit())
|
|
|
|
{
|
|
|
|
const btRigidBody* body = btRigidBody::upcast(rayCallback.m_collisionObject);
|
2019-01-03 14:26:51 +01:00
|
|
|
if (body && body->hasContactResponse())
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
result.m_hitPointInWorld = rayCallback.m_hitPointWorld;
|
|
|
|
result.m_hitNormalInWorld = rayCallback.m_hitNormalWorld;
|
|
|
|
result.m_hitNormalInWorld.normalize();
|
|
|
|
result.m_distFraction = rayCallback.m_closestHitFraction;
|
|
|
|
return (void*)body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|