PDA

View Full Version : Projectile Trajectory from a cannon ball



Umair Qureshi
09-25-2010, 01:20 AM
Need help from u all
am a newbie here!!!

making a 2D project, snap link is given below

http://i238.photobucket.com/albums/ff224/umairqureshi_6/back.png

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 &amp;&amp; xx<PosX+90 &amp;&amp; yy>PosY-80 &amp;&amp; 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
i am new to programming,
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

RefleX
09-26-2010, 09:01 AM
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

1) You need to swap buffers to make what ever you have drawn to the screen.

2) I'm not exactly sure what is going on in your equations :(

3) First of all you only need to use trig function (sin/cos/tan) only in the creation of the projectile, also it is very taxing to do allot of them every time you update your logic.


struct CannonBall
{
public:
Vector2 pos, vel, acc;

/** Used to calculate the x, and y factors of acceleration. */
Vector2 resolution(float power, float dir_in_rad) const // please tell me you know what a radian is
{
return Vector2(power*cos(dir_in_rad), power*sin(dir_in_rad));
}

/** convert degrees into radians. */
float toRad(float deg) const { return 3.14159274f/180*deg; }

void init(float power, float dir_in_degrees)
{
vel = resolution(power, toRad(dir_in_degrees));
float gravityPower = 1; // in pixels, may need adjustments
acc = resolution(gravityPower, toRad(270)); // 270 gravity down
}

void logic()
{
vel += acc;
pos += vel;
}
} cannonBall;