- #1
Msid17
- 5
- 0
HI,
I am new in this kind of programming ..this is my very first 2D game in OpenGl using Glut 3.7 (Win32 Console C++) and i have no idea how to apply projectile motion and gravitation on basket ball.
game is simple.. Inputs Are Force and Angle.. and player has to throw the ball into the basket.. for example..
if ball is at [x,y] and basket is at [x1,y1]..
seriously i don't knw anythng abt physics programming and all i knw is that i can use motion equations like s=vi + 1/2*at^2 or a = g = -9.8 or vf = vi + at or projectile equations but how to implement them...thats where i need you guys...please help me..its an assignment i have to complete it in 2 days.. therefore Any Kind of help.. literally any help would be greatly appreciated..so far this is my code... and i need you guys to help me improve it..
--------------------------------------------------------------------------#include<sstream>
#include<string>
#include<Windows.h>
#include<glut.h>
#include"RGBA.h"
#include<cmath>
//********* Namespace ********//
using namespace std;
//********* Variables Control ********//
// declair all variables here that are used in any function or control..
int width=800, height = 500;
float bx=10.0, by=10.0;
RGBApixmap image[2];
float force = 0.0;
int angle = 45;
float posx, posy, time=1;
//------------------
char sangle[] = "";
char sforce[] = "";
//********* Variables Control End********//
void calculate(float x0,float y0, float v0, float t, int theta)
{
float g = -9.8;
double PI =3.1415926535897932384626433832795;
double a = (PI /180)*theta;
posx = x0 + v0*cos(a)*t ;
posy = y0 + v0*sin(a)*t - 0.5*g*(t*t);
}
//********* Text Printing Func ********//
void renderBitmapString(float x, float y, void *font, char *string)
{
char *c;
glRasterPos2f(x,y);
for(c=string; *c != '\0'; c++)
{
glutBitmapCharacter(font, *c);
}
}
//********* ImageLoading Func ********//
void bgImg()
{
image[0].readBMPFile("bg.bmp");
glRasterPos2i(0,0);
image[0].mDraw();
}
void ballImg(float bx, float by)
{
image[1].readBMPFile("ball2.bmp");
image[1].setChromaKey(0,0,0);
glRasterPos2i(bx,by);
image[1].mDraw();
}
//********* Timer Func ********//
void timer(int t)
{
glutTimerFunc(1,timer,1);
calculate(posx,posy,15,time,45);
ballImg(posx,posy);
bx = posx ; by = posy; time+=0.1;
glutPostRedisplay();
}
//********* Keyboard Func ********//
void keyboard(int key, int x,int y)
{
switch(key)
{
case GLUT_KEY_RIGHT:
if(force==100)
{
force = 100;
sprintf(sforce,"%3f.3",force);
}
else force += 5;
break;
case GLUT_KEY_LEFT:
if(!force==0)
{
force -= 5;
sprintf(sforce,"%3f.3",force);
}
else force =0;
break;
case GLUT_KEY_DOWN:
if(angle >0 ) angle -= 1;
sprintf(sangle,"%f",angle);
break;
case GLUT_KEY_UP:
if(angle>=0 && angle <91) angle += 1;
sprintf(sangle,"%f",angle);
break;
default:
break;
}
}
//********* Display Func ********//
void display()
{
glClearColor(1,1,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_EQUAL,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,1000,0.0,10000);
glViewport(0.0,0.0,width,height);
glMatrixMode(GL_MODELVIEW);
//---------Loading Background Image--------//
bgImg();
glColor3f(0,0,1);
// glPointSize(8.0);
renderBitmapString(15.0,3.75,GLUT_BITMAP_TIMES_ROMAN_24,sforce);
//renderBitmapString(40.0,3.75,GLUT_BITMAP_TIMES_ROMAN_24,sangle);
//---------Loading Ball Image--------//
ballImg(bx,by);
glFlush();
}
//********* Display Func End********////********* Main Func ********//
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(width,height);
glutCreateWindow("Project --- BasketBall Trial ---");
glutSpecialFunc(keyboard);
glutDisplayFunc(display);
glutTimerFunc(1000,timer,1);
glutMainLoop();
return(0);
}
I am new in this kind of programming ..this is my very first 2D game in OpenGl using Glut 3.7 (Win32 Console C++) and i have no idea how to apply projectile motion and gravitation on basket ball.
game is simple.. Inputs Are Force and Angle.. and player has to throw the ball into the basket.. for example..
if ball is at [x,y] and basket is at [x1,y1]..
seriously i don't knw anythng abt physics programming and all i knw is that i can use motion equations like s=vi + 1/2*at^2 or a = g = -9.8 or vf = vi + at or projectile equations but how to implement them...thats where i need you guys...please help me..its an assignment i have to complete it in 2 days.. therefore Any Kind of help.. literally any help would be greatly appreciated..so far this is my code... and i need you guys to help me improve it..
--------------------------------------------------------------------------#include<sstream>
#include<string>
#include<Windows.h>
#include<glut.h>
#include"RGBA.h"
#include<cmath>
//********* Namespace ********//
using namespace std;
//********* Variables Control ********//
// declair all variables here that are used in any function or control..
int width=800, height = 500;
float bx=10.0, by=10.0;
RGBApixmap image[2];
float force = 0.0;
int angle = 45;
float posx, posy, time=1;
//------------------
char sangle[] = "";
char sforce[] = "";
//********* Variables Control End********//
void calculate(float x0,float y0, float v0, float t, int theta)
{
float g = -9.8;
double PI =3.1415926535897932384626433832795;
double a = (PI /180)*theta;
posx = x0 + v0*cos(a)*t ;
posy = y0 + v0*sin(a)*t - 0.5*g*(t*t);
}
//********* Text Printing Func ********//
void renderBitmapString(float x, float y, void *font, char *string)
{
char *c;
glRasterPos2f(x,y);
for(c=string; *c != '\0'; c++)
{
glutBitmapCharacter(font, *c);
}
}
//********* ImageLoading Func ********//
void bgImg()
{
image[0].readBMPFile("bg.bmp");
glRasterPos2i(0,0);
image[0].mDraw();
}
void ballImg(float bx, float by)
{
image[1].readBMPFile("ball2.bmp");
image[1].setChromaKey(0,0,0);
glRasterPos2i(bx,by);
image[1].mDraw();
}
//********* Timer Func ********//
void timer(int t)
{
glutTimerFunc(1,timer,1);
calculate(posx,posy,15,time,45);
ballImg(posx,posy);
bx = posx ; by = posy; time+=0.1;
glutPostRedisplay();
}
//********* Keyboard Func ********//
void keyboard(int key, int x,int y)
{
switch(key)
{
case GLUT_KEY_RIGHT:
if(force==100)
{
force = 100;
sprintf(sforce,"%3f.3",force);
}
else force += 5;
break;
case GLUT_KEY_LEFT:
if(!force==0)
{
force -= 5;
sprintf(sforce,"%3f.3",force);
}
else force =0;
break;
case GLUT_KEY_DOWN:
if(angle >0 ) angle -= 1;
sprintf(sangle,"%f",angle);
break;
case GLUT_KEY_UP:
if(angle>=0 && angle <91) angle += 1;
sprintf(sangle,"%f",angle);
break;
default:
break;
}
}
//********* Display Func ********//
void display()
{
glClearColor(1,1,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_EQUAL,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,1000,0.0,10000);
glViewport(0.0,0.0,width,height);
glMatrixMode(GL_MODELVIEW);
//---------Loading Background Image--------//
bgImg();
glColor3f(0,0,1);
// glPointSize(8.0);
renderBitmapString(15.0,3.75,GLUT_BITMAP_TIMES_ROMAN_24,sforce);
//renderBitmapString(40.0,3.75,GLUT_BITMAP_TIMES_ROMAN_24,sangle);
//---------Loading Ball Image--------//
ballImg(bx,by);
glFlush();
}
//********* Display Func End********////********* Main Func ********//
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(width,height);
glutCreateWindow("Project --- BasketBall Trial ---");
glutSpecialFunc(keyboard);
glutDisplayFunc(display);
glutTimerFunc(1000,timer,1);
glutMainLoop();
return(0);
}
Last edited: