adding thrust and gravity to a circle

this is a assignment that i have been unable to complete, and i need to finish it so i can move on to the next assignment. any tips would be greatly appreciated!

this is done using Visual Studio 2010: C++
I am using a book that comes with some header files that allow me to use a graphics window

For this assignment i needed to write up 3 functions that will make a circle move based on the users input, the user is supposed to be inputting a horizontal left and right thrust, and a vertical thrust:

A function that returns a new position based on the current position, velocity, acceleration, and time,

A function that returns a new velocity based on the current velocity, acceleration, and time, and

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.

here are the equations given to us by the professor to calculate the control algorithm:
To calculate the position you will use the equation:
pos_new = pos_initial + t * vel_initial + 0.5 * t * t * acceleration
To calculate the velocity you will use the equation:
v_new = v_initial + acceleration * t
For all equations above, "t" is the current time step value

The problem with my code is that after the user input the circle comes out but it does not move.
i believe the issue is with either the move function or with the position_from_vel function that is supposed to be calculating the new position.

here is the code:
////////////////////////////////////////////////////////////////////////////////
#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
*/
double position_from_vel(double init, double vel, double ACC, double t) // intial position, initial velocity, acceleration, and time all passed in
{
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;
}
For the benefit of anyone who may come across this thread.
I'm reposting your code with codetags. I haven't made any changes to it.

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;
} 


Last edited on
Thank you. Any chance you can tel me how to do that? or give me a direction to where it is explained because i cant find anything on code tags on this website. i remember seeing it somewhere but cant seem to find it again.
See the format word next to when you're posting? Should be located to the right (bottom on an original post). Click on the <> and bam, code tags.
yeah... just had to look while posting.
Topic archived. No new replies allowed.