Hi Everyone, I need to know how can we represent the projectile motion gven a certain angle and velocity, such that I can watch the animated ball moving in is path on screen.... all using openGL in C++

Recommended Answers

All 7 Replies

forgot to mention that I need the explanation when using openGL in c++

First you need to know the Kinematic Equations

You can use them to model projectile motion.

1) Have a initial position, initial velocity, final position, final velocity,
and acceleration. The acceleration is what is going to make your
projectile motion a projectile motion.


Here is another refrence , Link

Particularly, you can use :

x = x_0 + v*t + 1/2at^2

where x is the final position x_o is the starting position, v is the
velocity, t is the time, and a is the acceleration.

Use the equation above to model projectile motion.

Here is another refrence , Ref

It gives your the proper equations as well.

Just remember that acceleration could be position and negative,
when it is negative the velocity will decrease until its zero then the
object should stop, also you will need a timer to control the
velocity.

Give it a go and see how it goes.

Many thanks for your reply, well I still can't imagine how I can apply this to programming in OpenGL...it's so complicated.... I want to specify an angle and then let the particle move in a projectile accordingly. . . Would it help if i tell you that I'm doing this as the firing part for the "Pocket Tanks" game ?
hope you would be able to reply to me again :) Thanks.

First have the object ready to fire, meaning have the object at the position
of where its going to be launched.

Then you could do something like this :

Ball.y += ball.velY;
Ball.x += ball.velX;

Have the velX faster than velY;
Then you need to change velY and VelX;
You could do something like this :

Ball.velX += Ball.InitialVelocity - Ball.accel * Ball.time
Ball.velY += Ball.InitialVelocity - Ball.accel * Ball.time

Your ball.accel is just gravity.

That's great and perfect...well i'll give it a try and let you know the update.... I'm so grateful for your assistance (Y)(Y)(Y)

Actually you asked about theta, this code should solve it :

Declare these variable somewhere where its accessible;

const float DEGTORAD = 3.1415769/180.0f;

//initial velocity
float iVel = 7.0f;

//initial position
float posX = 0.0f;
float posY = 0.0f;

//angle being fired
float theta = 75.0f;

//initial x and y velocity calculated
float ivelX = iVel*cos(theta*DEGTORAD);
float ivelY = iVel*sin(theta*DEGTORAD);

//new velocity
float velX = 0.0f;
float velY = 0.0f;

//changing time 
float Time = 0.0f;

Now your draw function could look something like this :

GLvoid disp()
{	
	//get time in seconds
	Time = clock()*0.001f;

 
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	
	glTranslatef(0,0,-2000);
	
	glPushMatrix();
	
	glTranslatef(posX,0.0f,0.0f);
	glTranslatef(0.0f,posY,0.0f);
		glutSolidSphere(10.0f,10,10);
	glTranslatef(-posX,0.0f,0.0f);
	glTranslatef(0.0f,-posY,0.0f);
 
		
	posX += velX;
	posY += velY;
	velY = ivelY - 9.8*Time;
	velX = ivelX;


	glutPostRedisplay();
	glutSwapBuffers();

}

of course instead of glutSphere you would have some other object.

The initial velocity determines how far the object will travel
Theta will represent th angle of fire, which determines the x and y initial velocity
The equation simplifies because I am assuming that there is no other force like drag or friction, and it also
assumes that the ball will not hit a surface. If you want to make the ball bounce then this would reduce the
velocity in the x direction as a result of friction, so it would get a little more complicated. However the above is
just a simple movement. just thought your should realize that.

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include<math.h>
    const float DEGTORAD = 3.1415769/180.0f;
    //initial velocity
    float iVel = 7.0f;
    //initial position
    float posX = 0.0f;
    float posY = 0.0f;
    //angle being fired
    float theta = 75.0f;
    //initial x and y velocity calculated
    float ivelX = 1.81173332;
    float ivelY = 6.76148078;
    //new velocity
    float velX = 0.0f;
    float velY = 0.0f;
    //changing time
    float Time = 0.0f;
void init(void)
{
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 900 };
GLfloat light_position[] = { 1.0, 0.0, 0.0, 0.0 };
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
 //get time in seconds
    Time = clock()*0.001f;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0,0,-2000);
    glPushMatrix();
    glTranslatef(posX,0.0f,0.0f);
    glTranslatef(0.0f,posY,0.0f);
    glutSolidSphere(10.0f,10,10);
    glTranslatef(-posX,0.0f,0.0f);
    glTranslatef(0.0f,-posY,0.0f);
    posX += velX;
    posY += velY;
    velY = ivelY - 9.8*Time;
    velX = ivelX;
    glutPostRedisplay();
    glutSwapBuffers();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho (-1.5*(GLfloat)w/(GLfloat)h,
1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

this program isnt working on my compiler .I just see a blank screen .Please could u help me out ?I basically want to learn continous motion using OpenGL .

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.