A few more collision test questions for Pong!

Started by
7 comments, last by Ekim_Gram 19 years, 6 months ago
Lol, here's the code: [main.cpp]

#include "draw.h"

int main(int argc, char **argv)
{
	Uint8* keys;

	SDL_Init(SDL_INIT_VIDEO);

	screen = SDL_SetVideoMode(800,600,32, SDL_HWSURFACE | SDL_DOUBLEBUF);

	SDL_WM_SetCaption("My Pong Clone v0.1",NULL);
	InitImages();
	DrawBG();

	int done=0;

	while (done == 0)
	{
		SDL_Event event;

		while (SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT) { done = 1; }
			if(event.type == SDL_KEYDOWN)
			{
				if(event.key.keysym.sym == SDLK_ESCAPE) { done = 1; }
			}
		}
			
		keys = SDL_GetKeyState(NULL);
		if(keys[SDLK_UP])   { ypos2 -= 4; }
		if(keys[SDLK_DOWN]) { ypos2 += 4; }
		if(keys['w'])		{ ypos -= 4; }
		if(keys['s'])		{ ypos += 4; }

			ballx += ball_vx;
			bally += ball_vy;

			if(bally <= 10)
			{
				ball_vy = -ball_vy;
			}
			if(ballx >= 747)
			{
				ball_vx = -ball_vx;
			}
			if(bally >= 551)
			{
				ball_vy = -ball_vy;
			}
			if(ballx <= 10)
			{
				ball_vx = -ball_vx;
			}
			if(ballx <= xpos)  //WHY DOESNT THIS WORK!?
			{
				ball_vx = -ball_vx;
			}

		CheckPaddleYCol();
		DrawScene();
	}

	return 0;
}

[draw.cpp]

#include "draw.h"

int xpos=20;
int ypos=220;

int xpos2=735;
int ypos2=220;

int ballx = 370;
int bally = 235;
int ball_vx = 4;
int ball_vy = -4;

SDL_Surface *screen;
SDL_Surface *back;
SDL_Surface *image;
SDL_Surface *ball;

int InitImages()
{
	image = SDL_LoadBMP("paddle.bmp");
	back  = SDL_LoadBMP("bg.bmp");
	ball  = SDL_LoadBMP("ball.bmp");
	return 0;
}

void DrawIMG(SDL_Surface *img, int x, int y)
{
	SDL_Rect dest;
	dest.x = x;
	dest.y = y;
	SDL_BlitSurface(img, NULL, screen, &dest);
}

void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
{
	SDL_Rect dest;
	SDL_Rect dest2;

	dest.x = x;
	dest.y = y;

	dest2.x  = x2;
	dest2.y  = y2;
	dest2.w  = w;
	dest2.h  = h;

	SDL_BlitSurface(img, &dest2, screen, &dest);
}

void DrawBG()
{
	DrawIMG(back, 0,0);
}

void DrawScene()
{
	DrawIMG(back, xpos-8, ypos-8, 56, 166, xpos-8, ypos-8); //original image is 40x150
	DrawIMG(image, xpos,ypos);
	DrawIMG(back, xpos2-8, ypos2-8, 56, 166, xpos2-8, ypos2-8);
	DrawIMG(image, xpos2,ypos2);
	SDL_SetColorKey(ball, SDL_SRCCOLORKEY,
						SDL_MapRGB(ball->format, 0, -1, 0));
	DrawIMG(back, ballx-8, bally-8, 56,56,ballx-8,bally-8);
	DrawIMG(ball, ballx, bally);

	SDL_Flip(screen);
}

void CheckPaddleYCol()
{
		if(ypos <= 10)
		{
			ypos = 10;
		}
		else if(ypos >= 440)
		{
			ypos = 440;
		}

		if(ypos2 <= 10)
		{
			ypos2 = 10;
		}
		else if(ypos2 >= 440)
		{
			ypos2 = 440;
		}

}

[draw.h]

#ifndef _DRAW_H_
#define _DRAW_H_

#include <SDL/SDL.h>
#include <stdio.h>
#include <stdlib.h>

extern int xpos;
extern int ypos;
extern int xpos2;
extern int ypos2;
extern int ballx;
extern int bally;
extern int ball_vx;
extern int ball_vy;

