Collison Detection, Help
Author |
Message |
miller6
|
Posted: Mon Jan 25, 2010 3:54 pm Post subject: 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.
Turing: |
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 ) <= bordery1
% or pic_x < 20 %pic_y
end loop
View.Update
pic_x := pic_x + counter
pic_y := pic_y + counter
end MoveFlake3
proc MoveFlake4 % HITS RIGHT
end MoveFlake4
proc MoveFlake5 %HITS LEFT
end MoveFlake5
loop % collision detection and procedure call lines
if pic_y < bordery1 then % HITS BOTTOM
MoveFlake3
elsif pic_y + pic_width > 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
Description: |
|
Filesize: |
1.91 KB |
Viewed: |
1276 Time(s) |
|
Description: |
|
Filesize: |
1.91 KB |
Viewed: |
1276 Time(s) |
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
TheGuardian001
|
Posted: Mon Jan 25, 2010 5:09 pm Post subject: 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
|
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
|
Posted: Mon Jan 25, 2010 5:55 pm Post subject: 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.
Turing: |
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 <= borderx2 then
x_velocity := - 1
elsif pic_y + pic_width >= bordery3 or pic_y <= bordery1 then
y_velocity := - 1
end if
end MoveFlake2
loop
MoveFlake1
MoveFlake2
end loop
|
|
|
|
|
|
|
TheGuardian001
|
Posted: Mon Jan 25, 2010 6:46 pm Post subject: Re: Collison Detection, Help |
|
|
As I said, you can use any value you want for the velocities. 1, 1 is just easier to work with for the purposes of my example. It will work with any number though.
You can incorporate these by simply setting the X and Y velocities to the random values as required. So instead of simply inverting the X/Y velocity when the flake hits a wall, set it to a random number, with the correct sign, depending on the direction it is moving.
So for example
code: |
%X velocity should be positive, it's moving right now.
if hits_left_side then
x_vel = random_number(1,3)
%but this means it's moving left
else if hits_right_side then
%so we want these negative.
x_vel = random_number(-3,-1)
end if
|
One question though, what exactly is the loop with counter doing? Depending on what it's doing, there might be a better way to do that without having a loop inside of a procedure (loops inside procedures are generally not good ideas, they make everything else stop and wait for them)
|
|
|
|
|
|
|
|