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

Click For Summary
SUMMARY

The discussion focuses on implementing projectile motion for a basketball game using OpenGL with GLUT 3.7 in a Win32 Console C++ environment. The user seeks assistance in applying physics equations such as s = vi + 1/2 * at² and a = g = -9.8 to simulate realistic ball movement. The provided code includes functions for calculating ball position based on force and angle inputs, but the user requires guidance on refining the implementation to achieve accurate projectile behavior.

PREREQUISITES
  • Understanding of projectile motion physics, including key equations.
  • Familiarity with OpenGL and GLUT 3.7 for 2D graphics rendering.
  • Basic knowledge of C++ programming and syntax.
  • Experience with handling user input and rendering in graphical applications.
NEXT STEPS
  • Research "OpenGL GLUT timer functions" for smoother animations.
  • Learn about "C++ floating-point precision" to improve calculations.
  • Explore "physics engines" like Box2D for more realistic motion simulations.
  • Investigate "OpenGL coordinate systems" to better manage object positioning.
USEFUL FOR

Game developers, physics simulation enthusiasts, and students learning about graphics programming and projectile motion in C++.

Msid17
Messages
5
Reaction score
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..

2u8atdz.gif


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:
Technology news on Phys.org
In other words...i just want the ball to work like it actually does..:S please Help...
 

Similar threads

  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 21 ·
Replies
21
Views
9K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K