# Need help in a Basket Ball game(Projectile Motion / motion under Gravitation)

• Msid17
In summary, the programmer is new to programming and is working on an assignment. They are asking for help with using projectile motion and gravity on a ball. They provide code to calculate the position of the ball, and to render a bitmap of the ball. They also provide code to load a background image and a ball image. Finally, they provide a main function that starts the program.
Msid17
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);
}
}
void bgImg()
{
glRasterPos2i(0,0);
image[0].mDraw();
}

void ballImg(float bx, float by)
{
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);
gluOrtho2D(0.0,1000,0.0,10000);
glViewport(0.0,0.0,width,height);
glMatrixMode(GL_MODELVIEW);

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);
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);
glutSpecialFunc(keyboard);
glutDisplayFunc(display);
glutTimerFunc(1000,timer,1);
glutMainLoop();
return(0);
}

Last edited:

## 1. What is projectile motion?

Projectile motion is the movement of an object through the air or space under the influence of gravity. This type of motion is characterized by a combination of horizontal and vertical motion.

## 2. How is projectile motion related to basketball?

In basketball, projectile motion is important because it determines the trajectory of the ball when it is thrown or shot. Understanding this type of motion can help players improve their accuracy and make better shots.

## 3. What factors affect projectile motion in basketball?

The factors that affect projectile motion in basketball include the initial velocity of the ball, the angle at which it is thrown, and air resistance. The force of gravity also plays a significant role in determining the path of the ball.

## 4. How can projectile motion be calculated in a basketball game?

Projectile motion in a basketball game can be calculated using mathematical equations that take into account the initial velocity, angle of release, and acceleration due to gravity. These calculations can help players predict the trajectory of the ball and make more accurate shots.

## 5. How can understanding projectile motion help improve my basketball skills?

Understanding projectile motion can help improve your basketball skills by allowing you to make more accurate shots. By knowing how different factors affect the trajectory of the ball, you can adjust your technique and improve your shooting accuracy. Additionally, understanding projectile motion can also help with other aspects of the game, such as passing and rebounding.

• Programming and Computer Science
Replies
75
Views
4K
• Programming and Computer Science
Replies
3
Views
3K
• Programming and Computer Science
Replies
5
Views
3K
• Programming and Computer Science
Replies
8
Views
4K
• Programming and Computer Science
Replies
21
Views
8K
• Programming and Computer Science
Replies
2
Views
3K
• Programming and Computer Science
Replies
2
Views
3K
• Programming and Computer Science
Replies
6
Views
3K
• Engineering and Comp Sci Homework Help
Replies
14
Views
4K
• Engineering and Comp Sci Homework Help
Replies
0
Views
2K