Collision Detection Question

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
DTKilb
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 9
Joined: Sat Aug 06, 2011 9:43 pm

Collision Detection Question

Post 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.
User avatar
k1net1k
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 563
Joined: Sun Nov 07, 2010 2:58 pm
Contact:

Re: Collision Detection Question

Post 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 :)
DTKilb
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 9
Joined: Sat Aug 06, 2011 9:43 pm

Re: Collision Detection Question

Post 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.
User avatar
treyrust
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 36
Joined: Thu Jul 21, 2011 4:32 pm
Current Project: Between projects
Favorite Gaming Platforms: PS1/2/3/P, NES, Wii, Genesis, Dreamcast, GBA, DS
Programming Language of Choice: C++

Re: Collision Detection Question

Post 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.
User avatar
k1net1k
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 563
Joined: Sun Nov 07, 2010 2:58 pm
Contact:

Re: Collision Detection Question

Post by k1net1k »

oh sorry man, i didnt read your question properly :(

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