Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Square problem #11

Open
spider4216 opened this issue May 12, 2020 · 1 comment
Open

Square problem #11

spider4216 opened this issue May 12, 2020 · 1 comment

Comments

@spider4216
Copy link

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant