Page 1 of 1

Collision Detection Question

Posted: Sat Sep 10, 2011 3:53 pm
by DTKilb
I'm currently making a 2D game using Allegro (not tile based), and I've created this collision function:

Code: Select all

void player::setPreviousLocation()
{
	if(facingUp)
		y = y + speed;

	if(facingDown)
		y = y - speed;

	if(facingLeft)
		x = x + speed;

	if(facingRight)
		x = x - speed;
}

void player::applyCollision(int a, int b, int c, int d)
{
	if(y > a && y < b && x > c && x < d)
	{
		setPreviousLocation();
	}
}
This collision method is very successful, but I've been stuck making long lists of:
applyCollision(#, #, #, #);
applyCollision(#, #, #, #);
applyCollision(#, #, #, #);
applyCollision(#, #, #, #);
etc. for every level of the game. I've thought about making a separate program that allows you to click on specific points on the screen, and
store the value of the first two click's y coordinates (which would be 'a' and 'b' in terms of the applyCollision() function), and the last two click's x coordinates(which would be 'c' and 'd' in terms of the applyCollision() function). The values will be stored in a notepad file using the fstream library, breaking a line between every 4 numbers. I want to know how I could allow my game to read these notepad files and call the applyCollision() function, taking every four numbers in the notepad file as its parameters with the usage of a for loop. This way, I can save myself the trouble of listing down the applyCollision function for every level, and simply have the coordinates stored into notepad files for each level.

Re: Collision Detection Question

Posted: Sat Sep 10, 2011 10:40 pm
by k1net1k
I see a lot of people do simple level design with an array (or vector or whatever it is in c) with level data like this

map1[0][0][0][0][0]
map2[0][0][1][0][0]
map3[0][0][1][0][0]
map4[0][0][1][0][0]

So make a txt file somewhat similar structure, and load it into the array when during your initLevel()

and during your game loop you check to see if the array (where 'i' is the x/y coord of where you want to test)
1:collidable, or 0:notcollidable

This seems to be a quick way to do tile based maps. for no tile based you would have an array of objects (eg object[23,45,collidable];), and do a foreach on that array and return the object's x pos


not sure if that helps :)

Re: Collision Detection Question

Posted: Sat Sep 10, 2011 11:50 pm
by DTKilb
k1net1k wrote:I see a lot of people do simple level design with an array (or vector or whatever it is in c) with level data like this

map1[0][0][0][0][0]
map2[0][0][1][0][0]
map3[0][0][1][0][0]
map4[0][0][1][0][0]

So make a txt file somewhat similar structure, and load it into the array when during your initLevel()

and during your game loop you check to see if the array (where 'i' is the x/y coord of where you want to test)
1:collidable, or 0:notcollidable

This seems to be a quick way to do tile based maps. for no tile based you would have an array of objects (eg object[23,45,collidable];), and do a foreach on that array and return the object's x pos


not sure if that helps :)


I greatly appreciate the advice, but as I stated:
I want to know how I could allow my game to read these notepad files and call the applyCollision() function, taking every four numbers in the notepad file as its parameters with the usage of a for loop. This way, I can save myself the trouble of listing down the applyCollision function for every level, and simply have the coordinates stored into notepad files for each level.

Re: Collision Detection Question

Posted: Sun Sep 11, 2011 2:25 am
by treyrust
That's easy! I've spent the last two weeks burried in the fstream library making a text based adventue, here is an example:

Code: Select all

std::ifstream file("filepathhere.txt");

int firstParam, secondParam, thridParam, fourthParam;

while(file.good() == true)
{
   file >> firstParam >> secondParam >> thirdParam >> fourthParam;
   applyCollision(firstParam, secondParam, thirdParam, fourthParam);
}
Hope this helps, but this is a very basic coding problem and you really should be figuring out this stuff on you're own. The reason I say this is because when you start doing very complex stuff, especially if you get a job coding, you'll only have yourself to figure this out. I.E, better learn how to solve simple problems, so you can solve bigger problems that nobody will help you with.

Trey.

Re: Collision Detection Question

Posted: Sun Sep 11, 2011 7:42 am
by k1net1k
oh sorry man, i didnt read your question properly :(

Edit : In b4 "K1net1k, maybe you should have read your own avatar"