Sign in to follow this  
SSJCORY

Collision detection.

Recommended Posts

I was just reading an article on gamedev about bounding box collision detection but i cant seem to get it to work here is the article. http://www.gamedev.net/reference/articles/article735.asp Here is my code.
#include<windows.h>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
class cMap;
class cPlayer;
class cMap{
private:
	int width;
	int height;
	vector<cPlayer> players;
	int currentplayer;
public:
	int getwidth(){
		return width;
	}
	int getheight(){
		return height;
	}
	cMap(){
		width = 500;
		height = 500;
		currentplayer = 1; 
	}
	cMap(int newwidth,int newheight){
		width = newwidth;
		height = newheight;
		currentplayer = 1;
	}
	void moveplayer(int direction);
	void addplayer(cPlayer newplayer);
	void display(HWND hwnd);
	int didcollide(cPlayer player1,cPlayer player2);
};
class cPlayer{
private:
	int x;
	int y;
	int life;
	string name;
	HBITMAP visual;
public:
	int getX(){
		return x;
	}
	int getY(){
		return y;
	}
	HBITMAP getVisual(){
		return visual;
	}
	void setX(int newx){
		x = newx;
	}
	void setY(int newy){
		y = newy;
	}
	void setVisual(HBITMAP newbitmap){
		visual = newbitmap;
	}
	cPlayer(){
		visual = (HBITMAP)LoadImage(0,"defaultplayer.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
		x = 0;
		y = 0;
		name = "default";
		life = 100;
	}
	cPlayer(int startx,int starty,string newname){
		visual = (HBITMAP)LoadImage(0,"defaultplayer.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
		x = startx;
		y = starty;
		name = newname;
		life = 100;
	}
	cPlayer(HBITMAP hbitmap){
		visual = hbitmap;
		x = 0;
		y = 0;
		name = "default";
		life = 100;
	}
	cPlayer(HBITMAP hbitmap,int startx,int starty){
		visual = hbitmap;
		x = startx;
		y = starty;
		name = "default";
		life = 100;
	}
	void display(HWND hwnd){
		HDC hdc = GetDC(hwnd);
		HDC idc = CreateCompatibleDC(hdc);
		SelectObject(idc,visual);
		BitBlt(hdc,x,y,50,50,idc,0,0,SRCCOPY);
		ostringstream text;
		text<<life;
		TextOut(hdc,x-15,y-70,"--------------",14);
		TextOut(hdc,x-17,y-70,"+",1);
		TextOut(hdc,x-17,y-40,"+",1);
		TextOut(hdc,x-17,y-60,"+",1);
		TextOut(hdc,x,y-40,text.str().c_str(),text.str().size());
		TextOut(hdc,x,y-60,name.c_str(),name.size());
		DeleteDC(idc);
		SetPixel(hdc,500,500,RGB(255,0,0));
	}
	
};
cMap cmap;
bool done = false;
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam){
	switch(iMsg){
	case WM_DESTROY:
		PostQuitMessage(0);
		done = true;
		return 0;
		break;
	case WM_KEYDOWN:
		RECT rect;
		HDC hdc = GetDC(hwnd);
		GetWindowRect(hwnd,&rect);
		FillRect(hdc,&rect,(HBRUSH)GetStockObject(WHITE_BRUSH));
		switch(wParam){
		case VK_LEFT:
			cmap.moveplayer(1);
			break;
		case VK_RIGHT:
			cmap.moveplayer(2);
			break;
		case VK_UP:
			cmap.moveplayer(3);
			break;
		case VK_DOWN:
			cmap.moveplayer(4);
			break;
		}
		cmap.display(hwnd);
		break;
	}
	return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hpInstance,PSTR cmdLine,int iCmd){
	HWND hwnd;
	MSG msg;
	WNDCLASS wc;
	wc.cbClsExtra = NULL;
	wc.cbWndExtra = NULL;
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = "cory";
	wc.lpszMenuName = NULL;
	wc.style = CS_VREDRAW|CS_HREDRAW;
	RegisterClass(&wc);
	hwnd = CreateWindow("cory","window",WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL);
	ShowWindow(hwnd,iCmd);
	UpdateWindow(hwnd);
	cPlayer player(200,200,"Kara");
	cPlayer player2(300,300,"Cory");
	
	cmap.addplayer(player);
	cmap.addplayer(player2);

	while(!done){
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else{
			if(cmap.didcollide(player,player2) == 1){
				MessageBox(NULL,"COLLISION!","COLLISION!",MB_OK);
			}
		}
	}
	UnregisterClass("cory",hInstance);
	return 0;
}
void cMap::display(HWND hwnd){
	HDC hdc = GetDC(hwnd);
	ostringstream os;
	os<<currentplayer+1;
	TextOut(hdc,10,10,"Player ",7);
	TextOut(hdc,60,10,os.str().c_str(),os.str().size());
	int countdown = players.size()-1;
	while(countdown >= 0){
		players[countdown].display(hwnd);
		countdown--;
	}
}
void cMap::addplayer(cPlayer newplayer){
	players.push_back(newplayer);
}
void cMap::moveplayer(int direction){
	if(direction == 1){
		players[currentplayer].setX(players[currentplayer].getX()-10);
	}
	if(direction == 2){
		players[currentplayer].setX(players[currentplayer].getX()+10);
	}
	if(direction == 3){
		players[currentplayer].setY(players[currentplayer].getY()-10);
	}
	if(direction == 4){
		players[currentplayer].setY(players[currentplayer].getY()+10);
	}
	if(currentplayer >= players.size() -1){
		currentplayer = 0;
	}
	else{
		currentplayer++;
	}
}
int cMap::didcollide(cPlayer player1,cPlayer player2){
		int left1,left2;
		int right1,right2;
		int top1,top2;
		int bottom1,bottom2;
		left1 = player1.getX();
		left2 = player2.getX();
		right1 = player1.getX() + 50;
		right2 = player2.getX() + 50;
		top1 = player1.getY();
		top2 = player2.getY();
		bottom1 = player1.getY() + 50;
		bottom2 = player2.getY() + 50;
		if (bottom1 < top2) return(0);
		if (top1 > bottom2) return(0);

		if (right1 < left2) return(0);
		if (left1 > right2) return(0);
		return 1;
}

THE method on the very very bottom cMap::didcollide is where i did basically the same thing he does in the article. Any ideas why it doesnt work? Thanks, -Cory Fisher

Share this post


Link to post
Share on other sites
Nevermind guys i just put didcollide inside display and after objects are displayed then it does didcollide(players[0],players[1]) and it works fine. Next step i have to make didcollide run collision detection on all objects which i havent figured out how i'm going to do that. ANy ideas?
-Cory Fisher
EDIT okay sorry for double post! Now i have it so that i use 2 for loops to traverse and check for collisions between each object i tested it and it works :-D
Now i'm thinking of what to do next any ideas?
-Cory Fisher

[Edited by - SSJCORY on June 21, 2005 8:40:54 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this