Jump to content

  • Log In with Google      Sign In   
  • Create Account

Event in the 2D 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
4 replies to this topic

#1 FatManatee   Members   -  Reputation: 102

Like
0Likes
Like

Posted 20 June 2012 - 02:04 AM

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 ^^

Sponsor:

#2 AlanSmithee   Members   -  Reputation: 1064

Like
0Likes
Like

Posted 20 June 2012 - 05:32 AM

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 Posted Image

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, 20 June 2012 - 06:07 AM.


#3 FatManatee   Members   -  Reputation: 102

Like
0Likes
Like

Posted 20 June 2012 - 07:37 AM

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


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

#4 TechnoGoth   Crossbones+   -  Reputation: 2798

Like
0Likes
Like

Posted 20 June 2012 - 09:33 AM

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.


Tile
{
this.texture;
this.itemId;
this.eventId;
this.walkable;

}

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.

#5 FatManatee   Members   -  Reputation: 102

Like
0Likes
Like

Posted 20 June 2012 - 09:43 AM

True. Thanks all!
..and, tile size? I create tile 32x32. Better 16x16 pixel?




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