• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
bratiefanut

Collision, player sprite agains tile map

8 posts in this topic

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]

0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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 

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

 

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.

0

Share this post


Link to post
Share on other sites


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.

0

Share this post


Link to post
Share on other sites

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. 

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0