function problem

hi i am new in functions and i need to do that a circle can be moving on the screen at a speed that a person will choose and make it disappear in the edges of the windows this is the guide:

Moving the Cannonball

The cannonball will move due to principles of basic physics. The cannonball is referred to as a projectile and its motion is usually named a trajectory. The trajectory will depend on the angle of the cannon’s barrel. The equations of vertical/horizontal projectile trajectories are illustrated below:


Where g is gravity taken to be 32.2 feet/sec2.

We’re especially interested in the equations relating x and y as these represent the equations for the x/y coordinates of the cannonball as it moves across our screen.

An example C++ implementation of these equations are shown below:

t_val += .3; //Initialized to 0 and incremented .3 secs/iteration
x_dis = vox*t_val;
y_dis = voy*t_val - (.5*G*(t_val*t_val));


Time (t_val in equation above) is a completely arbitrary input value in our simulation. Since we are computing a new trajectory point for each iteration, we use t_val to represent the elapsed time between plotted points of the projectile’s trajectory. Smaller values (< 0.3) will generate more points. Larger values (> 0.3) will generate less points. You can think of time as providing a sampling of the trajectory path. Since our x/y distances are measured in pixels, 0.3 seconds works well for a screen resolution of 640x480 and provides an adequate number of points for the trajectory.

G represents gravity and is 32.2ft/sec2.

The user enters a velocity for the cannonball. Since the barrel is positioned at an angle, the velocity is resolved into 2 components - horizontal and vertical as show below:

The variable vox represents the velocity component in the horizontal direction and voy represents the velocity component in the vertical direction. These 2 components are computed as follows:

vox = cos((90-angle)*PI/180.0)*velocity;
voy = sin( (90-angle)*PI/180.0)*velocity;


Where angle is the angle entered by the user and is used for positioning the barrel, and velo city is the velocity entered by the user.

The x_dis and y_dis variables are computed in a loop. The cannonball will be moved using the moveObject function based on the x_dis and y_dis values relative to the initial position of the cannonball.

Hint: The projectile always moves in the positive x-direction, so x_dis should be added to endpointx. The projectile initially moves in the negative y-directions and later in the positive y direction. Always subtracting y_dis from endpointy handles both possibilities. (Try to prove to yourself why this is so). Example call of the moveObject function for the cannonball is as follow:

moveObject(cannon_ball_obj,(endpointx)+x_dis,(endpointy)-y_dis);

Displaying the Trajectory

The trajectory should be displayed using the ‘-‘ (dash) symbol. It must follow the cannonball.

no this is my code so far:::


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
#include <iostream>
#include <cmath>
#include "graph1.h"

using namespace std;

//function prototype
double computeLength(int x1, int y1, int x2, int y2);
void rotateLine(int obj_no, int x1, int y1, double length);
bool moveCannon(int obj_no, int new_x, int new_y);

int main()
{
	//variables initialization/declaration
	int obj_no = 0;
	int x1 = 0;
	int y1 = 0;
	double length = 0;
	double angle = 0.0;
	int new_x = 0;
	int new_y = 0;
	int x2 = 0;
	int y2 = 0;
	int velocity = 0;
	char repeat = 'y';
	bool finish = false;

	//displayGraphics
	displayGraphics();

	do
	{

		displayBMP("cannon_base1.bmp", 0, 454);

		//draw barrel
		obj_no = drawLine(26, 464, 26, 427, 5);

		//Compute length of line
		length = computeLength(26, 464, 26, 427);

		//Rotate the line
		rotateLine(obj_no, 26, 464, length);

		//Ask user to repeat
		cout << "Repeat Program? (y/n) ";
		cin >> repeat;
 
		//Clear the graphics
		clearGraphics();

	}while ( (repeat == 'y') || (repeat == 'Y') );

  return 0;
}

void rotateLine(int obj_no, int x1, int y1, double length)
{
  //Rotate one degree cw if left arrow
  //Rotate one degree ccw if right arrow
  //Press up arrow to quit
  const double PI = 3.1415926;
  int i = 0;
  int new_x = 0;
  int new_y = 0;
  double angle = 0;

		cout << "Enter Angle For Cannon <Between 0 and 90 inclusive>: ";
		cin >> angle;

		 new_x = (cos( (90.0-angle)*PI/180)*length)+26;
		 new_y = (cos( (90.0-angle)*PI/180)*40)+427;

		//Rotate the line
		moveObject(obj_no, new_x, new_y);
		drawCircle(5, new_x+3, new_y);
  do
  {

    if(right() )
    {
	  angle = 90 - (i);
      //Compute x/y coordinate for clock's hand
      new_x = cos(angle*PI/180.0)*length;
      new_y = sin(angle*PI/180.0)*length;

      //Rotate cw
      moveObject(obj_no,x1+new_x,y1-new_y);
      

      i++;
    }
    else if (left())
    {
      //Rotate ccw
      angle = 90 - (i);

      //Compute x/y coordinate for clock's hand
      new_x = cos(angle*PI/180.0)*length;
      new_y = sin(angle*PI/180.0)*length;

      //obj_no = drawLine(x1,y1,x2+new_x,y1-new_y,1);
      
      moveObject(obj_no,x1+new_x,y1-new_y);
      
      i--;
    }
    else if (up())
    {
      break;
    }


  }while(true);
}

//Compute length of line
double computeLength(int x1,int y1,int x2,int y2)
{
  double len = 0.0;

  //Compute length
  len = sqrt( pow(x1-x2,2.0) + pow(y1-y2,2.0) );

  return (len);
}


so now i am trying to make a functions that allow me to after input the velocity it will shoot the cannonball but i do not know how to do it can somebody help me thank you

It seems that whoever gave you this assignment gave you the implementations of the mechanics of a cannonball (basic at best, but...), so what are you having trouble with, exactly?

-Albatross
ok my problem is that i do not know how to make like the statement, i know i have to do a for loop to be able to be repeating the ball to be moving until it touch the screen edges but i do not have any clue about where to put it or if i need to create or call a new function because this is the first program that i do with functions, so if you can give me a hint i was thinking in put it inside the rotateobject function after a while true, but i do not really know, i understand the formulas and everything but i do not know how to input that into my program so i really need help i have been thinking about this program for 2 days and i do not know how to finish the program thank you
Topic archived. No new replies allowed.