Computer Science Canada

Problem With Collision

Author:  JumpingLimaBean [ Wed Jun 08, 2011 10:38 pm ]
Post subject:  Problem With Collision

So im having problems with collision. I collide with something and
my movable circle goes about half into the object then detects it and completly stops. I need it so certain objects can not
pass a certain point or colour. My problem is that my circle goes half away into it then detects it and cant go further,
and once it detects it and stops, i can no longer move it.

controls are arrows up, down, left, right.

I also do not know how to stop the circle from being able to go out of of the window area.


var x := 320
var y := 200
const INCREMENT : int := 5
var rx1, ry1, rx2, ry2 : int

%this variable is needed to use Input.Keydown
var chars : array char of boolean

rx1 := 40
ry1 := 30
rx2 := 200
ry2 := 100


loop
cls
Draw.Oval (x, y, 50, 50, red)
Draw.FillBox (rx1, ry1, rx2, ry2, black)

Input.KeyDown (chars) %tell computer to listen for keypresses

%check arrow keys
%Look up Keyboard in the Turing reference to find all special
%key codes
if chars (KEY_LEFT_ARROW) and chars(KEY_UP_ARROW) then
if whatdotcolour (x, y - 10) not= black then
x := x - INCREMENT
y := y + INCREMENT
end if
elsif chars (KEY_RIGHT_ARROW) and chars(KEY_UP_ARROW) then
if whatdotcolour (x, y - 10) not= black then
x := x + INCREMENT
y := y + INCREMENT
end if
elsif chars (KEY_LEFT_ARROW) and chars(KEY_DOWN_ARROW) then
if whatdotcolour (x, y - 10) not= black then
y := y - INCREMENT
x := x - INCREMENT
end if
elsif chars (KEY_RIGHT_ARROW) and chars(KEY_DOWN_ARROW) then
if whatdotcolour (x, y - 10) not= black then
y := y - INCREMENT
x := x + INCREMENT
end if
elsif chars (KEY_LEFT_ARROW) then
if whatdotcolour (x, y - 10) not= black then
x := x - INCREMENT
end if
elsif chars (KEY_RIGHT_ARROW) then
if whatdotcolour (x, y - 10) not= black then
x := x + INCREMENT
end if
elsif chars (KEY_UP_ARROW) then
if whatdotcolour (x, y - 10) not= black then
y := y + INCREMENT
end if
elsif chars (KEY_DOWN_ARROW) then
if whatdotcolour (x, y - 10) not= black then
y := y - INCREMENT
end if
end if



%check asdf keys
if chars ('a') then
x := x - INCREMENT
elsif chars ('d') then
x := x + INCREMENT
elsif chars ('w') then
y := y + INCREMENT
elsif chars ('s') then
y := y - INCREMENT
end if

delay(20)
end loop

Author:  Tony [ Wed Jun 08, 2011 10:59 pm ]
Post subject:  RE:Problem With Collision

You can draw additional points to check if your "whatdotcolour (x, y - 10)" is, where you think it is. It would obviously break the actual check (which is one of many reasons why checking for colour is a bad idea), but you just want to figure out the placement of your checks at this point.

Author:  JumpingLimaBean [ Wed Jun 08, 2011 11:11 pm ]
Post subject:  RE:Problem With Collision

Thanks although i still do not understand

Author:  Tony [ Wed Jun 08, 2011 11:28 pm ]
Post subject:  RE:Problem With Collision

replace every
code:

if whatdotcolour (x, y - 10) not= black then

with
code:

Draw.Oval(x,y-10,2,2,blue)
if whatdotcolour (x, y - 10) not= black then

Author:  JumpingLimaBean [ Thu Jun 09, 2011 11:48 am ]
Post subject:  RE:Problem With Collision

I tried that and it still detects it and stops half way into the black square

Author:  Zren [ Thu Jun 09, 2011 4:47 pm ]
Post subject:  RE:Problem With Collision

Your collision detection only checks if (x, y - 10) is black. No matter what direction you are going. So you basically can't move if you enter the box other then going directly down (which is when it isn't possible to enter).

~ From here on is just observations about your code.

Also, you can simplify this movement code.
Turing:

var speed := 10

if left then
    x -= speed
elsif right then
    x += speed
end if

if down then
    y -= speed
elsif up then
    y += speed
end if


With this type of movement, you're actually moving faster when your moving diagonally.

The problem there is that you have to separate the collision detection from the input/movement logic. So you need to use a separate variable to check where the point ends up, see if it's in the box, and if not, assign the old point to your calculated one.

Turing:

type Point :
    record
        x, y : int
    end record
var p : Point
p.x := maxx div 2
p.y := maxy div 2
loop
    var d : Point := p
    % Have input move point d (where you plan to move to).
    if left then
        d.x -= speed
    elsif right then
        d.x += speed
    end if

    if down then
        d.y -= speed
    elsif up then
        d.y += speed
    end if
    % Check if d is now inside box (or other objects).
    p := d % if d isn't in box, it's okay to move there.

    Draw.Dot (p.x, p.y, black)
end loop


: