• Advertisement
Sign in to follow this  

Event in the 2D Tile Map

This topic is 2044 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

Hi, i'm a beginner and i try to build a 2D Game using <canvas> element and JavaScript.
I create a map and a character.. now, i use the keyboard to move a character in the map, but i have many question.

What is the theory for collisions and event?

I build a map so:


var map = {
[0, 0, 0],
[0, 1, 1],
[0, 0, 1]
}


For example, 0 is water, and 1 is grass. My class render the canvas with this matrix, associating the values with the images.
The problem is: my character, walking in the water!

Should I create a new matrix? For example:

var map = {
[{0, event}, {0, event}, {0, event}],
[{0, event}, {1, event}, {1, event}],
[{0, event}, {0, event}, {1, event}]
}


...is the correct way?
I don't find any tutorial for solve this problem!

Thanks ^^

Share this post


Link to post
Share on other sites
Advertisement
AABB collision detection is pretty straight forward.

You just check if object A is "inside" of object B, and if so, take according action.
What you want to be careful of though is to not iterate through more tiles then you need to. It's a waste of RAM and not needed, you only want to check vs the adjuncent tiles, in other words, only the tiles that your character have any chance of colliding with.

Something like:
var startx = entity.x / tilewidth;
var endx = entity.x / tilewidth + entity.w;
var starty = entity.y / tileheight;
var endy = entity.y / tileheight + entity.h;

for (var y = starty; y <= endy; y++)
{
for (var x = starx; x <= endx; x++)
{
if (map[y][x].type === collidable)
{
// do something fun :-)
}
}
}


I am at work now but this psuedocode should hopefully get you started smile.png

Edit* also, I'm unsure of what events you are reffering to outside of collisiondetection but you should be able to invoke any event you want in the loop. Also you can adjust start and end x/y choords if need be. Edited by AlanSmithee

Share this post


Link to post
Share on other sites
Sure, it's simple. ^^

Next question, i create a class Tile and class Item.

Tile {
this.with;
this.height;
this.item = new item;
}
Item {
this.with;
this.height;
this.x;
this.y;
}


So, collidable == true if the next position of my character is in the item's area.

Example:
The tile represents grass with one rock; the rock is a Item().


Collidable == true if:

Item(x) <= EnityNextPosition(x) < item(x) + item.width
&&
Item(y) <= EnityNextPosition(y) < item(y) + item.height
[/quote]

This is my general idea for manage all events.
The question is: is a good idea? Or exist better solutions? I'm afraid of wasting a lot of ram!

I'm at work too, sorry for my bad post. XD

Share this post


Link to post
Share on other sites
If it's your first project one simple approach you could take is having the tile contain all the relevant information, with id's for events and items.


[color=#660066]Tile[color=#000000]
[color=#666600]{
[color=#000000] this.texture;
this.itemId;
this.eventId;
this.walkable;
[color=#666600]}

[color=#666600]Then you code it so that when trying to move to a tile that isn't walkable the game prevents you. When entering a title if it has a valid eventId it calls that event from a global event list. Like wise if there is an item in that tile you can pick it up moving from the tile into the characters inventory. I wouldn't worry about having sizes in there for now and just make everything 1 tile in size. Larger objects and areas can be made by having parts assigned to different tiles. so a lake might be made up for 5 tiles, but each tile only knows about itself.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement