How to create a simple rectangle collision algorithm?

How to create a simple rectangle collision algorithm?

– Game Development Stack Exchange a simple 2D rectangle collision algorithm that also determines which sides that the rectangles collide? I initially tried out implementing rectangular intersection, which works well.

How to find out which side was hit in a collision?

Adapted from my answer to “Which Side Was Hit?”: I suggest computing the Minkowski sum of B and A, which is a new rectangle, and checking where the centre of rectangle A lies relatively to that new rectangle (to know whether a collision is happening) and to its diagonals (to know where the collision is happening):

Why are rectangles unstable in floating point arithmetic?

However, for floating-point arithmetic, this proves to be unstable because of unknown time elapse. Sometime, the rectangles would actually intersect each other before it meets the defined range. On the other hand, I was thinking about spawning multiple rectangles, each rectangle for each sides.

How to calculate the distance between parallel rectangles?

What I did in the past is determine the distance between each parallel rectangular sides and check if the distance is close to 0 (use some initially defined distance range) or is 0. However, for floating-point arithmetic, this proves to be unstable because of unknown time elapse.

How does collision detection work in a 2D game?

Generally you will have a simple generic shape that covers the entity known as a “hitbox” so even though collision may not be pixel perfect, it will look good enough and be performant across multiple entities. This article provides a review of the most common techniques used to provide collision detection in 2D games.

What are the different types of collision detection?

2D collision detection 1 Axis-Aligned Bounding Box. One of the simpler forms of collision detection is between two rectangles that are axis aligned — meaning no rotation. 2 Circle Collision. Another simple shape for collision detection is between two circles. 3 Separating Axis Theorem. 4 Collision Performance.