canvas-boilerplate icon indicating copy to clipboard operation
canvas-boilerplate copied to clipboard

Square problem

Open spider4216 opened this issue 4 years ago • 1 comments

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 :(

out

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

spider4216 avatar May 12 '20 15:05 spider4216

To understand what is going on, I simulate this problem on paper by hand

photo_2020-05-13_08-46-40

In this case everything is okay, the distance between 2 dots is - 1 cm.

photo_2020-05-13_08-46-39

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

spider4216 avatar May 13 '20 02:05 spider4216