Browse Source

Hacked in basic tire model

It only looks at slip angle (or my bastardization of slip angle) to
reduce traction.
The current model does not work in reverse.
ogre-port2
Macoy Madson 7 months ago
parent
commit
8bf22d6c6e
4 changed files with 71 additions and 7 deletions
  1. +4
    -0
      docs/VehicleTuning.org
  2. +2
    -1
      src/Main.cpp
  3. +58
    -4
      src/PhysicsVehicle.cpp
  4. +7
    -2
      src/PhysicsVehicle.hpp

+ 4
- 0
docs/VehicleTuning.org View File

@ -137,8 +137,12 @@ 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.
Khan, James. [[https://github.com/jayfella/jme-vehicles][JME Vehicles]]. Retrieved 2020-07-25.
Marco Monster (2003). /[[https://web.archive.org/web/20040604214333fw_/http://home.planet.nl/~MONSTROUS/tutcar.html][Car Physics for Games]]/. Retrieved 2020-03-15.
Racer.nl. [[racer.nl/reference/pacejka.htm][Pacejka's Magic Formula]]. Retrieved 2020-07-25.
[[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.

+ 2
- 1
src/Main.cpp View File

@ -293,6 +293,7 @@ int main()
// World/meshes
PhysicsWorld physicsWorld;
PhysicsVehicle* vehicle = GameVehicles::CreateVehicle(physicsWorld, glm::mat4(1.f));
vehicle->debugOutput = true;
PhysicsVehicle* otherVehicle = GameVehicles::CreateVehicle(
physicsWorld, glm::translate(glm::mat4(1.f), glm::vec3(10.f, 0.f, 0.f)));
Graphics::Object worldRender;
@ -425,7 +426,7 @@ int main()
// Vehicle updates
{
// Ricky Suicide
// AI driver
if (!twoPlayer)
{
glm::vec3 targetPosition =


+ 58
- 4
src/PhysicsVehicle.cpp View File

@ -11,9 +11,10 @@
#include "Utilities.hpp"
#include <glm/ext/matrix_transform.hpp>
#include <glm/mat4x4.hpp> // mat4
#include <glm/trigonometric.hpp> //radians
#include <glm/vec3.hpp> // vec3
#include <glm/gtx/vector_angle.hpp> // angle
#include <glm/mat4x4.hpp> // mat4
#include <glm/trigonometric.hpp> //radians
#include <glm/vec3.hpp> // vec3
#include "BulletDynamics/ConstraintSolver/btContactConstraint.h"
#include "BulletDynamics/Vehicle/btWheelInfo.h"
@ -30,7 +31,7 @@ std::vector<PhysicsVehicle*> g_vehicles;
// Vehicle implementation
//
static btScalar sideFrictionStiffness2 = btScalar(1.0);
static btScalar sideFrictionStiffness2 = btScalar(1.0f);
CustomRaycastVehicle::CustomRaycastVehicle(const btVehicleTuning& tuning, btRigidBody* chassis,
btVehicleRaycaster* raycaster)
@ -41,6 +42,8 @@ CustomRaycastVehicle::CustomRaycastVehicle(const btVehicleTuning& tuning, btRigi
// We need to make friction a factor of velocity
void CustomRaycastVehicle::updateFriction(btScalar timeStep)
{
bool hackySlide = false;
// calculate the impulse, so that the wheels don't move sidewards
int numWheel = getNumWheels();
if (!numWheel)
@ -102,6 +105,12 @@ void CustomRaycastVehicle::updateFriction(btScalar timeStep)
btScalar sideFactor = btScalar(1.);
btScalar fwdFactor = 0.5;
glm::vec3 vehicleNormalVelocity =
glm::normalize(BulletVectorToGlmVec3(m_chassisBody->getLinearVelocity()));
glm::vec3 chassisOrigin = BulletVectorToGlmVec3(m_chassisBody->getCenterOfMassPosition());
DebugDraw::addLine(chassisOrigin, vehicleNormalVelocity + chassisOrigin, Color::Blue,
Color::Purple, 0.f);
bool sliding = false;
{
for (int wheel = 0; wheel < getNumWheels(); wheel++)
@ -153,8 +162,49 @@ void CustomRaycastVehicle::updateFriction(btScalar timeStep)
btScalar x = (m_forwardImpulse[wheel]) * fwdFactor;
btScalar y = (m_sideImpulse[wheel]) * sideFactor;
if (hackySlide)
{
btScalar impulseAbs =
m_sideImpulse[wheel] < 0.f ? -m_sideImpulse[wheel] : m_sideImpulse[wheel];
if (impulseAbs > 200.f)
{
maximpSquared = 0.f;
maximp = 0.f;
if (debugOutput)
LOGV << "[" << wheel << "] " << y << " side impulse";
}
}
btScalar impulseSquared = (x * x + y * y);
glm::mat4 wheelWSTransform = BulletTransformToGlmMat4(getWheelTransformWS(wheel));
glm::vec3 wheelNormalDirection = RotateGlmVec3ByMat4(wheelWSTransform, ForwardAxis);
glm::mat4 chassisTransform =
BulletTransformToGlmMat4(m_chassisBody->getCenterOfMassTransform());
glm::vec3 chassisDirection = RotateGlmVec3ByMat4(chassisTransform, ForwardAxis);
// TODO: front tires need their rotation added
float slipAngle = glm::angle(vehicleNormalVelocity, chassisDirection);
isSliding = slipAngle > glm::radians(4.f);
if (isSliding)
m_wheelInfo[wheel].m_skidInfo = 0.4f;
if (debugOutput && isSliding)
{
// glm::vec3 wheelOrigin = BulletVectorToGlmVec3(localTransform.getOrigin());
const btTransform& wheelTrans = getWheelTransformWS(wheel);
glm::vec3 wheelPosWorldSpace = BulletVectorToGlmVec3(wheelTrans.getOrigin());
// TODO Use actual ground offset
glm::vec3 offsetToGround =
RotateGlmVec3ByMat4(chassisTransform, glm::vec3(0.f, -0.6209f, 0.f));
DebugDraw::addLine(offsetToGround + wheelPosWorldSpace,
(wheelPosWorldSpace + chassisDirection) + offsetToGround,
Color::Red, Color::Red, 10.f);
// The fun version
// DebugDraw::addLine(chassisOrigin, wheelNormalDirection + chassisOrigin,
// Color::Yellow, Color::Purple, 10.f);
LOGV << "[" << wheel << "] " << slipAngle << " slip";
}
if (impulseSquared > maximpSquared)
{
sliding = true;
@ -177,6 +227,8 @@ void CustomRaycastVehicle::updateFriction(btScalar timeStep)
{
m_forwardImpulse[wheel] *= m_wheelInfo[wheel].m_skidInfo;
m_sideImpulse[wheel] *= m_wheelInfo[wheel].m_skidInfo;
if (debugOutput)
LOGV << "[" << wheel << "] " << m_sideImpulse[wheel] << " side impulse";
}
}
}
@ -551,6 +603,8 @@ float PhysicsVehicle::EngineForceFromThrottle(float deltaTime, float throttlePer
void PhysicsVehicle::Update(float deltaTime)
{
vehicle->debugOutput = debugOutput;
// Engine force
float engineForce = 0.f;
{


+ 7
- 2
src/PhysicsVehicle.hpp View File

@ -49,8 +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 = 0.5f; // 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
@ -85,6 +85,9 @@ struct PhysicsVehicleTuning
class CustomRaycastVehicle : public btRaycastVehicle
{
public:
bool debugOutput = false;
bool isSliding = false;
CustomRaycastVehicle(const btVehicleTuning& tuning, btRigidBody* chassis,
btVehicleRaycaster* raycaster);
@ -102,6 +105,8 @@ public:
void Reset();
bool debugOutput = false;
// Controls
// Percent means 0.f through 1.f
float ThrottlePercent = 0.f;


Loading…
Cancel
Save