0
\$\begingroup\$

I am working on creating a 2D side-scrolling car game. My problem is generating the terrain. When creating the terrain, I need to have it random and recorded, so I can create collisions at a later point. I can't use any external assets.

var keys = [];

var keyPressed = function() {
    keys[keyCode] = true;
};

var keyReleased = function() {
    keys[keyCode] = false;
};

var car = function(locatio, accel, image, dragcoof) {
    this.loc = locatio;
    this.accel = accel;
    this.image = image;
    this.volocity = new PVector(0, 0);
    this.acceleration = new PVector(0, 0);
    this.drag = new PVector(0, 0);
    this.dragcoof = new PVector(dragcoof.x, dragcoof.y);
};

car.prototype.draw = function() {
    if (this.image === null) {
        fill(255, 0, 0);
        rect(this.loc.x, this.loc.y, 50, 10);
    } 
    else {
        this.image(this.loc.x, this.loc.y);
    }
};

car.prototype.update = function() {
    if (this.volocity.x >= 10) {
        this.volocity.x = 9.99;
    }

    if (this.volocity.x <= 0.1) {
        this.volocity.x = 0;
    }

    this.volocity.add(this.acceleration);
    this.loc.add(this.volocity);
    this.drag = PVector.mult(this.volocity, this.dragcoof);
    this.volocity.sub(this.drag);

    if (this.loc.x >= 600) {
        this.loc.x = 0;
    }
};

car.prototype.move = function(keycodeLeft, keycodeRight) {
    if (keys[RIGHT] && this.volocity.x < 10) {
        this.acceleration.add(this.accel.x,0);
    }

    if (!keys[RIGHT] && !keys[LEFT]) {
        this.acceleration.set(0, 0);
    }

    if (keys[LEFT] && this.volocity.x >= 0.5) {
        this.acceleration.set(-0.1, 0);
    }
};

var a = new car(new PVector(10, 200), new PVector(0.01,0), null, new PVector(0.022, 0));

var draw = function() {
    background(255, 255, 255);
    a.draw();
    a.update();
    a.move();
    text(a.acceleration + "\n" + a.volocity+"\n"+a.drag, 25, 25);
};
\$\endgroup\$
  • 3
    \$\begingroup\$ You've told us your problem.. What is your question? \$\endgroup\$ – Gnemlock Aug 23 '16 at 0:49
  • \$\begingroup\$ It's also worth pointing out that you spelt velocity wrong. It doesn't necessarily matter, but if you try to reference it later using the correct spelling, your gunna have a bad time. \$\endgroup\$ – Gnemlock Aug 23 '16 at 0:57
  • 2
    \$\begingroup\$ Terrain generation is a huge topic, so you'll need to narrow your question down a bit further for it to fit the StackExchange Q&A format. Consider including mock images of the kind of terrain output you want, and describe the algorithms you've considered/implemented so far, and what specifically you feel needs improvement. \$\endgroup\$ – DMGregory Aug 23 '16 at 5:25
  • \$\begingroup\$ Your question does not show any effort. What have you tried? Take a look at what's been asked before. \$\endgroup\$ – Alexandre Vaillancourt Aug 23 '16 at 11:40
1
\$\begingroup\$

Generally for generating terrain(either 2D or 3D), you need a base "random" factor, something you can base your terrain of. There are several nice algorithms and tutorials for generating those, and with some arithmetic calculations you can easily customize them for you own needs. Some examples are the Perlin noise or the Simplex noise. Do a bit of research on those, experiment with parameters and you're good to go.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.