Skip to content

Commit

Permalink
advanced physics (began WSL integration)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiMoraru123 committed Jun 19, 2023
1 parent 3d07ea1 commit c0b391c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
2 changes: 0 additions & 2 deletions Control/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
13 changes: 7 additions & 6 deletions Objects/car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -101,21 +105,19 @@ void Car::renderWindshields(pcl::visualization::PCLVisualizer::Ptr &viewer) cons
}
}


void Car::render(pcl::visualization::PCLVisualizer::Ptr& viewer) const {
renderBottom(viewer);
renderTop(viewer);
renderWheels(viewer);
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<Control>& c) {
Expand All @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions Objects/car.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
1 change: 0 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down

0 comments on commit c0b391c

Please sign in to comment.