Marko
Marko

Reputation: 39

Simple 2D collision detection with vectors

I am trying to create collision algorithm and implement him in my Win32 2D GUI app. The task is that i got one vector that determinate mid-bottom of .bmp image and four more vectors in position of rhombus. I wanted to make it work so algorithm know if image is coming from left, right, up or bottom. There are a lot of tutorials over internet about collision detection of rectangle, circle and distance calculation but i am having difficulties in applying them for rhombus. There is also something called axis-aligned bounding but i think it is for 3d vectors. I am very weak when it comes to this topic, so if there is any skilled C++ programmer who could direct me to some good e-book that mention this topic or if the code is small maybe type it out. I tried doing iteration of X,Y coordinates over ABCD whole rhombus and failed miserably.

Thanks everyone who help.

Upvotes: 0

Views: 2572

Answers (1)

Vlad
Vlad

Reputation: 35594

I am not an expert on computer graphics, but generally your problem boils down to determining whether a dot is inside a convex polygon or not.

For this, I would use the following simple technique. There is a function called vector product, which helps to determine if the rotation direction from one vector to another is positive or negative. So, your check for whether a point X is inside rhombus formed by a polygon A_1, A_2, ..., A_n is just to check whether all the vector products (A_iA_{i+1}, A_iX) have the same sign.

Vector product of vectors (x1, y1) and (x2, y2) is defined as x1 * y2 - x2 * y1.

Now, your detection goes on like this: if the previous position is outside the polygon, and the current is inside, than the dot has hit the polygon. In order to determine, which side was crossed, you just have to check which of the signs of vector products (A_iA_{i+1}, A_iX) has changed since last position update.

Upvotes: 1

Related Questions