Computer Science Canada

Need help with collision detection on a rotated object

Author:  masterofcode [ Sun Jan 11, 2015 12:31 pm ]
Post subject:  Need help with collision detection on a rotated object

What is it you are trying to achieve?
I am making a driving simulator and need to use fail the user when they go off the road.


What is the problem you are having?
My car rotates using Pic.Rotate. I need to find a way to detect the collision based on the newly positioned image. My background if a large green box with roads drawn on top.


Describe what you have tried to solve this problem
There would be too many cases to use regular collision detection so I have tried to use whatdotcolour. It works fine when the car is rotated at 0 degrees, 90 degrees, and 180 degrees but I cant think of a way to do it any other way. My car rotates 9 degrees at a time to avoid using sin and cos.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
The code below is an example program of how I used whatdotcolour. Could somebody please tell me how to fix this code so it doesn't matter the angle at which the object is rotated.

Turing:

var x1 := 50
var x2 := 100
var y1 := 50
var y2 := 100
var exitLoop : boolean := false
var chars : array char of boolean
drawfillbox (320, 200, 520, 400, 7)
drawfillbox (x1, y1, x2, y2, 2)
loop
    drawfillbox (x1, y1, x2, y2, 0)
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) then
        y1 += 5
        y2 += 5
    elsif chars (KEY_DOWN_ARROW) then
        y1 -= 5
        y2 -= 5
    elsif chars (KEY_RIGHT_ARROW) then
        x1 += 5
        x2 += 5
    elsif chars (KEY_LEFT_ARROW) then
        x1 -= 5
        x2 -= 5
    end if
    drawfillbox (x1, y1, x2, y2, 2)
    for a : x1 .. x2
        for b : y1 .. y2
            if whatdotcolour (a + 1, b + 1) = 7 then
                exitLoop := true
            end if
        end for
    end for
    exit when exitLoop
end loop
locate (23, 1)
put "Colision detected"


Please specify what version of Turing you are using
4.1.1

Author:  Insectoid [ Mon Jan 12, 2015 8:55 pm ]
Post subject:  RE:Need help with collision detection on a rotated object

This requires line-line collision. It uses a lot more math than rectangular collisions. Fortunately, you've probably learned this math in your Advanced Functions class if you've taken it. You have to check every line in object A to find out if it intersects with any lines in object B. You also might need to check if object A is completely inside object B.


: