Browse Source

Forked bullet3 to make vehicle changes

* I also updated some of the other modules
* Added references to vehicle tuning
* Fixed linking bullet3 debug
ogre-port2
Macoy Madson 7 months ago
parent
commit
1c32187266
10 changed files with 236 additions and 20 deletions
  1. +1
    -1
      .gitmodules
  2. +2
    -1
      BuildBullet_Debug.sh
  3. +1
    -1
      BuildDependencies_Debug.sh
  4. +1
    -1
      Dependencies/base2.0
  5. +1
    -1
      Dependencies/bullet3
  6. +1
    -1
      Dependencies/tracy
  7. +1
    -0
      Jamrules
  8. +4
    -0
      docs/VehicleTuning.org
  9. +210
    -11
      src/PhysicsVehicle.cpp
  10. +14
    -3
      src/PhysicsVehicle.hpp

+ 1
- 1
.gitmodules View File

@ -1,6 +1,6 @@
[submodule "bullet3"]
path = Dependencies/bullet3
url = https://github.com/bulletphysics/bullet3
url = https://github.com/makuto/bullet3
[submodule "base2.0"]
path = Dependencies/base2.0
url = https://github.com/makuto/base2.0


+ 2
- 1
BuildBullet_Debug.sh View File

@ -4,6 +4,7 @@ cd Dependencies/bullet3
if [ -e CMakeCache.txt ]; then
rm CMakeCache.txt
fi
# rm -r build_cmake_debug
mkdir -p build_cmake_debug
cd build_cmake_debug
# cmake -DBUILD_PYBULLET=ON -DBUILD_PYBULLET_NUMPY=ON -DUSE_DOUBLE_PRECISION=ON -DBT_USE_EGL=ON -DCMAKE_BUILD_TYPE=Debug .. || exit 1
@ -11,8 +12,8 @@ cd build_cmake_debug
# Pybullet is necessary for world serialization stuff (why?)
cmake -DBUILD_PYBULLET=ON -DBUILD_PYBULLET_NUMPY=ON -DBT_USE_EGL=ON -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_CXX_COMPILER=clang++ -DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} -std=c++14 -stdlib=libstdc++" \
-DCMAKE_SHARED_LINKER_FLAGS="-stdlib=libstdc++" \
.. || exit 1
# -DCMAKE_SHARED_LINKER_FLAGS="-stdlib=libstdc++" \
# cmake -DBT_USE_EGL=ON -DCMAKE_BUILD_TYPE=Debug .. || exit 1
make -j $(command nproc 2>/dev/null || echo 12) || exit 1


+ 1
- 1
BuildDependencies_Debug.sh View File

@ -13,7 +13,7 @@ echo "Finished building Base2.0!"
echo "Building Horde3D..."
cd Dependencies/Horde3D
mkdir build && cd build
mkdir -p build && cd build
cmake .. -DCMAKE_BUILD_TYPE="Debug" \
-DCMAKE_CXX_COMPILER=clang++ -DCMAKE_CXX_FLAGS="-std=c++14 -stdlib=libstdc++" -DCMAKE_SHARED_LINKER_FLAGS="-stdlib=libstdc++" \
-DOpenGL_GL_PREFERENCE="LEGACY" || exit 1


+ 1
- 1
Dependencies/base2.0

@ -1 +1 @@
Subproject commit cf0451664674d91c3c0eb6c11981b420107d26e2
Subproject commit dbf8ce175a0e00d91a33990041cbc81802e8c309

+ 1
- 1
Dependencies/bullet3

@ -1 +1 @@
Subproject commit 3301b46367ea174f866465ce2ff7e1863aaf4a91
Subproject commit 5e7d8088ea0294be6f5dd9fcef53a12b41ac0df1

+ 1
- 1
Dependencies/tracy

@ -1 +1 @@
Subproject commit f945278959aed62d2758fd48fea348b3fca0c0eb
Subproject commit 47ef56b995d9d1396ec1eba6121ff230e5db07f3

+ 1
- 0
Jamrules View File

@ -2,6 +2,7 @@
## Compilation
##
C = clang ;
C++ = clang++ ;
LINK = clang++ ;


+ 4
- 0
docs/VehicleTuning.org View File

@ -137,4 +137,8 @@ Christensen, C. /(Personal communication, 2019-08-08)./ Email between Macoy Mads
Fernández, J. G. (2012): /A Vehicle Dynamics Model for Driving Simulators/. Department of Applied Mechanics,
Division of Vehicle Engineering and Autonomous Systems, Vehicle Dynamics at Chalmers University Of Technology. Göteborg, Sweden.
Marco Monster (2003). /[[https://web.archive.org/web/20040604214333fw_/http://home.planet.nl/~MONSTROUS/tutcar.html][Car Physics for Games]]/. Retrieved 2020-03-15.
[[https://raw.githubusercontent.com/Lumak/Urho3D-Offroad-Vehicle/master/Source/Samples/63_OffroadVehicle/Vehicle.cpp][Urho3D Offroad Vehicle]]. /Urho3D game engine/. MIT License. Retrieved 2020-02-20.
/[[https://dskjal.com/car/car-physics-for-simulator.html][シミュレータのための自動車物理]]/. Retrieved 2020-03-15.

+ 210
- 11
src/PhysicsVehicle.cpp View File

@ -15,6 +15,9 @@
#include <glm/trigonometric.hpp> //radians
#include <glm/vec3.hpp> // vec3
#include "BulletDynamics/ConstraintSolver/btContactConstraint.h"
#include "BulletDynamics/Vehicle/btWheelInfo.h"
#include <algorithm>
#include <iostream>
#include <vector>
@ -27,6 +30,199 @@ std::vector<PhysicsVehicle*> g_vehicles;
// Vehicle implementation
//
static btScalar sideFrictionStiffness2 = btScalar(1.0);
CustomRaycastVehicle::CustomRaycastVehicle(const btVehicleTuning& tuning, btRigidBody* chassis,
btVehicleRaycaster* raycaster)
: btRaycastVehicle(tuning, chassis, raycaster)
{
}
// We need to make friction a factor of velocity
void CustomRaycastVehicle::updateFriction(btScalar timeStep)
{
// calculate the impulse, so that the wheels don't move sidewards
int numWheel = getNumWheels();
if (!numWheel)
return;
m_forwardWS.resize(numWheel);
m_axle.resize(numWheel);
m_forwardImpulse.resize(numWheel);
m_sideImpulse.resize(numWheel);
int numWheelsOnGround = 0;
// 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.);
}
{
for (int i = 0; i < getNumWheels(); i++)
{
btWheelInfo& wheelInfo = m_wheelInfo[i];
class btRigidBody* groundObject =
(class btRigidBody*)wheelInfo.m_raycastInfo.m_groundObject;
if (groundObject)
{
const btTransform& wheelTrans = getWheelTransformWS(i);
btMatrix3x3 wheelBasis0 = wheelTrans.getBasis();
m_axle[i] =
-btVector3(wheelBasis0[0][m_indexRightAxis], wheelBasis0[1][m_indexRightAxis],
wheelBasis0[2][m_indexRightAxis]);
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();
m_forwardWS[i] = surfNormalWS.cross(m_axle[i]);
m_forwardWS[i].normalize();
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;
}
}
}
btScalar sideFactor = btScalar(1.);
btScalar fwdFactor = 0.5;
bool sliding = false;
{
for (int wheel = 0; wheel < getNumWheels(); wheel++)
{
btWheelInfo& wheelInfo = m_wheelInfo[wheel];
class btRigidBody* groundObject =
(class btRigidBody*)wheelInfo.m_raycastInfo.m_groundObject;
btScalar rollingFriction = 0.f;
if (groundObject)
{
if (wheelInfo.m_engineForce != 0.f)
{
rollingFriction = wheelInfo.m_engineForce * timeStep;
}
else
{
btScalar defaultRollingFrictionImpulse = 0.f;
btScalar maxImpulse =
wheelInfo.m_brake ? wheelInfo.m_brake : defaultRollingFrictionImpulse;
btWheelContactPoint contactPt(m_chassisBody, groundObject,
wheelInfo.m_raycastInfo.m_contactPointWS,
m_forwardWS[wheel], maxImpulse);
btAssert(numWheelsOnGround > 0);
rollingFriction = calcRollingFriction(contactPt, numWheelsOnGround);
}
}
// switch between active rolling (throttle), braking and non-active rolling friction
// (no throttle/break)
m_forwardImpulse[wheel] = btScalar(0.);
m_wheelInfo[wheel].m_skidInfo = btScalar(1.);
if (groundObject)
{
m_wheelInfo[wheel].m_skidInfo = btScalar(1.);
btScalar maximp =
wheelInfo.m_wheelsSuspensionForce * timeStep * wheelInfo.m_frictionSlip;
btScalar maximpSide = maximp;
btScalar maximpSquared = maximp * maximpSide;
m_forwardImpulse[wheel] = rollingFriction; // wheelInfo.m_engineForce*
// timeStep;
btScalar x = (m_forwardImpulse[wheel]) * fwdFactor;
btScalar y = (m_sideImpulse[wheel]) * sideFactor;
btScalar impulseSquared = (x * x + y * y);
if (impulseSquared > maximpSquared)
{
sliding = true;
btScalar factor = maximp / btSqrt(impulseSquared);
m_wheelInfo[wheel].m_skidInfo *= factor;
}
}
}
}
if (sliding)
{
for (int wheel = 0; wheel < getNumWheels(); wheel++)
{
if (m_sideImpulse[wheel] != btScalar(0.))
{
if (m_wheelInfo[wheel].m_skidInfo < btScalar(1.))
{
m_forwardImpulse[wheel] *= m_wheelInfo[wheel].m_skidInfo;
m_sideImpulse[wheel] *= m_wheelInfo[wheel].m_skidInfo;
}
}
}
}
// apply the impulses
{
for (int wheel = 0; wheel < getNumWheels(); wheel++)
{
btWheelInfo& wheelInfo = m_wheelInfo[wheel];
btVector3 rel_pos =
wheelInfo.m_raycastInfo.m_contactPointWS - m_chassisBody->getCenterOfMassPosition();
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;
btVector3 rel_pos2 = wheelInfo.m_raycastInfo.m_contactPointWS -
groundObject->getCenterOfMassPosition();
btVector3 sideImp = m_axle[wheel] * m_sideImpulse[wheel];
#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));
#else
rel_pos[m_indexUpAxis] *= wheelInfo.m_rollInfluence;
#endif
m_chassisBody->applyImpulse(sideImp, rel_pos);
// apply friction impulse on the ground
groundObject->applyImpulse(-sideImp, rel_pos2);
}
}
}
}
PhysicsVehicle::PhysicsVehicle(PhysicsWorld& physicsWorld, PhysicsVehicleTuning* newVehicleTuning)
: ownerWorld(physicsWorld), vehicleTuning(newVehicleTuning)
{
@ -92,7 +288,7 @@ PhysicsVehicle::PhysicsVehicle(PhysicsWorld& physicsWorld, PhysicsVehicleTuning*
}
vehicleRayCaster = new btDefaultVehicleRaycaster(physicsWorld.world);
vehicle = new btRaycastVehicle(suspensionAndFrictionTuning, carChassis, vehicleRayCaster);
vehicle = new CustomRaycastVehicle(suspensionAndFrictionTuning, carChassis, vehicleRayCaster);
physicsWorld.world->addVehicle(vehicle);
@ -117,20 +313,23 @@ PhysicsVehicle::PhysicsVehicle(PhysicsWorld& physicsWorld, PhysicsVehicleTuning*
DebugDraw::addLine(chassisOrigin, connectionPoint + chassisOrigin, Color::Green,
Color::Blue, 400.f);
// Axle (normal)
DebugDraw::addLine(chassisOrigin, BulletVectorToGlmVec3(vehicleTuning->wheelAxleCS) + chassisOrigin,
DebugDraw::addLine(chassisOrigin,
BulletVectorToGlmVec3(vehicleTuning->wheelAxleCS) + chassisOrigin,
Color::Orange, Color::Red, 400.f);
// Wheel radius
DebugDraw::addLine(connectionPoint,
connectionPoint + chassisOrigin + glm::vec3(0.f, -vehicleTuning->wheelRadius, 0.f),
Color::Green, Color::Red, 400.f);
DebugDraw::addLine(
connectionPoint,
connectionPoint + chassisOrigin + glm::vec3(0.f, -vehicleTuning->wheelRadius, 0.f),
Color::Green, Color::Red, 400.f);
// Wheel direction (normal) (normally -UpAxis)
DebugDraw::addLine(chassisOrigin,
chassisOrigin + BulletVectorToGlmVec3(vehicleTuning->wheelDirectionCS0),
Color::Green, Color::Red, 400.f);
DebugDraw::addLine(
chassisOrigin,
chassisOrigin + BulletVectorToGlmVec3(vehicleTuning->wheelDirectionCS0),
Color::Green, Color::Red, 400.f);
// Connection height (offset)
DebugDraw::addLine(chassisOrigin + glm::vec3(1.f, 0.f, 0.f),
chassisOrigin + glm::vec3(1.f, vehicleTuning->connectionHeight, 0.f), Color::Blue,
Color::Orange, 400.f);
chassisOrigin + glm::vec3(1.f, vehicleTuning->connectionHeight, 0.f),
Color::Blue, Color::Orange, 400.f);
// Example raycast
glm::vec3 wheelConnectionWithOffset =
@ -374,7 +573,7 @@ void PhysicsVehicle::Update(float deltaTime)
}
// LOGV << "Input throttle: " << ThrottlePercent << " Gear: " << SelectedGear
// << " output force: " << engineForce;
// << " output force: " << engineForce;
// Apply forces
// Rear-wheel drive


+ 14
- 3
src/PhysicsVehicle.hpp View File

@ -49,7 +49,8 @@ struct PhysicsVehicleTuning
// float wheelFriction = 1000; // BT_LARGE_FLOAT;
// Pretty slidey here, with some good characteristics (reverse 180s are fun)
// float wheelFriction = 1.5f; // BT_LARGE_FLOAT;
float wheelFriction = 5.f; // BT_LARGE_FLOAT;
// float wheelFriction = 5.f; // BT_LARGE_FLOAT;
float wheelFriction = 0.5f; // BT_LARGE_FLOAT;
// Not really necessary to customize these, unless you're making something weird
// These need to be normalized, otherwise they scale the wheels
@ -64,7 +65,7 @@ struct PhysicsVehicleTuning
float suspensionStiffness = 50.f; // 20.f;
// Damping removes energy from the springs, eliminating bounce
// Each update, remove this much force from the suspension, depending on force sign
float suspensionDampingRelaxation = 7.f; // 2.3f;
float suspensionDampingRelaxation = 7.f; // 2.3f;
float suspensionDampingCompression = 10.f; // 4.4f;
// This resists rolling for cars with high centers of gravity. Not sure how it works yet
// See
@ -81,6 +82,16 @@ struct PhysicsVehicleTuning
float defaultBrakingForce = 20.f;
};
class CustomRaycastVehicle : public btRaycastVehicle
{
public:
CustomRaycastVehicle(const btVehicleTuning& tuning, btRigidBody* chassis,
btVehicleRaycaster* raycaster);
// We need to make friction a factor of velocity
virtual void updateFriction(btScalar timeStep) override;
};
// TODO: Add destructor
class PhysicsVehicle
{
@ -105,7 +116,7 @@ public:
// Directly tie throttle percent to max possible force output
bool simpleDrivetrain = false;
btRaycastVehicle* vehicle;
CustomRaycastVehicle* vehicle;
// Note that these cache the last position since Update() was called. This is because the
// btMotionState interpolation will happen for each call to the chassis GetTransform, which


Loading…
Cancel
Save