Hello to all who are reading this post In trying to make Mario type of game but I have some collision bug maybe someone can explain to me whats is incorrect and how to fix it
Collision part there while be hard coded value I can't jump on box correctly is hard to explain if it's not hard just cache js fiddle link!
controls on arrow's space jump the issues is with jumping
And here while be a full code to look at or play whit it on js fiddle
var hero = {
X: 200,
Y: 450,
gravity: 0.05,
gravitySpeed: 0,
speed: 2,
height: 50,
width: 50
}
var box = {
X: 300,
Y: 450,
height: 50,
width: 50
}
var map = {
gravity: 0.5,
groundY: 350
}
var optiones = {
crashWith: function () {
var myleft = hero.X;
var myright = hero.X + (50);
var mytop = hero.Y;
var mybottom = hero.Y + (50);
var otherleft = box.X;
var otherright = box.X + (50);
var othertop = otherobj.y;
var otherbottom = box.Y + (50);
var crash = true;
//Hard coded values
var Y = 450,
X = 300,
w = 50,
h = 50
// not tuching
if ((mybottom < othertop) ||
(mytop > otherbottom) ||
(myright < otherleft) ||
(myleft > otherright)) {
crash = false;
// console.log("dont tuch")
hero.Y += hero.gravitySpeed;
hero.gravitySpeed += hero.gravity;
} else {
// collision rules
if (mytop >= othertop && othertop < otherbottom) {
if (hero.X <= X) {
hero.X = X - hero.width
} else if (hero.X > X) {
hero.X = X + hero.width
}
} else if (hero.X >= X && hero.X <= X + w) {
if (hero.Y <= Y) {
hero.Y = Y - hero.height
} else if (hero.Y >= Y) {
hero.Y = Y + hero.height
}
}
}