Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help With Collision.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
TW iz Rippin




PostPosted: Wed Feb 20, 2013 11:49 am   Post subject: Help With Collision.

What is it you are trying to achieve?

Using movement, the ball should not move past certain parameters.


What is the problem you are having?

I cannot achieve this goal.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

Turing:


setscreen ("nocursor, noecho, nobuttonbar")

colourback (green)
cls

var boardx1 : int := 25
var boardy1 : int := 25
var boardx2 : int := 275
var boardy2 : int := 275
var chars : string (1)
var boxx1 : int := 35
var boxy1 : int := 15
var locationx, locationy, x1, y1 : int
var dot : array 1 .. 10, 1 .. 2 of int
var font1, font2, font3, font4 : int
font1 := Font.New ("serif:20")
font2 := Font.New ("serif:15")

Font.Draw ("Lazer Assignment!", 250, 360, font1, black)
Font.Draw ("By: Taylor Waite", 270, 340, font2, black)

%Draws the white background and the lines for the box
procedure box
    Draw.FillBox (boardx1, boardy1, boardx2, boardy2, white)
    for x : 1 .. 11
        for y : 1 .. 11
            drawline (25, (x * 25), 275, (x * 25), black)
            drawline ((x * 25), 25, (x * 25), 275, black)
        end for
    end for
end box

%Randomizes the location of the dots
procedure dots
    var randomdots : int := Rand.Int (1, 10)
    for a : 1 .. randomdots
        locationx := Rand.Int (2, 9)
        locationy := Rand.Int (2, 9)
        if locationx = 2 then
            x1 := 63
        elsif locationx = 3 then
            x1 := 88
        elsif locationx = 4 then
            x1 := 113
        elsif locationx = 5 then
            x1 := 138
        elsif locationx = 6 then
            x1 := 163
        elsif locationx = 7 then
            x1 := 188
        elsif locationx = 8 then
            x1 := 213
        elsif locationx = 9 then
            x1 := 238
        end if
        if locationy = 2 then
            y1 := 63
        elsif locationy = 3 then
            y1 := 88
        elsif locationy = 4 then
            y1 := 113
        elsif locationy = 5 then
            y1 := 138
        elsif locationy = 6 then
            y1 := 163
        elsif locationy = 7 then
            y1 := 188
        elsif locationy = 8 then
            y1 := 213
        elsif locationy = 9 then
            y1 := 238
        end if
        Draw.FillOval (x1, y1, 5, 5, red)
        dot (a, 1) := x1
        dot (a, 2) := y1
    end for
end dots

box
dots
drawfilloval (boxx1, boxy1, 5, 5, blue)

loop
    getch (chars)
    if chars = 'W' or chars = 'w' then
        boxy1 += 25
        drawfilloval (boxx1, boxy1 - 25, 5, 5, green)
    elsif chars = 'A' or chars = 'a' then
        boxx1 -= 25
        drawfilloval (boxx1 + 25, boxy1, 5, 5, green)
    elsif chars = 'S' or chars = 's' then
        boxy1 -= 25
        drawfilloval (boxx1, boxy1 + 25, 5, 5, green)
    elsif chars = 'D' or chars = 'd' then
        boxx1 += 25
        drawfilloval (boxx1 - 25, boxy1, 5, 5, green)
    end if
    drawfilloval (boxx1, boxy1, 5, 5, blue)
    View.Update
end loop



Please specify what version of Turing you are using
4.1.1



The ball should move just around the board, between each square, but not be able to move away from or onto the board. I can not figure out how to do this.
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Wed Feb 20, 2013 12:09 pm   Post subject: RE:Help With Collision.

code:
if locationx = 2 then
            x1 := 63
        elsif locationx = 3 then
            x1 := 88
        elsif locationx = 4 then
            x1 := 113
        elsif locationx = 5 then
            x1 := 138
        elsif locationx = 6 then
            x1 := 163
        elsif locationx = 7 then
            x1 := 188
        elsif locationx = 8 then
            x1 := 213
        elsif locationx = 9 then
            x1 := 238
        end if


This entire statement (and the one below it) can be reduced to just one line using some multiplication and addition. That won't solve your problem though.

To apply collision, you need to know the coordinates of the lines the ball cannot cross. For example, if you have a 'wall' at x=10 from the bottom of the screen to the top, and you want the ball to remain on the right side of it, simply add a check when moving left (ie whenever A is pressed). If the ball's X is less than 10 (the wall's X), don't move left.
TW iz Rippin




PostPosted: Fri Feb 22, 2013 11:34 am   Post subject: Re: Help With Collision.

Turing:

setscreen ("nocursor, noecho, nobuttonbar")

colourback (green)
cls

var chars : string (1)
var boxx1 : int := 35
var boxy1 : int := 15
var locationx, locationy, x1, y1 : int
var dot : array 1 .. 10, 1 .. 2 of int
var font1, font2, font3, font4 : int
font1 := Font.New ("serif:20")
font2 := Font.New ("serif:15")

%Draws the white background and the lines for the box
procedure box
    Draw.FillBox (25, 25, 275, 275, white)
    for x : 1 .. 11
        for y : 1 .. 11
            drawline (25, (x * 25), 275, (x * 25), black)
            drawline ((x * 25), 25, (x * 25), 275, black)
        end for
    end for
end box

%Randomizes the location of the dots
procedure dots
    var randomdots : int := Rand.Int (1, 10)
    for a : 1 .. randomdots
        locationx := Rand.Int (2, 9)
        locationy := Rand.Int (2, 9)
        if locationx = 2 then
            x1 := 63
        elsif locationx = 3 then
            x1 := 88
        elsif locationx = 4 then
            x1 := 113
        elsif locationx = 5 then
            x1 := 138
        elsif locationx = 6 then
            x1 := 163
        elsif locationx = 7 then
            x1 := 188
        elsif locationx = 8 then
            x1 := 213
        elsif locationx = 9 then
            x1 := 238
        end if
        if locationy = 2 then
            y1 := 63
        elsif locationy = 3 then
            y1 := 88
        elsif locationy = 4 then
            y1 := 113
        elsif locationy = 5 then
            y1 := 138
        elsif locationy = 6 then
            y1 := 163
        elsif locationy = 7 then
            y1 := 188
        elsif locationy = 8 then
            y1 := 213
        elsif locationy = 9 then
            y1 := 238
        end if
        Draw.FillOval (x1, y1, 5, 5, red)
        dot (a, 1) := x1
        dot (a, 2) := y1
    end for
end dots

box
dots
drawfilloval (boxx1, boxy1, 5, 5, blue)

loop
    getch (chars)
    %if it is on the corner then move it to the side
    if chars = 'W' or chars = 'w' and boxy1 = 15 and boxx1 = 35 then
        boxy1 += 25
        boxx1 -= 25
        drawfilloval (boxx1 + 25, boxy1 - 25, 5, 5, green)
    elsif chars = 'W' or chars = 'w' and boxy1 = 15 and boxx1 = 260 then
        boxy1 += 25
        boxx1 += 25
        drawfilloval (boxx1 - 25, boxy1 - 25, 5, 5, green)
    elsif chars = 'W' or chars = 'w' and boxy1 = 15 and boxy1 + 25 < 35 then
        boxy1 += 25
        drawfilloval (boxx1, boxy1 - 25, 5, 5, green)
    elsif chars = 'W' or chars = 'w' and boxy1 >= 40 and boxy1 + 25 < 300 then
        boxy1 += 25
        drawfilloval (boxx1, boxy1 - 25, 5, 5, green)
    elsif chars = 'A' or chars = 'a' and boxx1 = 10 and boxx1 - 25 > 5 then
        boxx1 -= 25
        drawfilloval (boxx1 + 25, boxy1, 5, 5, green)
    elsif chars = 'A' or chars = 'a' and boxx1 >= 275 and boxy1 >= 40 then
        boxx1 -= 25
        boxy1 += 25
        drawfilloval (boxx1 + 25, boxy1 - 25, 5, 5, green)
    elsif chars = 'A' or chars = 'a' then
        boxx1 -= 25
        drawfilloval (boxx1 + 25, boxy1, 5, 5, green)
    elsif chars = 'S' or chars = 's' and boxx1 = 35 and boxy1 >= 285 then
        boxy1 -= 25
        boxx1 -= 25
        drawfilloval (boxx1 + 25, boxy1 + 25, 5, 5, green)
    elsif chars = 'S' or chars = 's' and boxx1 = 260 and boxy1 >= 40 then
        boxy1 -= 25
        boxx1 += 25
        drawfilloval (boxx1 - 25, boxy1 + 25, 5, 5, green)
    elsif chars = 'S' or chars = 's' and boxx1 = 285 and boxy1 = 40 then
        boxy1 -= 25
        boxx1 -= 25
        drawfilloval (boxx1 + 25, boxy1 + 25, 5, 5, green)
    elsif chars = 'S' or chars = 's' and boxx1 > 34 and boxx1 < 285 and boxy1 = 15 and boxy1 - 25 > 10 then
        boxy1 -= 25
        drawfilloval (boxx1, boxy1 + 25, 5, 5, green)
    elsif chars = 'S' or chars = 's' then
        boxy1 -= 25
        drawfilloval (boxx1, boxy1 + 25, 5, 5, green)
    elsif chars = 'D' or chars = 'd' and boxx1 = 10 and boxy1 >= 260 then
        boxx1 += 25
        boxy1 += 25
        drawfilloval (boxx1 - 25, boxy1 - 25, 5, 5, green)
    elsif chars = 'D' or chars = 'd' and boxx1 = 10 and boxy1 = 40 then
        boxx1 += 25
        boxy1 -= 25
        drawfilloval (boxx1 - 25, boxy1 + 25, 5, 5, green)
    elsif chars = 'D' or chars = 'd' then
        boxx1 += 25
        drawfilloval (boxx1 - 25, boxy1, 5, 5, green)
    end if
    drawfilloval (boxx1, boxy1, 5, 5, blue)
    View.Update
end loop


How is that? Any suggestions?
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: