colisiones en openGl
Publicado por
Estefania (1 intervención) el 18/06/2014 00:37:18
Hola a todos, soy novata programando con Opengl, pero tengo un ejercicio parecido al q se propuso aquí anteriormente, tengo un balón que rebota en la pantalla, y cuando topa con los bordes regresa en sentido contrario, pero lo único que me falta y que no puedo hacer es que al dar clic sobre el balón éste rebote en sentido opuesto, es decir, si le doy clic en la parte inferior, el balón rebote hacia arriba y si doy clic del lado derecho el balón regrese por el izquierdo. ¡¿Alguien podría ayudarme con esto??!! --- Este es el código q tengo ---
#include "stdafx.h"
#include <GL\glut.h>
#include <Math.h>
#define PI 3.14159265f
char title[] = "Pelota que Rebota 2D";
int windowWidth = 640;
int windowHeight = 480;
int windowPosX = 50;
int windowPosY = 50;
GLfloat ballRadius = 0.2f;
GLfloat xPos = 0.0f; //Posicion inicial en X
GLfloat yPos = 0.0f; //Posicion inicial en Y
GLfloat xPosMax, xPosMin, yPosMax, yPosMin;
GLdouble xLeft, xRight, yBottom, yTop;
GLfloat xSpeed = 0.002f; // velocidad a la que se movera en las X
GLfloat ySpeed = -.007f; // velocidad a la que se movera en las Y
int refreshMillis = 5;
void initGL() {
// color de fondo
glClearColor(0.0, 0.0, 0.1f, 1.0);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(xPos, yPos, 0.0f);
glBegin(GL_TRIANGLE_FAN);
glColor3f(1.0f, 0.3f, 0.6f);
glVertex2f(0.0f, 0.0f);
int numSegments = 100; //redondeo de la figura
GLfloat angle;
for (int i = 0; i <= numSegments; i++) {
angle = i * 2.0f * PI / numSegments;
glVertex2f(cos(angle) * ballRadius, sin(angle) * ballRadius);
}
glEnd();
glutSwapBuffers();
/**
xPos += xSpeed;
yPos += ySpeed;
if (xPos > xPosMax) {
xPos = xPosMax;
xSpeed = -xSpeed;
} else if (xPos < xPosMin) {
xPos = xPosMin;
xSpeed = -xSpeed;
}
if (yPos > yPosMax) {
yPos = yPosMax;
ySpeed = -ySpeed;
} else if (yPos < yPosMin) {
yPos = yPosMin;
ySpeed = -ySpeed;
}**/
}
void Transicion(){
display();
xPos += xSpeed;
yPos += ySpeed;
if (xPos > xPosMax) {
xPos = xPosMax;
xSpeed = -xSpeed;
} else if (xPos < xPosMin) {
xPos = xPosMin;
xSpeed = -xSpeed;
}
if (yPos > yPosMax) {
yPos = yPosMax;
ySpeed = -ySpeed;
} else if (yPos < yPosMin) {
yPos = yPosMin;
ySpeed = -ySpeed;
}
}
//----------------------------------------------
void reshape(GLsizei weight, GLsizei height) {
if (height == 0) height = 1;
GLfloat aspect = (GLfloat)weight / height;
glViewport(1, 1, weight, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (weight <= height) {
xLeft = -1.0;
xRight = 1.0;
//yBottom = -1.0 / aspect;
//yTop = 1.0 / aspect;
} else {
xLeft = -1.0 * aspect;
xRight = 1.0 * aspect;
yBottom = -1.0;
yTop = 1.0;
}
gluOrtho2D(xLeft, xRight, yBottom, yTop);
xPosMin = xLeft + ballRadius;
yPosMax = yTop - ballRadius;
xPosMax = xRight - ballRadius;
yPosMin = yBottom + ballRadius;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Timer(int value) {
glutPostRedisplay();
glutTimerFunc(refreshMillis, Timer, 0);
}
void raton(int boton, int estado, int x, int y) {
switch (boton)
{
case GLUT_LEFT_BUTTON:
if (estado = GLUT_DOWN){
xPos += xSpeed;
yPos += ySpeed;
if(x == xPos){
x= xPosMin;
xSpeed = -xSpeed;
xPosMin = xLeft + ballRadius;
}
else if (y== yPos){
y =yPosMin;
ySpeed= -ySpeed;
}
}
else{
}
glutPostRedisplay();
default:
break;
}
}
void teclado(unsigned char tecla, int x, int y)
{
switch (tecla)
{
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, CHAR* argv[]) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition(windowPosX, windowPosY);
glutCreateWindow(title);
//glutDisplayFunc(display);
glutDisplayFunc(Transicion);
glutReshapeFunc(reshape);
glutTimerFunc(0, Timer, 0);
glutKeyboardFunc(teclado);
glutMouseFunc(raton);
initGL();
glutMainLoop();
return 0;
}
Cuaquier solución sería de gran ayuda. :)