canvas-boilerplate
canvas-boilerplate copied to clipboard
Square problem
I ran into a problem with square shape. Euclidean distance formula works but not as I expected. When one square touch the corner of other suqare - collision does not happened :(
Code Example:
collision()
{
// this.collisions - many objects on scene (1 in this example)
// this one current object
for (var index in this.collisions) {
if (this == this.collisions[index]) continue;
if (this.distance(this.x, this.y, this.collisions[index].x, this.collisions[index].y) < this.width) {
console.log('sense');
break;
}
}
}
distance(x1, y1, x2, y2) {
const xDist = x2 - x1
const yDist = y2 - y1
return Math.sqrt(Math.pow(xDist, 2) + Math.pow(yDist, 2));
}
Can you explain why and how to solve it, please? Thank you
To understand what is going on, I simulate this problem on paper by hand
In this case everything is okay, the distance between 2 dots is - 1 cm.
In this case the distance is almost 1.5
It's clear but I don't understand what should I do to fix this problem