Need help from u all
am a newbie here!!!

making a 2D project, snap link is given below

here i have to blast the ship which is moving downward on x-axis
with a cannon ball ,on the mountain
the function i am using for projectile trajectory is

----------------------------------------------------

void ball(int xx,int yy)
{
glRasterPos2i(xx,yy);
pix[7].mDraw();
glutSwapBuffers();

}

void ballMove(void)
{
calculateY(xx);
}

void calculateY(int xx)
{

yy= (xx*tan(angle*3.14/180))-(g*pow((float)xx, 2))/(2*(pow(u, 2))*(pow(cos(angle*3.14/180), 2)));
glRasterPos2i(xx+50,yy+520);
pix[7].mDraw();

if(xx>PosX-90 && xx<PosX+90 && yy>PosY-80 && yy<PosY+80)
{
glRasterPos2i(xx+50,yy+520);
pix[10].mDraw();
position=true;
}

glutSwapBuffers();
glutTimerFunc(10, calculateY , xx+5);

}

void pressKey(unsigned char key, int abc, int xyz)
{
switch(key)
{
case 'a':
{
if (state==1)
{
u=40;
angle=85;
ballMove();
break;
}.....

------------------------------

when i press 'a', ball starts to follow projectile trajectory and hit the ship

kindly help me out

Problems are :

1) i dont want to use glutSwapBuffers(); other than display function

it is possible?

2)if i keep the angle about 80, speed of ball is fast,but if i changed the angle to 30,speed of ball is too much slow

3) which equation should i use to calculate projectile ??

waiting for positive response

## All 3 Replies

You should newtonion equation of motion.

In all your code might look something like so :

``````Cannon cannon;

if(keyLeftPressed){
cannon.decreaseAngle();
}
else if(keyRightPressed){
cannon.increaseAngle();
}

Vector initVelocity(5,5);

if(keySpaceBarPressed){
cannon.shoot( initVelocity() );
}

//...
void Cannon::shoot(const Vector& initVelocity, int dt = 1){
//x = initVelocity *dt + 1/2*acceleration * dt
ball.updatePosition( initVelocity * dt + 1/2 * Constant::gravity * dt);
}``````

Haven't touched this stuff for a while, so no guarantees. I'm sure someone will come and fix something.

I am just learning C++ and will have to write the same exercise when I get to the chapter on graphics. I wrote similar non-graphics programs when I was learning BASIC years (decades rather) ago.

I follow and understand what FirstPerson wrote much more than what the OP wrote.

I can't tell if the angles the OP has are measured in radians or degrees. He quotes 80 in his question so that must be degrees. Does C++ let you choose which?

I can't find a passage of time in the OP's code. Is it 'glutTimerFunc'? FP has time passing at a rate of 1 second per iteration (dt = 1).

The OP says that the target is a moving ship. I can't find any code that seems to relate to the position of the ship or whether the ship is hit or not. Does 'Raster' refer to the ship?

I hope there are more comments because I would like to learn how to graphically represent this type of problem.

i have not posted the whole code here!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.