Guye Incognito
Guye Incognito

Reputation: 2844

Collision and rectangles/line intersection

I'm doing collision detection for a game, The levels are randomly generated from blocks each with its own bounding box. My collision detection is ok except sometimes, the player ship can collide with 2 or more blocks at the same time and I need to work out which one it hit 1st.

Ive already worked out that If I find the corner of the ship bounding box thats in its movement direction, and then draw a line in the opposite direction of the ships movement vector from that corner (red line on diagram), the intersection point thats furthest away on that line will be on the 1st box hit. I just don't know how to program that logic!

What I have to work with

enter image description here

Upvotes: 1

Views: 494

Answers (1)

philkark
philkark

Reputation: 2457

My approach would be: Add another set of bounding boxes to the ship that are smaller and more acurate. If collision occurs, you should then start checking into the scale of smaller bounding boxes. From what I understand, many games do that to avoid unneccasary collision checks, but have a way to determin collisions more acurately when they occur.

Upvotes: 1

Related Questions