Jump to content
  • Advertisement
Sign in to follow this  
RedRabbit

How can I implement collision detection?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

heres the code: [main.cpp]
#include "draw.h"

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

	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		printf("Error: %s", SDL_GetError());
		exit(1);
	}
	atexit(SDL_Quit);

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

	if(screen == NULL)
	{
		printf("Error: %s", SDL_GetError());
		exit(1);
	}

	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])   { ypos -= 4; }
		if(keys[SDLK_DOWN]) { ypos += 4; }
		if(keys['w'])		{ ypos2 -= 4; }
		if(keys['s'])		{ ypos2 += 4; }


		DrawScene();
	}

	return 0;
}

[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 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();

#endif

[draw.cpp]
#include "draw.h"

int xpos=20;
int ypos=220;

int xpos2=735;
int ypos2=220;

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

int InitImages()
{
	image = SDL_LoadBMP("paddle.bmp");
	back  = SDL_LoadBMP("bg.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_Flip(screen);
}

Its a pong clone, and I want to check when a paddle hits either the top or the bottom of the window. Any help will be appreciated!

Share this post


Link to post
Share on other sites
Advertisement

if(paddle.Y < 0)
{
// paddle hit top of screen
}
if(paddle.Y > Screen.Height - paddle.Height)
{
// paddle hit bottom of screen
}




[Edited by - pimple on October 14, 2004 4:40:47 PM]

Share this post


Link to post
Share on other sites
All you have to do is check whether or not the paddle y coordinate is outside of the boundaries that it is allowed. Assuming ypos for the y coordinate and 0 to 480 for the boundaries..

if ( ( ypos < 0 ) || ( ypos > 480 ) )
{
// Collision
}

Simple as that...

Share this post


Link to post
Share on other sites
Quote:
Original post by falkone
All you have to do is check whether or not the paddle y coordinate is outside of the boundaries that it is allowed. Assuming ypos for the y coordinate and 0 to 480 for the boundaries..

if ( ( ypos < 0 ) || ( ypos > 480 ) )
{
// Collision
}

Simple as that...


thats the part i get....how do i stop the paddle from moving?

i was thinking something like:

if((ypos<0) || (ypos>600))
{
ypos = ypos;
}

but i dont know if that would work....thanks for the feedback guys i appreciate it

Share this post


Link to post
Share on other sites
That would work but you really don't have to do anything to the y at all. Just have it set so if its greater than the left bound and its less than the right bound it can move. No need to even check for the position.

Share this post


Link to post
Share on other sites
wouldnt it be
#define SCREENYMAX 1024

if (paddle.y <= 0) //paddle hit the top of the screen
if ((paddle.y + paddle.height) >= SCREENYMAX) //bottom
// if you dont add the paddle height, then your paddle
// could go out of the bottom of the screen

paddle.x
|
|
___ _____ paddle.y
| | ^
| | |
| | |h
| | |e
| | |i
| | |g
| | |h
| | |t
| | |
|___| __v



for the ball i would just do

if ((ball.x <= (paddle.x + paddle.width)) && 
(ball.y >= paddle.y) && ((ball.y + ball.height) <= (paddle.y + paddle.height))) // then the ball hit the paddle


height is the y extent/width the x...
maybe i forgot a bracket but i hope that theres no bug in it... its 7am and i cant guarantee for anything ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!