-1
\$\begingroup\$

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


        }

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

Do this on line 103

hero.jumping = false;

And change the conditional on line 100 to this:

hero.X + w >= X && hero.X <= X + w

There are many other things wrong with your code though...

\$\endgroup\$
1
\$\begingroup\$
(mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)

This will always be true, no matter what, because an object is alway either to the left from the right side of another, or to the right from the left side of the same object, etc.. You need to change the ORs to ANDs.

\$\endgroup\$
5
  • \$\begingroup\$ oky i changed but it stil is bugging \$\endgroup\$ Commented Nov 9, 2016 at 17:56
  • \$\begingroup\$ @Guntars well, because you never mentioned how it is wrong, or what did you try to solve it, I could only assume what's the problem. \$\endgroup\$
    – Bálint
    Commented Nov 9, 2016 at 17:58
  • \$\begingroup\$ sory for that im new in javascript it's hard to exaplain its like mario game you cant go thro blue box you can only jump on it \$\endgroup\$ Commented Nov 9, 2016 at 18:02
  • 1
    \$\begingroup\$ I have no problems with your knowledge of JS, but you should use proper grammar if you can speak english (use your and you're correctly, capitalize the "I", use aphostropes, etc.) and try to post both what you want (e.g. the red box should be able to stand on the blue box) and what happens currently (e.g. the red block doesn't collide with the blue one, the red block teleports to a seemingly random location, the red block gets frozen in place) in your answer \$\endgroup\$
    – Bálint
    Commented Nov 9, 2016 at 18:14
  • \$\begingroup\$ thank you for helping me with english i while re write a question and ask it agen \$\endgroup\$ Commented Nov 9, 2016 at 18:18

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.