Collision, player sprite agains tile map

Started by
7 comments, last by bratiefanut 10 years, 6 months ago

I have made 2 days ago a topic about "how to load a map into my game". Finally, after searching over the internet and HGE forums, I have found an tutorial about tile maps in HGE and loaded a map into my game. What I cant figure out is how i can do the collision detection and my sprite dont get stuck in tiles or fall tru it?

Here are the files on Pastebin:

main.cpp

HGETileMapDatabases.h

HGETileMap.h

HGETileMap.cpp

Some help would be apreciated.

I posted below a screen shot of the tile level.

NOTE: The player is not standing on the platform. It is rendered just above the platform.

[attachment=18238:tile map test.PNG]

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

Advertisement
I'm going to make a wild guess and say your tile graphics have little holes at the top which cause your player to be stuck and you need a flat, invisible solid for it to walk over and to use for collision detection.

My map si flat and solid now. I want help to understand this class function and how i should call it because i am pretty confused now.....


bool HGETileMap::RectCollide(hgeRect r, CTile *plats[9],  int *nCols)
{
	int cPlat = 0;

	if (r.x1 < 0 || r.x2 < 0 || r.y1 < 0 || r.y2 < 0) return false;

	// calculate which tiles this affects
	int tx1 = int(r.x1 / TILE_W);		// start tile x
	int ty1 = int(r.y1 / TILE_H);		// start tile y
	int tx2 = int(r.x2 / TILE_W)+1;		// end tile x
	int ty2 = int(r.y2 / TILE_H)+1;		// end tile y

	// build a list of rects to test collision with
	std::vector<hgeRect> cols;
	std::vector<int> colidx;

	for (int iy=ty1; iy<ty2; iy++)
		for (int ix=tx1; ix<tx2; ix++)
			if (tiles[ix+(iy*info.mapwidth)])
				if (tiles[ix+(iy*info.mapwidth)]->collide || tiles[ix+(iy*info.mapwidth)]->collide2 || tiles[ix+(iy*info.mapwidth)]->collide3 || tiles[ix+(iy*info.mapwidth)]->collide4)
				{

					if (tiles[ix+(iy*info.mapwidth)]->collide3 || tiles[ix+(iy*info.mapwidth)]->collide4)
					{
						// if its an upwards slope

						hgeVector player_pos = hgeVector( r.x1 + ((r.x2 - r.x1)/2) , r.y2 );
						hgeRect tile_pos = hgeRect(ix*TILE_W, iy*TILE_H, (ix*TILE_W)+TILE_W, (iy*TILE_H)+TILE_H);

						if (tiles[ix+(iy*info.mapwidth)]->collide3)
						{
							if ( (player_pos.x - tile_pos.x1) > 40-(player_pos.y - tile_pos.y1) )
							{
								cols.push_back(hgeRect(ix*TILE_W, iy*TILE_H, (ix*TILE_W)+TILE_W, (iy*TILE_H)+TILE_H));
								colidx.push_back(ix+(iy*info.mapwidth));
							}
						}
						else
						{
							if ( (player_pos.x - tile_pos.x1) < (player_pos.y - tile_pos.y1) )
							{
								cols.push_back(hgeRect(ix*TILE_W, iy*TILE_H, (ix*TILE_W)+TILE_W, (iy*TILE_H)+TILE_H));
								colidx.push_back(ix+(iy*info.mapwidth));
							}
						}

					}
					else
					{
						cols.push_back(hgeRect(ix*TILE_W, iy*TILE_H, (ix*TILE_W)+TILE_W, (iy*TILE_H)+TILE_H));
						colidx.push_back(ix+(iy*info.mapwidth));
					}
				}


	
	// test for collisions
	for (int i=0; i<cols.size(); i++)
		if (cols[i].Intersect(&r))
		{
			plats[cPlat] = tiles[colidx[i]];
			cPlat ++;
		}

	*nCols = cPlat;

	// no collision was found	
	return false;
}

i understand that this function:


bool HGETileMap::PointCollide(float x, float y)
{
	if (x<0 || y<0) return false;

	// calculate which tile this is
	int tx = int(x / TILE_W);
	int ty = int(y / TILE_H);

	// tile index
	int idx = (tx)+((ty)*info.mapwidth);

	if (tiles[idx])
		return tiles[idx]->collide;

	return false;
}

is callled like this: tilemap->PointCollide(spriteX + mapX, spriteY + mapY)

But i dont understand what parameter are this: CTile *plats[9], int *nCols

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

Isn't there a built in rectangle class in c++?

If not, then just create an intersects function of a rectangle class that does a check when two rectangles from two different objects overlapped each other.

In your case, have your character be bounded inside a rectangle object. Have your tile be bounded by another rectangle object.

Make sure the rectangle are scaled relatively to the object's x and y position values, width and height.

The function your provided is unreadable to me because of the variable names. But hopefully my approach will help guide you in solving your problem.

Isn't there a built in rectangle class in c++?

If not, then just create an intersects function of a rectangle class that does a check when two rectangles from two different objects overlapped each other.

In your case, have your character be bounded inside a rectangle object. Have your tile be bounded by another rectangle object.

Make sure the rectangle are scaled relatively to the object's x and y position values, width and height.

The function your provided is unreadable to me because of the variable names. But hopefully my approach will help guide you in solving your problem.

Thank you. I will try that. Is not called bounding boxes collision?smile.png

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

There is no built-in rectangle class in C++, nor in the standard library. In fact I don't know any language with a built-in rectangle construct (even in languages like C# and Java rectangles would be provided by importing from a library, although in C# the library is part of the .NET runtime, so always available). Maybe a very high level language or a game maker style language has it built in.

There are a great deal of libraries which do offer rectangle-rectangle collision though. You want axis-aligned bounding box collision for simple rectangles. The code for that is pretty simple though, you could easily write it yourself, but if you plan on having more complicated physics in your game I'd definitely investigate a 2d physics library (the sooner you do the easier it will be to integrate into your code base).

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Isn't there a built in rectangle class in c++?

If not, then just create an intersects function of a rectangle class that does a check when two rectangles from two different objects overlapped each other.

In your case, have your character be bounded inside a rectangle object. Have your tile be bounded by another rectangle object.

Make sure the rectangle are scaled relatively to the object's x and y position values, width and height.

The function your provided is unreadable to me because of the variable names. But hopefully my approach will help guide you in solving your problem.

Thank you. I will try that. Is not called bounding boxes collision?smile.png

yes, it is commonly called that. But I decided to describe it to you rather than just throw jargon around.


yes, it is commonly called that. But I decided to describe it to you rather than just throw jargon around.

Just a thought: knowing what terms to search for (the jargon) helps one find answers.

I am searching now (search = learning/research) more about classes in C++ because I am not really confortable with them and i think i will use the built in bounding box function of HGE (Haaf's game engine). Thank you all for your comments.

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

This topic is closed to new replies.

Advertisement