Public Group

I canot finish it ( I think im so bad )

This topic is 4105 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Well I have tried to make this easy esasy game but is to dificcult. First I scrap a lot of things in my code and began using Classes. Everithing I dont know is: Make The ball bounce. Make the ball bounce in different positions. The AI is the last part that I want to make since collision detection is more difficult I think. I tried to make the "collision" like I have seen in some code before but i dont undersant what to do then void Collision(Rpaddle &pad, Lpaddle &lpad) { if(.......) return true. else.......... } Here is the code:
//Ball, Paddle classes
#ifndef CLASES_H
#define CLASES_H

{
public:
GLfloat xPos;		//THE Y POSITION

{
public:
GLfloat yPos;		 //Y position

class Ball
{
public:
GLfloat PosX; //X position
GLfloat PosY;  //Y position
Ball(){ this->PosX = 0.0; this->PosY = 0.0;}
//Collision
}; Ball Balon;
#endif


//main
#include <glut.h>
#include "Clases.h"

const float UpWall = 1.48;
const float DownWall = -1.257;

//This is my function that TRIES to detect the collision betewn the ball and paddles and wall
{
if(unBalon.PosX >= 1.8)
return false;
if(unBalon.PosX >= -1.8)
return false;
if(unBalon.PosY <= UpWall)
return false;
if(unBalon.PosY <=  DownWall)
return false;
else
return true;
} //But I dont know what more to do
//I see some games making it like it

void update(int w);
void keyboard(int key,int a,int b);
void display();
void init();
void handleResize(int w, int h);

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow("Pong");
init();
glutDisplayFunc(display);
glutSpecialFunc(keyboard);
glutReshapeFunc(handleResize);
glutTimerFunc(25,update,0);
glutMainLoop();
}

void init()
{
glEnable(GL_DEPTH_TEST);
}

void handleResize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0,(double)w / (double)h, 1.0, 200.0);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0, Izquierdo.yPos, 0);
glVertex3f(-2.1f, -0.6f, -5.0f);
glVertex3f(-1.8f, -0.6f, -5.0f);
glVertex3f(-1.8f,  0.4f, -5.0f);
glVertex3f(-2.19f, 0.4f, -5.0f);
glEnd();

glLoadIdentity(); // Reset the transformation matrix
glTranslatef(0,Derecho.xPos,0);
glVertex3f(2.1, -0.6f, -5.0f);
glVertex3f(1.8, -0.6f, -5.0f);
glVertex3f(1.8,  0.4f, -5.0f);
glVertex3f(2.1,  0.4f, -5.0f);
glEnd();

//Ball
glTranslatef(Balon.PosX,Balon.PosY, 0);
glVertex3f(-0.1f, -0.3f, -5.0f);
glVertex3f( 0.1f, -0.3f, -5.0f);
glVertex3f( 0.1f, -0.1f, -5.0f);
glVertex3f(-0.1f, -0.1f, -5.0f);
glEnd();

//Begin The top wall
glVertex3f( 5.2f, 2.1f, -5.0f);
glVertex3f(-2.2f, 2.1f, -5.0f);
glVertex3f(-2.2f, 1.9f, -5.0f);
glVertex3f( 5.2f, 1.9f, -5.0f);
//Begin Down Wall
glVertex3f( 5.2f, -2.1f, -5.0f);
glVertex3f(-2.2f, -2.1f, -5.0f);
glVertex3f(-2.2f, -1.9f, -5.0f);
glVertex3f( 5.2f, -1.9f, -5.0f);
glEnd();
glutSwapBuffers();
}

void keyboard(int key,int a,int b)
{

switch (key)
{
//Player 1
case  GLUT_KEY_UP:
Izquierdo.yPos += 0.1;			// Move up
if(Izquierdo.yPos >= UpWall)		//"Up WallDetection"
{
Izquierdo.yPos = UpWall;
}
break;

case GLUT_KEY_DOWN:
Izquierdo.yPos -= 0.1;				//Move down
if(Izquierdo.yPos <= DownWall)		//"Down Wall Detection"
{
Izquierdo.yPos = DownWall;
}
break;
//Player 2 It is gonna be the Computer
}
display();
}

