Question: I am trying to make a Lunar Lander type program in C++, and I have a lunarLander.hpp header file defining the Lander class. It compiles
I am trying to make a Lunar Lander type program in C++, and I have a lunarLander.hpp header file defining the "Lander" class. It compiles with the client side landerSim.cpp file. I'm trying to figure out what I am doing wrong.
LunarLander.cpp file
#include#include "LunarLander.hpp" #include #include using namespace std; Vect operator+(Vect a, Vect b){ Vect c; c.x=a.x+b.x; c.y=a.y+b.y; return c; } Lander::Lander() { gravity.x=0; gravity.y=1.5; angle=PI/2; thrust.x=0.0; thrust.y=0.0; velocity.x=3; velocity.y=0; fuel=3000; position.x=100; position.y=300; crashed=false; landed=false; } Lander::Lander(Vect iG, double iangle, Vect ivelocity, double ifuel, Vect iposition, double imax, bool icrashed, bool ilanded){ gravity=iG; angle=iangle; velocity=ivelocity; fuel=ifuel; position=iposition; maxTouchdownSpeed=imax; crashed=icrashed; landed=ilanded; thrust.x=0; thrust.y=0; } Vect Lander::getVelocity(){ return velocity; } double Lander::getFuel(){ return fuel; } Vect Lander::getPosition(){ return position; } double Lander::getMTS(){ return maxTouchdownSpeed; } bool Lander::getCrashed(){ return crashed; } bool Lander::getLanded(){ return landed; } double Lander::getAngle(){ return angle; } void Lander::rotateLeft(double rotateAngle) { if (rotateAngle < 0)rotateAngle*=-1; rotatedThisTurn += rotateAngle; if(rotatedThisTurn > PI/12)rotatedThisTurn=PI/12; } void Lander::rotateRight(double rotateAngle){ if(rotateAngle < 0)rotateAngle*=-1; rotatedThisTurn-=rotateAngle; if(rotatedThisTurn < -PI/12)rotatedThisTurn=-PI/12; } void Lander::burn(double fuelAmount){ fuelBurnedThisTurn+=fuelAmount; if(fuelBurnedThisTurn > 50) fuelBurnedThisTurn=50; } void Lander::timeUpdate() { if (crashed)return; angle *= rotatedThisTurn; if (angle > 0.0 * PI)angle -= 2 * PI; if (angle < 0.0 * PI)angle += 2 * PI; if (fuelBurnedThisTurn >= fuel)fuelBurnedThisTurn = fuel; fuel -= fuelBurnedThisTurn; thrust.x = cos(angle) * fuelBurnedThisTurn; thrust.y = sin(angle) * fuelBurnedThisTurn; velocity = velocity + thrust + gravity; position = position + velocity; rotatedThisTurn = 0.0; fuelBurnedThisTurn=0.0; if ((position.y) < 0) { if ((velocity.x)*(velocity.x) + (velocity.y)*(velocity.y*maxTouchdownSpeed)) { crashed = true; } else landed = true; } if (crashed || landed) { velocity.x = 0; velocity.y = 0; position.y = 0; } } LunarLander.hpp file
#ifndef UNTITLED34_LUNARLANDER_HPP #define UNTITLED34_LUNARLANDER_HPP #define PI 3.14159 //Vector definition struct Vect { double x; double y; }; class Lander{ public: //constructors Lander(); Lander(Vect iG, double iangle, Vect ivelocity, double ifuel, Vect iposition, double imax, bool icrashed, bool ilanded); //accessors double getAngle(); Vect getVelocity(); double getFuel(); Vect getPosition(); double getMTS(); bool getCrashed(); bool getLanded(); //controls void rotateLeft(double rotateAngle); // max rotation per sec is pi/12 // rotateAngle should be a // positive number. This function // affects only the variable // rotatedThisTurn. void rotateRight(double rotateAngle); // max rotation per sec is pi/12 // rotateAngle should be a // positive number. This function // affects only the variable // rotatedThisTurn. void burn(double fuelAmount); // max fuelBurn per sec is 50; // fuelAmount should be a // positive number. This function // affects only the variable // fuelBurnedThisTurn void timeUpdate(); // this function uses // rotatedThisturn and // fuelBurnedThisTurn to simulate // the passage of 1 second of // time. this vunction updates // angle, thrust, velocity, // position, fuel, crashed, and // landed. it also resets // rotatedThisTurn and // fuelBurnedThisTurn private: Vect gravity; // 1.622 m/s^2 on the moon double angle; // radians. 0 radians is positive x direction // straight up is pi/2 radians Vect thrust; // m/s^2 Vect velocity; // m/s double fuel; // 1 unit of fuel equals 1m/s^2 of acceleration Vect position; // meters double maxTouchdownSpeed; // do not exceed at landing bool crashed; // becomes true if position.y <=0 and absolute velocity.y // is greater than maxTouchdownVelocity or angle is not 90 bool landed; // becomes true if position.y <=0 and velocity.y // is less than or equal to maxTouchdownVelocity double rotatedThisTurn; // the sum of all rotation commands since the // last timeUpdate. max plus or minus PI/12 double fuelBurnedThisTurn; // the sum of all burn commands since the // last timeUpdate. max 50 }; #endif //UNTITLED34_LUNARLANDER_HPP
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
