diff --git a/Control/controller.cpp b/Control/controller.cpp index 47d5648..67580bf 100644 --- a/Control/controller.cpp +++ b/Control/controller.cpp @@ -32,8 +32,6 @@ void EgoCarController::handleKeyboardInput() { } else if (keyStates_["Down"]) { car_.accelerate(3.0, -1); } else if (keyStates_["space"]) { - car_.accelerate(-15.0, 1); - } else { car_.accelerate(0.0, 1); } diff --git a/Objects/car.cpp b/Objects/car.cpp index f2c0826..183c131 100644 --- a/Objects/car.cpp +++ b/Objects/car.cpp @@ -15,7 +15,9 @@ Car::Car() steering(0), rollingInstance(0), frontCenterDistance(0), control_index(0), sinNegAngle(0), - cosNegAngle(0) {} + cosNegAngle(0), + air_resistance(0.01), + tire_friction(0.1) {} Car::Car( Vect3 setPosition, Vect3 setDimensions, Color setColor, @@ -33,6 +35,8 @@ Car::Car( velocity = 0; control_index = 0; rollingInstance = 0.5; + air_resistance = 0.01; + tire_friction = 0.1; } void Car::renderBottom(pcl::visualization::PCLVisualizer::Ptr& viewer) const { @@ -101,7 +105,6 @@ void Car::renderWindshields(pcl::visualization::PCLVisualizer::Ptr &viewer) cons } } - void Car::render(pcl::visualization::PCLVisualizer::Ptr& viewer) const { renderBottom(viewer); renderTop(viewer); @@ -109,13 +112,12 @@ void Car::render(pcl::visualization::PCLVisualizer::Ptr& viewer) const { renderWindshields(viewer); } - void Car::accelerate(float acc, int dir) { - acceleration = acc * dir; + acceleration = acc * dir - air_resistance * velocity * abs(velocity) - tire_friction * sign(velocity); } void Car::steer(float s) { - steering = s; + steering = s / (1 + abs(velocity)); } void Car::control(const std::vector& c) { @@ -139,7 +141,6 @@ void Car::move(float dt, int time_us) { orientation = Eigen::Quaternionf(Eigen::AngleAxisf(angle, Eigen::Vector3f::UnitZ())); velocity += acceleration * dt; - // Apply rolling instance if there is no acceleration input if (acceleration == 0) { if (velocity > 0) { velocity -= rollingInstance; diff --git a/Objects/car.hpp b/Objects/car.hpp index 25da33c..b993772 100644 --- a/Objects/car.hpp +++ b/Objects/car.hpp @@ -67,6 +67,8 @@ class Car { int control_index; double sinNegAngle; double cosNegAngle; + float air_resistance; + float tire_friction; public: Car(); Car(Vect3 setPosition, Vect3 setDimensions, Color setColor, float setAngle, @@ -94,4 +96,8 @@ class Car { pcl::ModelCoefficients generateWheelCoefficients(int i, int j) const; }; +inline int sign(float x) { + return (x > 0) - (x < 0); +} + #endif // NFS_CAR_HPP diff --git a/main.cpp b/main.cpp index b438e23..7b91071 100644 --- a/main.cpp +++ b/main.cpp @@ -11,7 +11,6 @@ int main() { viewer->setBackgroundColor(0, 0, 0); viewer->addCoordinateSystem(1.0); viewer->initCameraParameters(); - int* screenSize = viewer->getRenderWindow()->GetScreenSize(); viewer->setSize(screenSize[0], screenSize[1]);