

-----------------------------------

miller6

Mon Jan 25, 2010 3:54 pm


Collison Detection, Help

-----------------------------------

What is it you are trying to achieve?
I am trying to get an image to move around and act just like the ball in brick breaker. 


What is the problem you are having?
Using collision detection to make sure it does not go off the screen.


Describe what you have tried to solve this problem
I ahve tried using a variety of different techniques in order to make the image move.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
So far i have to procedures and collision detection for the top and bottom butt he bottom is not working.


var WinId : int
WinId := Window.Open ("position:centre;centre,graphics:1010;695")
var pic : int
pic := Pic.FileNew ("sflake.gif")
var pic_x := 18
var pic_y := 24
var pic_width : int := 52
var borderx1 := maxx
var bordery1, borderx2 := 20 % x1 and y1 coordinate for box
var bordery2 := maxy
var bordery3 := maxy - 20 % y2 coordinate for box
var borderx4 := maxx - 20 % x2 coordinate for box

var x_angle, y_angle, x_angle2, y_angle2, x_angle3, y_angle3 : int := 1

Pic.Draw (pic, pic_x, pic_y, picCopy)
Draw.Box (borderx2, bordery1, borderx4, bordery3, black)

x_angle := Rand.Int (-3, -1)
y_angle := Rand.Int (-3, 3)
x_angle2 := Rand.Int (-3, 3)
y_angle2 := Rand.Int (-3, -1)
x_angle3 := Rand.Int (1, 3)
y_angle3 := Rand.Int (1, 3)

proc MoveFlake1  % MOVES SNOWFLAKE AROUND
    var counter := 0
    loop
        counter := counter + 1
        Pic.Draw (pic, pic_x + counter, pic_y + counter, picCopy)  %animates flake
        delay (5)
        View.Update
        exit when pic_y + counter + pic_width >= maxy - 19
    end loop
    pic_x := pic_x + counter  % saves coordinates
    pic_y := pic_y + counter
    View.Update
end MoveFlake1

proc MoveFlake2 %HITS TOP
    % counter is now to make it decrease
    var counter := 0
    loop
        counter := counter + 1

        pic_x := pic_x + x_angle2
        pic_y := pic_y + y_angle2
        Pic.Draw (pic, pic_x - counter, pic_y - counter, picCopy)
        delay (5)
        View.Update
        exit when pic_y + counter + pic_width >= bordery3 or pic_y < bordery1
    end loop
    View.Update
    pic_x := pic_x + counter
    pic_y := pic_y + counter
end MoveFlake2

proc MoveFlake3 % HITS BOTTOM
    var counter := 0
    loop
        counter := counter + 1

        pic_x := pic_x + x_angle2
        pic_y := pic_y + y_angle3
        Pic.Draw (pic, pic_x + counter, pic_y + counter, picCopy)

        delay (5)
        View.Update
        exit when pic_y + counter + Pic.Height (pic)  bordery3 then % HITS TOP
        MoveFlake2
    elsif pic_y < bordery3 and pic_x < borderx4 then
        MoveFlake1
    end if
end loop





Please specify what version of Turing you are using
4.1.1


-----------------------------------

TheGuardian001

Mon Jan 25, 2010 5:09 pm


Re: Collison Detection, Help

-----------------------------------

Instead of having 4 separate procedures for each direction, you should be using 1 procedure to move the flake, and using different velocity values to change direction.

So first, create a variable for each X velocity and Y velocity. Set them both equal to 1 (you can set them to whatever you want, but this is a good starting point to get it working. Having a positive x and y velocity means that the flake will move Up and to the Right.

Whenever the flake hits the side of the screen, you should multiply the X velocity by -1. This will switch whether it is moving left or right.
Whenever the flake hits the top or bottom of the screen, you should multiply Y velocity by -1, which switches whether it moves up or down.

Once you've done that, the one remaining movement procedure should follow this sort of structure:
[code]
proc Move_flake
    flake_x_coord += x_Velocity
    flake_y_coord += y_Velocity

    if flake hits side
        invert x velocity
    end if
    if flake hits top or bottom
        invert y velocity
    end if
end Move_flake
[/code]

It should be pointed out that there is no loop in that procedure structure. you should be looping calls to the procedure, not looping the contents of the procedure.


-----------------------------------

miller6

Mon Jan 25, 2010 5:55 pm


Re: Collison Detection, Help

-----------------------------------

Do i still incorporate the different angles so that each time it hits it bounces off a new direction (just like the ball in brick breaker) if so, where? 

This is the new codding i changed.



var WinId : int
WinId := Window.Open ("position:centre;centre,graphics:1010;695")
var pic : int
pic := Pic.FileNew ("sflake.gif")
var pic_x := 18
var pic_y := 24
var pic_width : int := 52
var borderx1 := maxx
var bordery1, borderx2 := 20 % x1 and y1 coordinate for box
var bordery2 := maxy
var bordery3 := maxy - 20 % y2 coordinate for box
var borderx4 := maxx - 20 % x2 coordinate for box
% var boderx4 := maxx - 20
var x_angle, y_angle, x_angle2, y_angle2, x_angle3, y_angle3 : int := 1
var x_velocity, y_velocity : int := 1

Pic.Draw (pic, pic_x, pic_y, picCopy)
Draw.Box (borderx2, bordery1, borderx4, bordery3, black)

x_angle := Rand.Int (-3, -1)
y_angle := Rand.Int (-3, 3)
x_angle2 := Rand.Int (-3, 3)
y_angle2 := Rand.Int (-3, -1)
x_angle3 := Rand.Int (1, 3)
y_angle3 := Rand.Int (1, 3)

proc MoveFlake1
    var counter := 0
    loop
        counter := counter + 1
        Pic.Draw (pic, pic_x + counter, pic_y + counter, picCopy)  %animates flake
        delay (5)
        View.Update
        exit when pic_y + counter + Pic.Height (pic) >= maxy - 19
    end loop
    pic_x := pic_x + counter  % saves coordinates
    pic_y := pic_y + counter
    View.Update
end MoveFlake1

proc MoveFlake2
    pic_x := pic_x + x_velocity
    pic_y := pic_y + y_velocity

    if pic_x + pic_width >= borderx4 or pic_x = bordery3 or pic_y 