void update(int w)
{
Balon.PosX += 0.1;
bool Registro = Balon.checkCollision(Derecho,Balon,Izquierdo);
glutPostRedisplay();
glutTimerFunc(25,update,0);

}

I know is a mess but I first want to do everithing first and when everithing works I will like change a lot from it


Share on other sites
if(unBalon.PosX >= 1.8)		return false;	 if(unBalon.PosX >= -1.8)		return false;	 if(unBalon.PosY <= UpWall)		 return false;	 if(unBalon.PosY <=  DownWall)		 return false;	else 		return true;

condition 1 is implied by condition 2 meethinks. and more or less the same can be said about conditions 3 and 4 once the values of UpWall and DownWall are evaluated. Don't you want to replace the gequal with lequal somewhere ( e.g. at condition 1 )?
Furthermore you don't need an else there because you can't reach it if one of your if-s was true anyway ( because you returned )

Share on other sites
Hey,

Let's start from the beginning. Please keep the code in english, re-edit it for submission if you have to, variables such az Iziquerdo won't help us understand your code.

Now, let's move on to bouncy bouncy.

First of all you need to start thinking with objects. The ball is an object.

Most important thing for us is the VECTOR along which our ball moves. I assume that you're trying to do that in 2d.

class Vector2d{ public:  float x;  float y;  ...}

We assume that the vector is anchored at the position of your ball. The length of the vector is the speed of the ball. This means that in the next frame the ball will appear at ballX = ballX + vector.x , ballY= ballY+ vector.y

When the ball hits something, it will bounce at speed reduced by friction, against the normal (perpendicular to surface) vector of the surface, using the same angle.
To learn what are normal vectors, and how bouncing works, you will need to fetch your math book, and possibly a physics book for the bounce thing. Or simply draw it on a piece of paper with a grid.

Collision detection is something you will have to think a lot about. Generally, you need to check your ball against ALL collidable objects in the scene (reducing this number to speed up computation is quite a task in itself, so let's disregard this).

The basic idea is to encase every object in a bounding volume, like a box, and then see if boxes overlap for collision.

Basic sphere->box collision test can look like this :
bool AABB_Sphere(nSphere sphere,int mode) 	{		// false for no collision, true for collision, how smart is that? :)				float d=0.0;							float sphereaxis[3]={sphere.center.x,sphere.center.y,sphere.center.z};			float minaxis[3]={BV.vmin.x,BV.vmin.y,BV.vmin.z};			float maxaxis[3]={BV.vmax.x,BV.vmax.y,BV.vmax.z};						for (int i=0;i<3;i++)			{				if (sphereaxis < minaxis)				{					d += (sphereaxis - minaxis)*(sphereaxis - minaxis);				}				else if (sphereaxis > maxaxis)				{					d += (sphereaxis - maxaxis)*(sphereaxis - maxaxis);				}			}			// if sphere intersects the box			if (d < (sphere.radius*sphere.radius))			{				return true;			} return false ; // false in all other cases}

This is for 3d. For 2d test, you need to simply reduce the number of tested axes to two. Basically, if the distance from the box is less than radius^2 there is a collision. This is quite an old code of mine, and I'm not entirely sure why it's sphere^2 anymore :P I'm quite certain this method was taken from some book, but Real Time Rendering 2nd ed didn't have it when i looked just now.

When there is a collision, you perform the bounce mentioned above.

Unfortunately this is not an easy matter, and WILL require a lot of thought and thinking about the underlying geometry of objects you are using, and a LOT of maths to make it spin.
Read up on vectors, normal vectors, normalizing, bouncing, etc. High-school level math manual should have most of this stuff.

For the rest, if you're interested, mentioned real-time rendering 2nd ed will be useful, however it has a lot of stuff you won't ever need.

Hope this somewhat sketchy rundown helped you grasp what you're being pitted against.

Good luck!

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 11
• 26
• 10
• 11
• 9
• Forum Statistics

• Total Topics
633722
• Total Posts
3013538
×