Jump to content

  • Log In with Google      Sign In   
  • Create Account


Collision, player sprite agains tile map


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Vidar son of Odin   Members   -  Reputation: 1320

Like
0Likes
Like

Posted 05 October 2013 - 11:18 AM

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.

tile map test.PNG


Check out my portfolio : https://bratie.wordpress.com

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

 


Sponsor:

#2 Shane C   Crossbones+   -  Reputation: 1103

Like
0Likes
Like

Posted 05 October 2013 - 01:37 PM

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.

#3 Vidar son of Odin   Members   -  Reputation: 1320

Like
0Likes
Like

Posted 06 October 2013 - 02:47 AM

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 


Check out my portfolio : https://bratie.wordpress.com

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

 


#4 warnexus   Prime Members   -  Reputation: 1385

Like
0Likes
Like

Posted 07 October 2013 - 10:51 AM

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.


Edited by warnexus, 07 October 2013 - 10:53 AM.


#5 Vidar son of Odin   Members   -  Reputation: 1320

Like
0Likes
Like

Posted 08 October 2013 - 08:08 AM

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


Check out my portfolio : https://bratie.wordpress.com

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

 


#6 Paradigm Shifter   Crossbones+   -  Reputation: 5153

Like
0Likes
Like

Posted 08 October 2013 - 08:26 AM

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).


Edited by Paradigm Shifter, 08 October 2013 - 08:27 AM.

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

#7 warnexus   Prime Members   -  Reputation: 1385

Like
0Likes
Like

Posted 08 October 2013 - 08:19 PM

 

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.



#8 Dragonsoulj   Crossbones+   -  Reputation: 2009

Like
0Likes
Like

Posted 08 October 2013 - 10:13 PM


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.



#9 Vidar son of Odin   Members   -  Reputation: 1320

Like
0Likes
Like

Posted 09 October 2013 - 05:55 AM

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. 


Check out my portfolio : https://bratie.wordpress.com

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

 





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS