Computer Science Canada

Wall Collision

Author:  Mr. T [ Tue Jun 20, 2006 10:18 pm ]
Post subject:  Wall Collision

The commented part of code is that which I wish to be helped with. Using whatdotcolour (preferably Rolling Eyes ), how could I determine if the expanding bars have collided with the outer boundaries that I have drawn. TheOneTrueGod suggested using grid collision, but I wanted to know if there is a whatdotcolour solution. Smile
code:
View.Set ("offscreenonly,nobuttonbar")
proc drawBar (barX, barY, a, b : int)
    Draw.FillBox (barX, barY, barX + a, barY + 10, red)
    Draw.FillBox (barX, barY, barX - b, barY + 10, green)
end drawBar

var x, y, button, left, middle, right : int := 0
var barX, barY, a, b : int := 0
var boolMoveRed, boolMoveGreen := false

loop

    View.Update
    delay (10)
    cls

    %Mouse Controls
    Mouse.ButtonChoose ("multibutton")
    Mouse.Where (x, y, button)
    left := button mod 10      % left = 0 or 1
    middle := (button - left) mod 100      % middle = 0 or 10
    right := button - middle - left      % right = 0 or 100

    %Wall Movement Collision
    %if %COLLISION WITH RIGHT SIDE OF THE SCREEN then
    %   boolMoveRed := false
    %end if
    %if %COLLISION WITH LEFT SIDE OF THE SCREEN then
    %   boolMoveGreen := false
    %end if

    %Wall Movement Flags
    if left = 1 then
        barX := x
        barY := y
        boolMoveRed := true
        boolMoveGreen := true
    end if
    if boolMoveRed = true then
        a += 10
        drawBar (barX, barY, a, b)
    end if
    if boolMoveGreen = true then
        b += 10
        drawBar (barX, barY, a, b)
    end if

    %Draw Border
    Draw.Box (10, 10, maxx - 10, maxy - 10, black)

end loop

Author:  Mr. T [ Tue Jun 20, 2006 11:50 pm ]
Post subject:  Alex's Opinion

Ok, I managed to solve that problem on my own, but I have a new one. I know it's impossible to pass a flexible array into a procedure through a parameter, so how would I convert my code so that I can draw an increasing # of walls after the first one is complete. I have already gotten a few suggestions from TheOneTrueGod, but I'm still a little bit confused as to how I would implement it in my code.

Author:  Mr. T [ Wed Jun 21, 2006 4:04 am ]
Post subject:  Alex's Opinion

^Read the post above, then look at the code below:
code:
View.Set ("offscreenonly,nobuttonbar")
proc drawBar (barX, barY, a, b, colRight, colLeft : int)
    Draw.FillBox (barX, barY, barX + a, barY + 10, colRight)
    Draw.FillBox (barX, barY, barX - b, barY + 10, colLeft)
end drawBar

const RADIUS : int := 5

type ballData :
    record
        x : int
        y : int
        dx : int
        dy : int
    end record

var ballMovement : flexible array 1 .. 2 of ballData

for i : 1 .. upper (ballMovement)
    ballMovement (i).x := Rand.Int (RADIUS + 10, maxx - RADIUS - 10)
    ballMovement (i).y := Rand.Int (RADIUS + 10, maxy - RADIUS - 10)
    ballMovement (i).dx := Rand.Int (-5, 5)
    ballMovement (i).dy := Rand.Int (-5, 5)
end for

var clickX, clickY, button, left, middle, right : int := 0
var barX, barY, a, b, barStoreA, barStoreB : int := 0
var boolMoveRed, boolMoveGreen := false
var boolClick := true
var topCounter, bottomCounter := 0

