Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Tile based game objects/collisions still confused :(

This topic is 6431 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Let's say you want monsters in your game
Make a class called CMonster (For example) that will control the monster.
You can have all of your monsters in one big linked list (Better this way) or if you need a more constant size, use a normal array
Now you would make a function, Let's call it: ControlMonsters()
The function will go through your linked list/array and see which monsters needs to be processed (Or something like that).
The function will, basically, move them and make them think (Or seem like it ).
Now you make another function: CheckCollisionMonster(int x,int y)
This function will check for collision with any of the monsters.
You can use a "regular" bounding-box collision detection (Easiest to do, very useful for side-scrollers) or some other methods.
Goodluck with your game!

EDIT: Oops, forgot about collision with the tiles
You need to have an array that will hold collision information with your tiles.
So when you do CheckCollision(int x,int y), CheckCollision() will check, according to your global position in the map, what tile your currently on and if that tile have collision "turned on".
In my editor, I did it so you can have collision information for 4 sides: Up, Down, Left and Right.
Later, in the engine, I call CheckColl() that checks my position, then figures out if I'm on the edge of a tile (Like, left edge, right edge, etc) and returns true or false if the tile has collision in that direction.
Hope I made myself clear


- Goblineye Entertainment
The road to success is always under construction

Edited by - Tornado on October 7, 2000 4:25:53 AM

Share this post


Link to post
Share on other sites
to expand on what tornado said , you might have a struct for a sprite that contains attributes, width, height, state, etc..

you would just run a loop and check for collision with each of your enemies in the linked list, and if you detect collision, do something or other... make the player die, play a sound, whatever.

as for tile collision, im still implementing this myself , but its basically the same as the monster collision, you just run a loop and check if one side of the player collides with a side of the tile, and if that tiles properties are collidable for that side, stop moving the player, else move the player in the direction he wants to go

heres my collision function:

    
int CSprite::Collision(CSprite *Sprite)
{
if(this->YPos + this->Height < Sprite->YPos)
return 0; //no collision

if(this->YPos > Sprite->YPos + Sprite->Height)
return 0; //no collision


if(this->XPos + this->Width < Sprite->XPos )
return 0; //no collision

if(this->XPos > Sprite->XPos + Sprite->Width )
return 0; //no collision


return 1; //collision! (:
}

Share this post


Link to post
Share on other sites
THX a lot! How do I find out which tile my player''s x is on? How do I scan the tiles ahead of the player to find out if the collision info is on or off??


THX again! Code would be nice too thx!

Share this post


Link to post
Share on other sites
To check which tile your on, let's assume that the tile's size is 32x32, the map's size is 64x64 (In tiles, of course )
Also, The resolution is 800x600.
Now let's assume that x and y is the current map position.
x and y ranges from 0->799,0->599
Finally (), Let's assume that m_x and m_y is where we want to check for collision (They present "local" positions - according to the current screen, not the entire map)
And now for some code:

        

// Let's define the structure that holds collision information for a tile

struct TILE
{
bool Collision_Top;
bool Collision_Bottom;
bool Collision_Left;
bool Collision_Right;
};

TILE OurTiles[64][64];

// Fill in info in OurTiles...


int posx = m_x+x-800; // We find the global position

int posy = m_y+y-600;
int tile_x = posx / 32; // let's find the tile we're standing on

int tile_y = posy / 32;
int off_x = posx & 31; // The offset of the tile we're currently on - same as posx % 32

int off_y = posy & 31;

if (off_x == 0) // we're on the edge of a tile, now we want to find if it has collision in this direction (Must be right collision of the tile on the left or left collision of the tile on the right)
{
if ((OurTiles[tile_x-1][tile_y].Collision_Right) || (OurTiles[tile_x][tile_y].Collision_Left))
return(true); // we have collision

}

if (off_y == 0) // edge of a tile, up or down, up of the bottom tile or down of the topper tile
{
if ((OurTiles[tile_x][tile_y-1].Collision_Bottom) || (OurTiles[tile_x][tile_y].Collision_Top))
return(true); // collision!

}
return(false); // otherwise, no collision



Sorry for the long message

Edited by - Tornado on October 7, 2000 1:37:42 PM

Share this post


Link to post
Share on other sites
Well, you have to be keeping track of your player's position or you wouldn't know where to scroll the map to^^ For the tile collision, just do like
if(mapColData[player.x + 1)][player.y] == false)
{
//move player
}

That's using an array of bools called mapColData to keep track of which tiles you can't walk on (where true=can't walk on, false=can), and assuming you're trying move right (just use player.x-1 for left, player.y+1 for down, player.y-1 for up). Or you could make an obstruction class like
class obs
{
public:
bool top;
bool bottom;
bool left;
bool right;
};
if you wanna do like Tornado and have it where you can make it so it'll only block you if you run into it from a certain direction.



-Deku-chan

DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)

"I'm dropping like flies!" - me, playing Super Smash Bros. (and losing)
"What fun!" - me, playing Super Smash Bros. (and beating the crap out of somebody)

Edited by - DekuTree64 on October 7, 2000 1:41:59 PM

Share this post


Link to post
Share on other sites
Yeah, DekuTree has a point there
You can make an array of bool that will hold collision info for every pixel on the screen.

Share this post


Link to post
Share on other sites
int posx = m_x+x-800; // We find the global position
int posy = m_y+y-600;

What does that mean? What is the m_x and the y? can u explain carefully what the mapx globalx etc means?

Share this post


Link to post
Share on other sites
Sure
The global position is like the "master" position on the screen.
Let''s say the map size is (In pixels) - 5000x6000
A global position can be 0->4999 in the X axis or 0->5999 in the Y axis.
Now, when you move your character across the screen, you have the local position.
If the resolution is 800x600, a local position can be 0->799 (X) or 0->599 (Y).
So in this code:

int posx = m_x+x-800;
int posy = m_y+y-600;

m_x and m_y is the local position on the screen (0->799, 0->599) that is passed to the function.
x and y is the current position on the map.
I forgot about one thing though, x and y, in this case, ranges from 800->4999 and 600->5999.
I took it from some code I wrote a while ago
So, maybe this code makes more sense:

int posx = m_x + x;
int posy = m_y + y;

Now x and y ranges from 0->5000-800 and 0->6000-600
Sorry for the confusion
Hope it makes more sense now

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!