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

Add a collision detection algorithm #12569

Open
Andy666Fox opened this issue Feb 9, 2025 · 2 comments
Open

Add a collision detection algorithm #12569

Andy666Fox opened this issue Feb 9, 2025 · 2 comments
Labels
enhancement This PR modified some existing files

Comments

@Andy666Fox
Copy link

Feature description

In the physics section, you can add an algorithm that allows you to detect collisions of different types of primitives. This will be useful when creating games, etc.

@Andy666Fox Andy666Fox added the enhancement This PR modified some existing files label Feb 9, 2025
@ayushh0406
Copy link

Axis-Aligned Bounding Box (AABB) Collision Detection
An AABB is a rectangle whose edges are aligned with the coordinate axes. Collision detection between two AABBs is relatively straightforward:

Define Boundaries: Each rectangle is defined by its minimum and maximum x and y coordinates (or alternatively, by its top-left corner and width/height).

Check for Overlap: For two rectangles to collide, they must overlap in both the x and y axes. This can be checked with simple comparisons.

The conditions for two AABBs to intersect can be expressed as:

Rect1.x < Rect2.x + Rect2.width

Rect1.x + Rect1.width > Rect2.x

Rect1.y < Rect2.y + Rect2.height

Rect1.y + Rect1.height > Rect2.y

If all these conditions are true, the rectangles intersect.

@Rishiram20757
Copy link

function isColliding(rect1, rect2) {
return (
rect1.x < rect2.x + rect2.width &&
rect1.x + rect1.width > rect2.x &&
rect1.y < rect2.y + rect2.height &&
rect1.y + rect1.height > rect2.y
);
}

const rectA = { x: 10, y: 20, width: 50, height: 50 };
const rectB = { x: 40, y: 50, width: 50, height: 50 };

console.log(isColliding(rectA, rectB)); // becomes true if they overlap

this is just an snippet for an example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This PR modified some existing files
Projects
None yet
Development

No branches or pull requests

3 participants