Jakub Menšík
Jakub Menšík

Reputation: 174

Two dimensional elastic collision of squars

I am creating mobile game in two dimensional space with only squares. At some point 2 squares will collide together and a want make an elastic collision. That means i need to calculate new velocity for X axis, velocity for Y axis.

There is an image of colliding example

Here is my class.

class Block {
constructor(x, y, vx, vy, e, m){
    this.x = x;
    this.y = y;
    this.vx = vx;
    this.vy = vy;
    this.edgeSize = e;
    this.mass = m;
   } 
}

I already achive one dimension elastic collision with this function

    bounce = (other) =>((this.mass-other.mass)/(this.mass+other.mass))*this.vx + ((2*other.mass)/(this.mass+other.mass))*other.vx;

I try to modify this function and implement Y axis but nothing work so far.

Also i find this equation at wiki but i dont understand which atrributes should i pass in that equation. enter image description here

Can you anyone please help me achive this collison or at least point me torwards now i am just stuck for 3 days?

Upvotes: 1

Views: 320

Answers (1)

Prakhar Londhe
Prakhar Londhe

Reputation: 1471

According to physical laws, the interaction of two bodies can be seperated on the axes. That is, you can compute the final velocities seperately for both the axes, and you will have the correct velocity.

Upvotes: 1

Related Questions