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 · 2 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;
}
@KilledByAPixel
Copy link
Owner

So is the it the edge cases that are important? Maybe we can refactor isOverlapping to work more like this. Just really redefining how the edge cases work.

@michael-dean-haynie
Copy link
Author

michael-dean-haynie commented Jan 14, 2025

The edge cases were important for my personal use case.
I'm not sure how important they might be for other people.

I think the main reason I opened this issue was because I couldn't find a helper to check if a rect contained a point.
I thought that would be something everyone might want.
I'm new to game math though, maybe it's a trivial calculations with one of the other helpers or something that I missed.

... oh I see. maybe isOverlapping() pulls double duty here with smart usage of arguments.
If that's the case then maybe we should just close this.

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

2 participants