1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
#include "ccc_win.h"
#include <string>
#include <Windows.h>
#include<cmath>
#include "ccc_time.cpp"
// global variables used in various locations
//int horiz_l_thrust;
//int horiz_r_thrust;
//int vert_thrust;
//double horizontal;
double t;
double init = 0;
double vel = 1;
double ACC = 2;
/*
The values calculated in the functions will be used in a control
algorithm. The control algorithm simulates a body falling under the
force of gravity, and with the capacity to
*/
/*
A function that returns a new velocity based on the current velocity,
acceleration, and time.
@param init is the initial position
@param vel is the velocity
@param ACC is the acceleration
@param t is the curren time step value
@return pos_new is the new position that the function is returning
*/
// intial position, initial velocity, acceleration, and time all passed in
double position_from_vel(double init, double vel, double ACC, double t)
{
double pos_new = init + (vel * t) + 0.5 * (ACC * t * t);
return pos_new; // need to calculate a new position that is then transferred to the move function
}
/*
A function that returns a new position based on the current position,
velocity, aceleration, and time.
*** FIRST: MAKE THE CIRCLE MOVE AND RETURN A NEW POSITION.
@param center point of circle that will be moved
*/
Circle c;
Point move(Point position)
{
double x = position_from_vel(init, vel, ACC, t);
double y = position_from_vel(init, vel, ACC, t);
Point pos_new(x, y);
return pos_new;
}
/*
A function that assigns a vertical, and horizontal acceleration value
based on the user's input. Since the last function is assigning two
values, it should return void, and assign the two values to reference
variables in the parameter list that is passed to it.
@param vert_thrust is user input for the vert thrust of the ship
@param horizontal the right and left horizontal thrust from user input
*/
void accel(string& vert_thrust, double& horizontal)
{
Point k(0,0);
if(vert_thrust == "h" && horizontal == -0.5)
c.move(-0.25, 0.15);
else if(vert_thrust == "h" && horizontal == 0)
c.move(0, 0.15);
else if(vert_thrust == "h" && horizontal == -1)
c.move(-0.5, 0.15);
else if(vert_thrust == "h" && horizontal == 0.5)
c.move(0.25, 0.15);
else if(vert_thrust == "h" && horizontal == 1)
c.move(0.5, 0.15);
else if(vert_thrust == "l" && horizontal == -0.5)
c.move(-0.25, -0.0981);
else if(vert_thrust == "l" && horizontal == 0)
c.move(0, -0.0981);
else if(vert_thrust == "l" && horizontal == -1)
c.move(-0.5, -0.0981);
else if(vert_thrust == "l" && horizontal == 0.5)
c.move(0.25, -0.0981);
else if(vert_thrust == "l" && horizontal == 1)
c.move(0.5, -0.0981);
else if(vert_thrust == "n" && horizontal == -0.5)
c.move(-0.25, -0.981);
else if(vert_thrust == "n" && horizontal == 0)
c.move(0, -0.981);
else if(vert_thrust == "n" && horizontal == -1)
c.move(-0.5, -0.981);
else if(vert_thrust == "n" && horizontal == 0.5)
c.move(0.25, -0.981);
else if(vert_thrust == "n" && horizontal == 1)
c.move(0.5, -0.981);
else
Message error(k,"error in accel function");
}
int ccc_win_main()
{
// user input of vertical main thrust
Point p5(-10, 9);
Message m(p5, "Vertical/Main Thruster(under the circle, pushing circle up: ");
Point p6(-10, 8);
Message m2(p6, "(l)ow = l");
Point p7(-10, 7);
Message m3(p7, "(h)igh = h");
Point p8(-10, 6);
Message m4(p8, "(n)one = n");
cwin << m << m2 << m3 << m4;
string vert_thrust = cwin.get_string("Choose your vertical thrust(l, n, h): ");
cwin.clear();
// user input of Horizontal Right thrust
Point p9(-10, 9);
Message m5(p9, "Horizontal Right Thruster(on right side of circle, pushing circle left): ");
Point p10(-10, 8);
Message m6(p10, "(l)ow = l ");
Point p11(-10, 7);
Message m7(p11, "(h)igh = h ");
Point p12(-10, 6);
Message m8(p12, "(n)one = n ");
cwin << m5 << m6 << m7 << m8;
string horiz_r_thrust = cwin.get_string("Choose your Horizontal Right thrust(l, n, h): ");
cwin.clear();
Point k(0,0);
double horizontal_l_thrust;
double horizontal_r_thrust;
if(horiz_r_thrust == "l")
horizontal_r_thrust = -0.5;
else if(horiz_r_thrust == "h")
horizontal_r_thrust = -1;
else if(horiz_r_thrust == "n")
horizontal_r_thrust = 0;
else
Message error(k,"Choose a horizontal left thrust of l, n, h.");
//user input of Horizontal Left thrust
Point p(-10, 9);
Message m9(p, "Horizontal Left Thruster(on left side of circle, pushing circle right): ");
Point p2(-10, 8);
Message m10(p2, "(l)ow = l ");
Point p3(-10, 7);
Message m11(p3, "(h)igh = h ");
Point p4(-10, 6);
Message m12(p4, "(n)one = n ");
cwin << m9 << m10 << m11 << m12;
string horiz_l_thrust = cwin.get_string("Choose your horizontal left thrust(l, n, h): ");
cwin.clear();
if(horiz_l_thrust == "l")
horizontal_l_thrust = 0.5;
else if(horiz_l_thrust == "h")
horizontal_l_thrust = 1;
else if(horiz_l_thrust == "n")
horizontal_l_thrust = 0;
else
Message error(k,"Choose a horizontal left thrust of l, n, h.");
double horizontal = horizontal_l_thrust + horizontal_r_thrust;
// passing these values to the acceleration function
Point a(-9, 0);
Circle c(a, 1);
Point end(10,0);
double x_end = end.get_x();
Point butt = c.get_center();
double moving_butt = butt.get_x();
while(moving_butt < x_end)
{
cwin.clear();
Time now;
double t = now.get_seconds();
accel(vert_thrust, horizontal);
position_from_vel(init, vel, ACC, t);
Point butt = c.get_center();
cwin << c;
move(butt);
moving_butt = butt.get_x();
}
return 0;
}
|