collision detection question using Oslib

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
cruisx
Posts: 8
Joined: Tue Mar 25, 2008 8:26 am

collision detection question using Oslib

Post by cruisx »

int()
Last edited by cruisx on Wed Apr 02, 2008 8:06 am, edited 1 time in total.
a_noob
Posts: 97
Joined: Sun Sep 17, 2006 8:33 am
Location: _start: jr 0xDEADBEEF

Post by a_noob »

there are many ways to do collision detections for that, but you need to tell us more, like is that an image or a tile map. if its and image and you want pixel perfect collisions you could make a collision map which is a shilouette of the image and you just check you see that you are on the designated color that allows walking. You can do similar on a tile based map. just check is the character is on a walkable tile.

Code: Select all

.øOº'ºOø.
'ºOo.oOº'
PosX100
Posts: 98
Joined: Wed Aug 15, 2007 1:02 am

Post by PosX100 »

Looks like its tile based.

You simply check if player's tile collides with the grass tile type.

Something like this:

Code: Select all



typedef enum
	{
		TILE_PLAYER=1,
		TILE_GRASS=2...ETC
	}

inline int getCurrentTileType(const int& x,const int& y,const map& themap)
{

	return &#40;&#40;&#40;x>=themap.width&#40;&#41; ||y>=themap.height&#40;&#41; ||y<0||x<0&#41;?-1 &#58;themap.tiles&#91;y&#93;&#91;x&#93;&#41;&#41;;
&#125;

inline void getTilePos&#40;const int& wanted_tile,int& px,int& py,const map& themap&#41;
&#123;
        px=-1,py=-1;
	int x=0,y=0;
        for &#40;y=0;y<themap.height&#40;&#41;;y++&#41;
	&#123;
        for &#40;x=0;x<themap.width&#40;&#41;;x++&#41;
	&#123;
		if &#40; getCurrentTileType&#40;px,py,themap&#41; != wanted_tile &#41; continue;
        	px = x;
	        py = y;
                return;
               
	&#125;
	&#125;


&#125;



 

int main&#40;&#41;
&#123;
		map themap;

		while&#40;running&#41;
		&#123;
 
         	   int player_x,player_y,current_tile;
  	
		   getTilePos&#40;TILE_PLAYER,player_x,player_y,themap&#41;;
		   current_tile = getCurrentTileType&#40;player_x,player_y,themap&#41;;
		   if&#40;current_tile == TILE_GRASS&#41;
			&#123;
				//COLLISON! do whatever.. 
			&#125;
		    if&#40;timer.diff&#40;&#41; >= 10&#41;..cleanup render etc..
		   
		&#125;
&#125;
Simple as that :)
DoE
Posts: 2
Joined: Wed Mar 26, 2008 5:17 am

Post by DoE »

^ Perhaps instead of checking for a certain tile type, the tile structs/objects could have a `walkable' field that is set to either true or false.
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

DoE wrote:^ Perhaps instead of checking for a certain tile type, the tile structs/objects could have a `walkable' field that is set to either true or false.
As a_noob said, there are MANY ways of doing it. I'd suggest the OP consult one of the many tutorials on the web about basic game design. You can also find many fine books in any bookstore on basic game design. It has nothing to do with the PSP specifically.
cruisx
Posts: 8
Joined: Tue Mar 25, 2008 8:26 am

Post by cruisx »

PosX100 wrote:Looks like its tile based.

You simply check if player's tile collides with the grass tile type.

Something like this:

Code: Select all



typedef enum
	&#123;
		TILE_PLAYER=1,
		TILE_GRASS=2...ETC
	&#125;

inline int getCurrentTileType&#40;const int& x,const int& y,const map& themap&#41;
&#123;

	return &#40;&#40;&#40;x>=themap.width&#40;&#41; ||y>=themap.height&#40;&#41; ||y<0||x<0&#41;?-1 &#58;themap.tiles&#91;y&#93;&#91;x&#93;&#41;&#41;;
&#125;

inline void getTilePos&#40;const int& wanted_tile,int& px,int& py,const map& themap&#41;
&#123;
        px=-1,py=-1;
	int x=0,y=0;
        for &#40;y=0;y<themap.height&#40;&#41;;y++&#41;
	&#123;
        for &#40;x=0;x<themap.width&#40;&#41;;x++&#41;
	&#123;
		if &#40; getCurrentTileType&#40;px,py,themap&#41; != wanted_tile &#41; continue;
        	px = x;
	        py = y;
                return;
               
	&#125;
	&#125;


&#125;



 

int main&#40;&#41;
&#123;
		map themap;

		while&#40;running&#41;
		&#123;
 
         	   int player_x,player_y,current_tile;
  	
		   getTilePos&#40;TILE_PLAYER,player_x,player_y,themap&#41;;
		   current_tile = getCurrentTileType&#40;player_x,player_y,themap&#41;;
		   if&#40;current_tile == TILE_GRASS&#41;
			&#123;
				//COLLISON! do whatever.. 
			&#125;
		    if&#40;timer.diff&#40;&#41; >= 10&#41;..cleanup render etc..
		   
		&#125;
&#125;
Simple as that :)
yes it is tile based =). i am lookin over the coe right now, if i have any Qs ill post back.
pjeff
Posts: 1
Joined: Thu Mar 27, 2008 11:15 pm

collision

Post by pjeff »

cruisx
Posts: 8
Joined: Tue Mar 25, 2008 8:26 am

Re: collision

Post by cruisx »

THANK YOU!!! this is what i was looking for =D. to bad its in french but i get the main idea.
Post Reply