0

I want to create collision between two different objects, but I have no idea how I could make it work, since I have multiple arrays, with multiple objects with different height and width. What is the best way to create collision? I'll put some of my code you can see a bit of my code.

I have this Class player:

function Player() {
    this.speed = 10;
    this.height = 180;
    this.width = 150;
    this.pos_y = 425;
    this.pos_x = CENTER;
    this.player_image = new Image();
    this.player_image.src = 'img/car_middle.png';
}

And the class car:

function Car(lane) {
    this.lane = lane;
    this.height = 108.75;
    this.width = 99;
    this.pos_x = CENTER;
    this.pos_y = -350;
    this.car_image = car_image[this.lane-1];
}

Car movement method:

Car.prototype.move = function(){
    this.height += ZOOM_RATE;
    this.width += ZOOM_RATE;

        if (this.lane == 1) {
            this.pos_x -= SIDES_RATE;
        } else if (this.lane == 2) {
            this.pos_x -= MIDSIDE_RATE;
        } else if (this.lane == 3) {
            this.pos_x += MIDSIDE_RATE;
        } else if (this.lane == 4) {
            this.pos_x += SIDES_RATE;
        }

    this.pos_y += 3;
};

I move the player with the arrow keys, and the objects have a fixed diagonal movement, depending on the lane where the object is.

Carlos Menzoni
  • 223
  • 1
  • 4
  • 17
  • Check out this answer. I'm sure you'll be able to convert between (x,y,width,height) and (top,left,bottom,right). http://stackoverflow.com/a/2752387/2727710 – kamoroso94 Aug 02 '16 at 01:36
  • There are many collision test having varying degrees of accuracy. The simplest, fastest & least accurate is to put a bounding box around each game-piece and test if those bounding boxes are colliding. – markE Aug 02 '16 at 04:09
  • This is a 2D game and cars are sprites?. Looking at code there is no perspective calculations. This will make is difficult to get a good collision test. Eg blue cars roof covers the road in front for a hit there would be overlap first then hit.. You need to modify the sim to make it internally 2/3D ( ignoring up off the road). The sprite size are calculated from the 3D pos. All to much for one answer. So best is circle /circle collide `if((c1.x-c2.x) * (c1.x-c2.x) + (c1.y-c2.y) * (c1.y-c2.y) < Math.pow(c1.size *c1.zoom + c2.size * c2.zoom)){ // then hit` c1,c2 are cars – Blindman67 Aug 02 '16 at 14:53
  • @Blindman67 Do you know where I could find more information about this 2/3D simulation? I'd love to adapt that to my game. But if I'm not able, I'll definitely put circle collision. Thanks! – Carlos Menzoni Aug 02 '16 at 15:00
  • All I know is in my head, so can not point you to a resource. If you can solve right triangles you can do the math. From the side 3D has a triangle from the eye through the screen to the top of the object, down to the center then back to the eye. The triangle from eye to screen is a similar triangle from eye to object. So size of object on screen is (screen distance from eye / object distance from eye) * object size. Use real number helps (screen 14") toy cars 5-10" screen 2"feet from eye cars somewhere behind screen. BTW error in last post change `c2.zoom)` to `c2.zoom),2` – Blindman67 Aug 02 '16 at 15:18

0 Answers0