extern SDL_Surface *screen;
extern SDL_Surface *image;
extern SDL_Surface *back;

int InitImages();
int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dest, SDL_Rect *destrect);
void DrawIMG(SDL_Surface *img, int x, int y);
void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2);
void DrawBG();
void DrawScene();
void CheckPaddleYCol();
void CheckBallCol();

#endif

i cant make the ball bounce off the paddles...any help? thanks! *oh and i know this wont matter when my paddle-to-ball detection works but when the ball moves over the paddle (the ball is a BMP image as you can see) i can see blue space in the shape of a square around the ball. does anybody know why? it should be transparent from the SDL_SetColorKey()
//-----------------------------------------One short sleepe past, wee wake eternally, And Death shall be no more, Death thou shalt die.
Advertisement
i just skimmed over it quickly but ...

Quote:

if (ballx <= xpos) {
ball_vx = -ball_vx;
}



you will need more bounding box checks than just this one ...
something along these lines, where ballX and ballY and paddleX and paddleY are the top left corners of the object.

if (  (ballX + ballWidth >= paddleX) &&  (ballX <= paddleX + paddleWidth) &&  (ballY + ballHeight >= paddleY) &&  (ballY <= paddleY + paddleHeight)) ballVX = -ballVX;


i hope this helps ... (and i hope that you didn't already have this in your code already and i was too dumb to see it)
no i didnt already have it :) how would i create a ball with all these functions accompanying it when all it is is a mere BMP file?

EDIT: This is my first graphics game so bare with me please :)
//-----------------------------------------One short sleepe past, wee wake eternally, And Death shall be no more, Death thou shalt die.
sorry if this doesn't help (i'm in a hurry and couln't look at all the code) but it looks like you're just checking if the ball is to the left of the paddle. try checking to see if the ball is between the left and right sides of the paddle:

assuming paddle_left is the left of your paddle and paddle_right is the right (or paddle_left + paddle_width)...

if(ballx >= paddle_left && ballx <= paddle_right)

and also check to see if the ball is below or at the top of the paddle:

(bally >= paddle_top)

if those two conditions are true, reverse the ball's y speed.

as for the color, humour me and try using something like magenta (255, 0, 255). change SDL_MapRGB(ball->format, 0, -1, 0)) to SDL_MapRGB(ball->format, 255, 0, 255)) and change that blue that you don't want drawn to EXACTLY (255, 0, 255). also, you can probably move the call to SDL_SetColorKey() to your InitImages() function. just call it right after you init your images.

i hope this helps, sorry that i couldn't go over all of your code. my site has a pong tutorial that uses SDL though, the link's in my siggie.

good luck!
-------------------------------See my tutorial site: Click here
(still need help with my last reply)
//-----------------------------------------One short sleepe past, wee wake eternally, And Death shall be no more, Death thou shalt die.
instead of just storing the bitmap, you should store its dimensions. I recommend having the x,y coordinates as well as width and height. do this for the ball and the paddle. you can do this with a struct as so:

struct Ball{    int x;    int y;    int w;    int h;    int xspeed;    int yspeed;}struct Paddle{    int x;    int y;    int w;    int h;    int xspeed;}


Collision detection would look something like:

Ball ball;ball.x = whatever;ball.y = whatever;...Paddle player;player.x = whatever;...if ( (ball.x >= player.x && ball.x <= player.x + player.w) &&      (ball.y >= player.y) ){    ball.yspeed = -ball.yspeed;}


does this help?
-------------------------------See my tutorial site: Click here
IF(X1 + WIDTH X2 + WIDTH2) NO COLLISION
IF(Y1 + HEIGHT Y2 + HEIGHT2) NO COLLISION
ELSE
COLLISON

That is the best logic for 2D rectangular collision, make it into a function that takes 2 objects or rects and returning a bool.
My message just got cut??? weird, I think theres an article on this anyway.
Just a meer matter of preference, instead of using ball.yspeed = -ball.yspeed; to reverse the y velocities on the ball, you could also use

ball.yspeed *= -1;


Yay inverses!

This topic is closed to new replies.

Advertisement