PDA

View Full Version : Problem with a bounce ball



qla123
09-17-2009, 09:53 AM
Welcome!
As the developer of me weak and I need to include the subject, I turn to you for assistance!

I have to make a ball that will bounce from the four edges of the monitor. I already have ball, which is reflected from the bottom edge of the monitor. How do I do to be reflected in the schema <bottom, left, top, right, bottom edge? I know that something needs to do some something with vectors, but once that this was not in school, and on the internet hard to understand what and how, the two does not quite know where to place it in your code.
I would be grateful for your help. Sorry for my English.
Here's the code:

#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")

#define _USE_MATH_DEFINES
#include <cmath>

void InitGL(void)
{

glClearColor(0.5f,0.6f,1.0f,1.0f);


glEnable(GL_DEPTH_TEST);

glEnable(GL_NORMALIZE);



float pozy[4] = {45,40,100,20};

float ambi[4] = {1.0f,0.0f,0.04f,0};

float diff[4] = {0.75f,0.75f,0.4f,0};

float spec[4] = {1,1,0,0};
glLightfv(GL_LIGHT0,GL_POSITION, pozy);

glLightfv(GL_LIGHT0,GL_AMBIENT, ambi);

glLightfv(GL_LIGHT0,GL_DIFFUSE, diff);

glLightfv(GL_LIGHT0,GL_SPECULAR, spec);







float pozy1[4] = {0,-10,0,1};

float ambi1[4] = {0.0,0.0,1.01f,0};

float diff1[4] = {0.75f,0.75f,0.4f,0};

float spec1[4] = {1,1,1,0};
glLightfv(GL_LIGHT1,GL_POSITION, pozy1);

glLightfv(GL_LIGHT1,GL_AMBIENT, ambi1);

glLightfv(GL_LIGHT1,GL_DIFFUSE, diff1);

glLightfv(GL_LIGHT1,GL_SPECULAR, spec1);

glEnable(GL_COLOR_MATERIAL);



float mambi[4] = {0,0,0.01f,0};

float mdiff[4] = {0.75f,0.75f,0.4f,0};

float mspec[4] = {1,1,1,0};



glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mambi);

glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mdiff);

glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mspec);

glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 32);





glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

//glEnable(GL_LIGHT1);





}

void DrawGLScene(void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50,4.0f/3.0f,1,100);
glTranslatef(0,-4,-20);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_MAP1_VERTEX_3);


glTranslatef (0, -0.001, 0);


GLfloat x,y,kat;

float czas = GetTickCount()/1000.0;

float YY = 0.4+6.0*abs(cos(czas));//

float b = min(YY,1); //wielko&amp;#347;&amp;#263; ugi&amp;#281;cia
float a = 0.99/b; // kszta&amp;#322;t kuli

const GLfloat GL_PI=3.1415f; //PI

for (int i=0; i<1000; i++){ // P&amp;#281;tla rysuj&amp;#261;ca Sfere
glPushMatrix(); //??
glRotatef(i/1000.0*360.0,0,1,0); //
glTranslatef(0.1,0.0,0); //?
glBegin(GL_TRIANGLE_FAN); // rysowanie kólka
glColor3f(0.5,0.5,0.5);
for(kat = 0.0f; kat < (2.0f*GL_PI); kat += (GL_PI/32.0f))
{

x = a*sin(kat); // odpowiada za rysowanie ko&amp;#322;a
y = b*cos(kat); //


glVertex2f(x, y+YY);


}

glEnd();
glPopMatrix();
}



}
HGLRC hRC = NULL;
HDC hDC = NULL;
HWND hWnd = NULL;
HINSTANCE hInstance = NULL;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);


bool CreateGLWindow(char* title, int width, int height)
{
GLuint PixelFormat;
WNDCLASS wc;
RECT WindowRect;
WindowRect.left = (long)0;
WindowRect.right = (long)width;
WindowRect.top = (long)0;
WindowRect.bottom = (long)height;
LPCWSTR nazwa = TEXT("Start");


hInstance = GetModuleHandle(NULL);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = nazwa;

RegisterClass(&amp;wc);

hWnd = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE, nazwa,
nazwa,
WS_SYSMENU |
WS_CLIPSIBLINGS |
WS_CLIPCHILDREN,
0, 0,
width,
height,
NULL,
NULL,
hInstance,
NULL);

static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};

hDC=GetDC(hWnd);
PixelFormat=ChoosePixelFormat(hDC,&amp;pfd);
HRESULT rez = SetPixelFormat(hDC,PixelFormat,&amp;pfd);
hRC=wglCreateContext(hDC);
wglMakeCurrent(hDC,hRC);
ShowWindow(hWnd,SW_SHOW);
InitGL();
return true;

}

LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch (uMsg)
{
case WM_ACTIVATE:
{
return 0;
}

case WM_CLOSE:
{
PostQuitMessage(0);
return 0;
}

}

return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
bool done = false;



if (!CreateGLWindow(NULL,800,600))
{
return 0;
}

while(!done)
{
if (PeekMessage(&amp;msg,NULL,0,0,PM_NOREMOVE))
{
if (!GetMessage(&amp;msg, 0, 0, 0)) done=true;
else {
TranslateMessage(&amp;msg);
DispatchMessage(&amp;msg);
}

}
else
{
DrawGLScene();
SwapBuffers(hDC);
}
}

return (msg.wParam);
}