View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

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

8 replies to this topic

### #1Mr. Lawliet  Members

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.

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

### #2Shane C  Members

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.

### #3Mr. Lawliet  Members

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

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

### #4warnexus  Prime Members

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.

### #5Mr. Lawliet  Members

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?

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

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

### #7warnexus  Prime Members

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?

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

### #8Dragonsoulj  Members

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.

### #9Mr. Lawliet  Members

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.

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