I am creating a simple JS canvas tile based game, I know it needs refactoring and updating of the syntax which I plan to do when I understand more about what is going on.
I have created the tile sizes dynamically which I think is why I am struggling. The collision detection isn't precise, and I am stumped as to making it more precise... By precise, I mean depending on the size of the display the collision is not hitting the right wall but just before it or after it for example.
Any help/advice, or pointing in the right direction, would be greatly appreciated.
this.moveUp = function(){
if(this.y > gameCanvas.height / 8){
this.y -= 7;
}
};
this.moveDown = function(){
if(this.y < gameCanvas.height - map.tSizeY * 1.5){
this.y += 7;
}
};
this.moveLeft = function(){
if(this.x > gameCanvas.width / 8){
this.x -= 7;
}
};
this.moveRight = function(){
if(this.x < gameCanvas.width - map.tSizeX * 1.25){
this.x += 7;
}
}
Github Repo: https://github.com/MrWalshy/jsLameTileGame

Set the players position touching the wall boundary rather than move the position by an arbitrary amount to an unknown position relative to the wall.
You also need to include the size of the player in the calculations.
I will assume that
map.tSizeXandmap.tSizeYare the tile sizes and that the player moves by a amount calledthis.speed(which I have added in example) and that the player has a sizethis.sizeX,this.sizeY(which I have also added in the example below)Example
Move the player then resolve the collisions