Shooting the radius of a circle
Author 
Message 
Luffy

Posted: Mon May 24, 2004 3:01 pm Post subject: Shooting the radius of a circle 


Ok well I'm trying to figure out how do I shoot a line and when it hits the radius of a circle, it makes the circle disappear. Also I read the tutorial for collision but I don't really get it...and I'm trying to make it as well for if the rock hits my ship then it disappears. Heres my coding below...
Use the arrow keys to move around and push CTRL to shoot.
Quote:
setscreen ("graphic: 500,500")
var move : array char of boolean
var change, change2 : int := 0
var fire : int := 0
var rock : int := 0
var fall : int := 1
procedure rockfall
randint (rock, 1, 1)
if rock = 1 then
fall := fall  1
drawfilloval (10, 400 + fall, 10, 10, brown)
end if
end rockfall
procedure movement
Input.KeyDown (move)
if move (KEY_RIGHT_ARROW) then
change := change + 1
if change = maxx  20 then
change := change  1
end if
end if
if move (KEY_LEFT_ARROW) then
change := change  1
if change = 0 then
change := change + 1
end if
end if
if move (KEY_UP_ARROW) then
change2 := change2 + 1
if change2 = maxy  20 then
change2 := change2  1
end if
end if
if move (KEY_DOWN_ARROW) then
change2 := change2  1
if change2 = 0 then
change2 := change2 + 1
end if
end if
if move (KEY_CTRL) then
delay (10)
fire := fire + 1
drawline (10 + change, change + (change2 + fire), 10 + change, 10 + change2, red)
if fire = rock then
cls
end if
end if
drawfilloval (10 + change, 10 + change2, 10, 10, blue)
end movement
loop
View.Update
cls
setscreen ("offscreenonly")
movement
rockfall
end loop






Sponsor Sponsor



Tony

Posted: Mon May 24, 2004 3:21 pm Post subject: (No subject) 


the the line/circle collision  you can ether check if the distance between the line and the center of the circle is <= radious or try to find a real solution for the point of intersection by making the two equations (that of circle and the line) equate to each other.
whatever geometry approach is easier for you. I think the distance would be easier to find... You'd need to find a normal to the line that passes though circle's center and find the distance between circle's center and point where the line and it's normal intersect.

Tony's programming blog. DWITE  a programming contest. 




Luffy

Posted: Mon May 24, 2004 3:25 pm Post subject: (No subject) 


Boo...geometry...I dislike those things but meah I'll try that...thanks for helping
Edit: BTW can you give me an example of a coding so it'll be easier for me...o.O






Luffy

Posted: Mon May 24, 2004 3:56 pm Post subject: (No subject) 


Bla...I still don't get this geometry thing...my line is still going pass the circle and the circle isn't disappearing...do I find the x and y of the line or the circle?






Paul

Posted: Mon May 24, 2004 3:59 pm Post subject: (No subject) 


The y value of the tip of the line, compared to the y value of the center of the circle  the radius of the circle. PLUS the x value of the tip of the line compared to the x value of the circle  radius and x value of the circle +radius (to find if the line is within the width of the circle.






Luffy

Posted: Mon May 24, 2004 4:05 pm Post subject: (No subject) 


So I'll need a variable for radius, x and y of the circle and the x and y of the line?






Cervantes

Posted: Mon May 24, 2004 5:06 pm Post subject: (No subject) 


code: 
function MathDistance (x1, y1, x2, y2 : int) : real
result ((y2  y1) * (y2  y1)) + ((x2  x1) * (x2  x1)) ** 0.5
end MathDistance
var player_x, player_y : int
player_x := maxx div 2
player_y := 15
var laser_length := 0
var rock_x, rock_y : int
rock_x := maxx div 2
rock_y := maxy + 100
const rock_radius := 8
loop
laser_length += 1
rock_y = 1
drawfilloval (player_x, player_y, 6, 6, blue)
drawfilloval (rock_x, rock_y, rock_radius, rock_radius, red)
drawline (player_x, player_y, player_x, player_y + laser_length, brightgreen)
delay (10)
exit when MathDistance (rock_x, rock_y, player_x, player_y + laser_length) <= rock_radius
drawfilloval (rock_x, rock_y, rock_radius, rock_radius, white)
end loop

i don't know if you have turing 4.0.5; if you do, in the exit when line, where it says "MathDistance" you can change that to "Math.Distance", and get rid of the function at the beginning.






Luffy

Posted: Mon May 24, 2004 5:26 pm Post subject: (No subject) 


I have v.3...hey thanks man I really appreciate it






LiquidDragon

Posted: Mon May 24, 2004 7:07 pm Post subject: (No subject) 


Nuts to all that geometry stuff. Just use whatdotcolor and check if the x and y of the top of the line is the same colour of the other falling circle thing







