Skip to main content
converted url to linked text; removed comments from code to make more minimal
Source Link

https://www.khanacademy.org/computer-programming/car-physics-thingy/5378059343 I am working on creating a game that is a 2d side scrolling car gameI 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. II can't use any externals or anything so yeahexternal assets.

/**
@TODO:
Work on collisions with curves and stuff
Create the physics for driving/hills and stuff

*/



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.power = power;
    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) {
        //println("please put a nice image but I will supply a simple box for testing");
        //pushMatrix();
        fill(255, 0, 0);
        rect(this.loc.x, this.loc.y, 50, 10);
       } //popMatrix();
    } else {
        this.image(this.loc.x, this.loc.y);
    }
}; 

car.prototype.update = function() {
    //this.momentum = this.volocity.mult(this.mass);
    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.volocity.add(-this.drag);
    //  this.acceleration.sub(this.drag);
    this.loc.add(this.volocity);
    //  this.drag = this.volocity.mult(this.dragcoof);
    this.drag = PVector.mult(this.volocity, this.dragcoof);
    this.volocity.sub(this.drag); 

    if (this.loc.x >= 600) {
        this.loc.x = 0;
        // println(this.volocity);

    }
    //this.
}; 

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);
    }
 
    //if(this.acceleration.x<0){
    //    this.acceleration.x = 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);
    


};

https://www.khanacademy.org/computer-programming/car-physics-thingy/5378059343 I am working on creating a game that is 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 externals or anything so yeah.

/**
@TODO:
Work on collisions with curves and stuff
Create the physics for driving/hills and stuff

*/



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.power = power;
    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) {
        //println("please put a nice image but I will supply a simple box for testing");
        //pushMatrix();
        fill(255, 0, 0);
        rect(this.loc.x, this.loc.y, 50, 10);
        //popMatrix();
    } else {
        this.image(this.loc.x, this.loc.y);
    }
};
car.prototype.update = function() {
    //this.momentum = this.volocity.mult(this.mass);
    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.volocity.add(-this.drag);
    //  this.acceleration.sub(this.drag);
    this.loc.add(this.volocity);
    //  this.drag = this.volocity.mult(this.dragcoof);
    this.drag = PVector.mult(this.volocity, this.dragcoof);
    this.volocity.sub(this.drag);
    if (this.loc.x >= 600) {
        this.loc.x = 0;
        // println(this.volocity);

    }
    //this.
};
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);
    }
 
    //if(this.acceleration.x<0){
    //    this.acceleration.x = 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);
    


};

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);
};
added 2666 characters in body; edited title
Source Link

Colliding with Creating terrain

https://www.khanacademy.org/computer-programming/car-physics-thingy/5378059343 I am working on creating a game that is a 2d side scrolling car game. My problem is 2 thingsgenerating the terrain. The collisions of my cars onWhen creating the terrain and getting them to react correctly (I don't even know howI need to start)have it random and generating the terrain.recorded so I can create collisions at a later point. I can't use any externals or anything so yeah.

/**
@TODO:
Work on collisions with curves and stuff
Create the physics for driving/hills and stuff

*/



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.power = power;
    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) {
        //println("please put a nice image but I will supply a simple box for testing");
        //pushMatrix();
        fill(255, 0, 0);
        rect(this.loc.x, this.loc.y, 50, 10);
        //popMatrix();
    } else {
        this.image(this.loc.x, this.loc.y);
    }
};
car.prototype.update = function() {
    //this.momentum = this.volocity.mult(this.mass);
    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.volocity.add(-this.drag);
    //  this.acceleration.sub(this.drag);
    this.loc.add(this.volocity);
    //  this.drag = this.volocity.mult(this.dragcoof);
    this.drag = PVector.mult(this.volocity, this.dragcoof);
    this.volocity.sub(this.drag);
    if (this.loc.x >= 600) {
        this.loc.x = 0;
        // println(this.volocity);

    }
    //this.
};
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);
    }

    //if(this.acceleration.x<0){
    //    this.acceleration.x = 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);
    


};

Colliding with terrain

https://www.khanacademy.org/computer-programming/car-physics-thingy/5378059343 I am working on creating a game that is a 2d side scrolling car game. My problem is 2 things. The collisions of my cars on the terrain and getting them to react correctly (I don't even know how to start) and generating the terrain. I can't use any externals or anything so yeah.

Creating terrain

https://www.khanacademy.org/computer-programming/car-physics-thingy/5378059343 I am working on creating a game that is 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 externals or anything so yeah.

/**
@TODO:
Work on collisions with curves and stuff
Create the physics for driving/hills and stuff

*/



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.power = power;
    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) {
        //println("please put a nice image but I will supply a simple box for testing");
        //pushMatrix();
        fill(255, 0, 0);
        rect(this.loc.x, this.loc.y, 50, 10);
        //popMatrix();
    } else {
        this.image(this.loc.x, this.loc.y);
    }
};
car.prototype.update = function() {
    //this.momentum = this.volocity.mult(this.mass);
    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.volocity.add(-this.drag);
    //  this.acceleration.sub(this.drag);
    this.loc.add(this.volocity);
    //  this.drag = this.volocity.mult(this.dragcoof);
    this.drag = PVector.mult(this.volocity, this.dragcoof);
    this.volocity.sub(this.drag);
    if (this.loc.x >= 600) {
        this.loc.x = 0;
        // println(this.volocity);

    }
    //this.
};
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);
    }

    //if(this.acceleration.x<0){
    //    this.acceleration.x = 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);
    


};
Source Link

Colliding with terrain

https://www.khanacademy.org/computer-programming/car-physics-thingy/5378059343 I am working on creating a game that is a 2d side scrolling car game. My problem is 2 things. The collisions of my cars on the terrain and getting them to react correctly (I don't even know how to start) and generating the terrain. I can't use any externals or anything so yeah.