loop

    View.Update
    delay (10)
    cls

    %Draw Border
    Draw.Box (10, 10, maxx - 10, maxy - 10, black)
    Draw.Fill (5, 5, black, black)

    %Mouse Controls
    Mouse.ButtonChoose ("multibutton")
    Mouse.Where (clickX, clickY, button)
    left := button mod 10      % left = 0 or 1
    middle := (button - left) mod 100      % middle = 0 or 10
    right := button - middle - left      % right = 0 or 100

    %Wall Movement Flags
    if left = 1 and boolClick = true and whatdotcolour (clickX, clickY) not= black and whatdotcolour (clickX, clickY) not= blue then
        barX := clickX
        barY := clickY
        boolMoveRed := true
        boolMoveGreen := true
        boolClick := false
    end if
    if boolMoveRed = true then
        a += 5
        barStoreA := barX + a
        drawBar (barX, barY, a, b, red, green)
    end if
    if boolMoveGreen = true then
        b += 5
        barStoreB := barX - b
        drawBar (barX, barY, a, b, red, green)
    end if

    %Wall Movement Collision
    if whatdotcolour (barStoreA + 1, barY) = 7 then
        boolMoveRed := false
        if boolMoveGreen = false then
            drawBar (barX, barY, a, b, blue, blue)
        elsif boolMoveGreen = true then
            drawBar (barX, barY, a, b, blue, green)
        end if
    end if
    if whatdotcolour (barStoreB - 1, barY) = 7 then
        boolMoveGreen := false
        if boolMoveRed = false then
            drawBar (barX, barY, a, b, blue, blue)
        elsif boolMoveRed = true then
            drawBar (barX, barY, a, b, red, blue)
        end if
    end if

    %Wall Completion
    if whatdotcolour (barStoreA + 1, barY) = 7 and whatdotcolour (barStoreB - 1, barY) = 7 then

        %Box Creation Counter (above, below, or none)
        for i : 1 .. upper (ballMovement)
            if ballMovement (i).y > barY then          %Balls above wall
                topCounter += 1
            elsif ballMovement (i).y < barY then      %Balls below wall
                bottomCounter += 1
            end if

            %Drawing Completed Wall
            if topCounter not= 0 and bottomCounter not= 0 then %balls are located ABOVE and BELOW the bar
                %No change
            elsif topCounter not= 0 then                %balls are located ABOVE the bar
                Draw.FillBox (10, 10, maxx - 10, barY, blue)
            elsif bottomCounter not= 0 then %balls are located BELOW the bar
                Draw.FillBox (10, barY, maxx - 10, maxy - 10, blue)
            end if
        end for
    end if

    %Various Ball Collisions
    for i : 1 .. upper (ballMovement)

        %Balls to Bar Collision (for balls BELOW the bar)
        if ballMovement (i).y + RADIUS >= barY and whatdotcolour (ballMovement (i).x, ballMovement (i).y + RADIUS) = blue then
            ballMovement (i).dy := -ballMovement (i).dy
        end if

        %Balls to Bar Collision (for balls ABOVE the bar)
        if ballMovement (i).y - RADIUS >= barY and whatdotcolour (ballMovement (i).x, ballMovement (i).y - RADIUS) = blue then
            ballMovement (i).dy := -ballMovement (i).dy
        end if

        %Outer Boundary Collision for Balls
        if ballMovement (i).x + ballMovement (i).dx - 10 < RADIUS or
                ballMovement (i).x + ballMovement (i).dx > maxx - 10 - RADIUS then
            ballMovement (i).dx := -ballMovement (i).dx
        end if
        if ballMovement (i).y + ballMovement (i).dy - 10 < RADIUS or
                ballMovement (i).y + ballMovement (i).dy > maxy - 10 - RADIUS then
            ballMovement (i).dy := -ballMovement (i).dy
        end if

        %Change Direction of Balls
        ballMovement (i).x := ballMovement (i).x + ballMovement (i).dx
        ballMovement (i).y := ballMovement (i).y + ballMovement (i).dy

        %Draw Balls
        Draw.FillOval (ballMovement (i).x, ballMovement (i).y, RADIUS, RADIUS, black)
    end for

end loop

Author:  zylum [ Thu Jun 22, 2006 12:00 am ]
Post subject: 

you can pass a flexible array to a procedure but you cant change its dimension once in the procedure..

ie:

code:
var arr : flexible array 1 .. 5 of int
for i : 1 .. upper (arr)
    arr (i) := Rand.Int (1, 9)
end for

proc printArray (a : array 1 .. * of int)
    for i : 1 .. upper (a)
        put a (i), " " ..
    end for
end printArray

printArray (arr)


if that doesnt help, can you explain a little further what you need this for?

Author:  Mr. T [ Thu Jun 22, 2006 12:15 am ]
Post subject:  Alex's Opinion

I know...so how would you work around this problem? I will need to update the flexible array.

Author:  Tony [ Thu Jun 22, 2006 12:22 am ]
Post subject: 

you could pass a reference to the array using a pointer

Author:  Mr. T [ Thu Jun 22, 2006 12:27 am ]
Post subject:  Alex's Opinion

I didn't understand a single thing you just said. Care to link me to the necessary tutorials?

Author:  Tony [ Thu Jun 22, 2006 12:34 am ]
Post subject: 

pointers

part of the Turing Walkthrough actually

Author:  Mr. T [ Sat Jun 24, 2006 9:52 pm ]
Post subject:  Alex's Opinion

I read over the tutorial, but I'm still not sure how pointers will allow me to update a flexible array that has been passed into a procedure through a paramater...? Confused


: