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

isContaining() utility function might be a nice addition #130

Open
michael-dean-haynie opened this issue Dec 19, 2024 · 0 comments
Open

Comments

@michael-dean-haynie
Copy link

Below is an example of my own utility I ended up making which might be useful to others.
Perhaps the exact behavior I needed is not important, but the isOverlapping() helper wouldn't work for me.

I did JUST notice the isIntersecting() helper though ... maybe that's good enough. I could just specify the same position for the start and end parameters...

/**
 * Checks if a position is inside a rect.
 * Important that it doesn't generously accept points on the far edges.
 * This is so that engine objects can belong to exactly 1 sector, even though they may overlap multiple.
 * So points will match on the lower end, but not the upper end.
 */
export function posInRect(pos: Vector2, rectPos: Vector2, rectSize: Vector2) {
	const bottomLeft = rectPos.subtract(rectSize.scale(0.5));
	const topRight = rectPos.add(rectSize.scale(0.5));
	if (
		pos.x >= bottomLeft.x &&
		pos.y >= bottomLeft.y &&
		pos.x < topRight.x &&
		pos.y < topRight.y
	) {
		return true;
	}

	return false;
}
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