1
\$\begingroup\$

I am making a basic game. I currently have an object (box4) that moves left and right using Keycode Events. But I want to add Gravity. I'm not sure how to go about this; everything I've found online isn't directly implementable verbatim in my game. Usually because it's entirely inline and isn't object-oriented whatsoever (I'm using classes/inheritance), but also because I'm a newbie.

Here's what my code looks like:

HTML:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" href="style.css" />
</head>
<body>
    <canvas id="canvas" width="600" height="600"></canvas>
    <script src="sprites/box.js"></script>
    <script src="sprites/box2.js"></script>
    <script src="sprites/box3.js"></script>
    <script src="sprites/box4.js"></script>
    <script src="sprites/arrow.js"></script>
    <script>
        window.onload = function () {
            //get the canvas
            var canvas = document.getElementById('canvas'),
                //get the context
                context = canvas.getContext('2d'),
                //create an arrow object
                arrow = new Arrow(),
                //create a box object
                box = new Box();
                box2 = new Box2();
                box3 = new Box3();
                box4 = new Box4();
                var gravity = 0.5;
            //position the box at 80,40 on the canvas
            box.X = 80;
            box.Y = 40;
            box2.X = 80;
            box2.Y = 40;
            box3.X = 150;
            box3.Y = 150;
            box4.X = 200;
            box4.Y = 200;
            //position the arrow at 300,300 (middle of the canvas)
            arrow.X = 300;
            arrow.Y = 300;

            //draw the first frame
            drawFrame();

            //fucntion for drawing the frames
            function drawFrame() {
                //clear the canvas
                context.clearRect(0, 0, canvas.width, canvas.height);
                //increase the y posn by the y velocity
                box.Y += box.VY;
                //increase the x posn by the x velocity
                box.X += box.VX;
                //increase the y posn by the y velocity
                box2.Y += box2.VY;
                //increase the x posn by the x velocity
                box2.X += box2.VX;
                //increase the y posn by the y velocity
                box3.Y += box3.VY;
                //increase the x posn by the x velocity
                box3.X += box3.VX;

                window.onkeydown = function (e) {
                    var code = e.keyCode ? e.keyCode : e.which;
                    if (code === 37) { //left key
                        box4.X -= box4.VX;
                    } else if (code === 38) {
                        box4.Y -= 20;
                    } else if (code === 39) { //right key
                        box4.X += box4.VX;
                    } else if (code === 40) {
                        box4.Y += 20;
                    } else if (code === 32) {
                        box4.Y -= 20;
                    }
                };

                //if the box hits the canvas top bottom edge
                if (box.Y - box.Size < 0 | box.Y + box.Size > canvas.height) {
                    //reverse the y velocity
                    box.VY = -box.VY;
                }
                //if the box hits the canvas left or right
                if (box.X - box.Size < 0 | box.X + box.Size > canvas.width) {
                    //reverse the x velocity
                    box.VX = -box.VX;
                }
                if (box2.Y - box2.Size < 0 | box2.Y + box2.Size > canvas.height) {
                    box2.VY = -box2.VY;
                }
                if (box2.X - box2.Size < 0 | box2.X + box2.Size > canvas.width) {
                    box2.VX = -box2.VX;
                }
                if (box3.Y - box3.Size < 0 | box3.Y + box3.Size > canvas.height) {
                    box3.VY = -box3.VY;
                }
                if (box3.X - box3.Size < 0 | box3.X + box3.Size > canvas.width) {
                    box3.VX = -box3.VX;
                }
                //calculate the difference between the box x position and the arrow x position
                var dx = box.X - arrow.X;
                //calculate the difference between the box y position and the arrow y position
                var dy = box.Y - arrow.Y;
                //set the angle of rotation for the arrow
                arrow.Rotation = Math.atan2(dy, dx);
                //draw the box
                box.draw(context);
                box2.draw(context);
                box3.draw(context);
                box4.draw(context);
                //request the next frame
                window.requestAnimationFrame(drawFrame, canvas);
            }
        }
    </script>
</body>
</html>

Box4 class:

function Box4() {
    //private data members
    //x posn
    var x = 0,
    //y posn
    y = 0,
    //colour
    colour = "red",
    //size
    size = 20,
    //x velocity
    vx = 4,
    //y velocity
    vy = 4;

    //public property for VX
    Object.defineProperty(this, 'VX',
    {
        get: function () {
            return vx;
        },
        set: function (value) {
            vx = value;
        }
    }
    )

    //public property for VY
    Object.defineProperty(this, 'VY',
    {
        get: function () {
            return vy;
        },
        set: function (value) {
            vy = value;
        }
    }
    )

    //public property for size
    Object.defineProperty(this, 'Size',
    {
        get: function () {
            return size;
        },
        set: function (value) {
            size = value;
        }
    }
    )

    //public property for X
    Object.defineProperty(this, 'X',
    {
        get: function () {
            return x;
        },
        set: function (value) {
            x = value;
        }
    }
    )

    //public property for Y
    Object.defineProperty(this, 'Y',
    {
        get: function () {
            return y;
        },
        set: function (value) {
            y = value;
        }
    }
    )

    //function public draw method
    Box4.prototype.draw = function (context) {
        //save the context
        context.save();
        //set x and y
        context.translate(x, y);
        //set the line width
        context.lineWidth = 2;
        //set the colour of the fill
        context.fillStyle = colour;
        //begin the path
        context.beginPath();
        //draw the box
        context.moveTo(-size, -size);
        context.lineTo(-size, size);
        context.lineTo(size, size);
        context.lineTo(size, -size);
        //close the path
        context.closePath();
        //fill the shape
        context.fill();
        //draw it
        context.stroke();
        //restore the context
        context.restore();
    };

}

My program to download: http://s000.tinyupload.com/index.php?file_id=87972197214078945115

Does anyone know any workarounds that would work easily with my current code and code structure?

\$\endgroup\$
2
  • \$\begingroup\$ Well, normally you should just implement a velocity change (i.e. acceleration) in the negative y direction during your main loop... For reference: Earth gravity accelerates a body by about 9.81 m/s^2 \$\endgroup\$ Commented Nov 11, 2017 at 13:16
  • 2
    \$\begingroup\$ It looks like you have all the building blocks for a simple physics simulation: objects with position, velocity, and size properties, and a game loop where you modify velocities & positions prior to a display update. This looks like it should match up pretty much exactly with most beginner tutorials on the subject. But you say you haven't found anything online that matches up? It might help if you include a sample of a guide you're trying to follow, and highlight where you're running into trouble with it, so we can target our explanation toward this specific issue or gap. \$\endgroup\$ Commented Nov 11, 2017 at 13:51

0

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.