As per the title, I'm trying to figure out the best way to detect collision with tiles. I came up with two ideas.
The first is have the main loop always be calling map.get for wherever the character is. and then something like this:
if(map.get(characterXRight, characterYbottom) != null){
if ((map.get(characterX, characterYbottom).sprite == '3') || (map.get(characterXRight, characterYbottom).sprite == '3')) {
//console.log("hello")
if (btn.down){
character.y -= 1
}
}
}
(something like that for all 4 directions.)
The second idea is to use map.find and make an array of all the tiles. Like:
cars = [...map.find(3)];
Which find all tiles of id 3 and add them to the array cars. After that, loop through the array
var i;
for (i = 0; i < cars.length; i++) {
if (AABBcollision(mario, cars[i])==true){
print("Hello, Mario", 12, 10);
}
function AABBcollision(rect1, rect2) {
print(rect2.x * 8, 50,70)
print(rect2.y * 8, 50,80)
print(rect1.x, 40,70)
print(rect1.y, 40,80)
if (rect1.x < (rect2.x * 8) + (rect2.w) &&
rect1.x + rect1.w > (rect2.x * 8) &&
rect1.y < (rect2.y * 8) + (rect2.h) &&
rect1.y + rect1.h > (rect2.y * 8)) {
return true
}
The problem wth this though is that the tilemap returns x.y coordinates based on the tilemap I think I need to multiple by the tile width/height to do the correctly.
I still havent gotten either to work though, so any help would be greatly appreciated.