Ok, lets get a ready for a tutorial that I don't think has yet come in Comp.Sci, and it is obviosuly Math.Distance. Math.Distance, along with many other ways, is a way of creating a 'Hit Detection'...Usually with boxes, you may right in the coordinates liek so:
code:
if x > 100 and x < 200 and y > 100 and y< 200 then
%Code
end if
...Hmm, yes that was the Math way...Well, what about circles? there are ways to do it with circles also...
code:
if whatdotcolor (x,y) = 7 then
%Code
end if
There are other ways also, but I will explain Math.Distance...
First and foremost, you must create the variables...
code:
var ballx,bally,radius:int
ballx:=100
baly:=200
radius:=30
So now we have a variable for the whole ball, the radius can be whatever number you like, along with the ballx and bally. In this case, the ball will be the size of 30...
So let's assume that when the mouse hits the circle, the hit detection will occur, well we haven't created the x and y varialbes for the mouse have we?...
code:
Now we have...Ok, so now we will input the Math.Distance function, I will do it one line by one, and explain each line...Only one line hehe.
code:
if Math.Distance (mx,my,ballx,bally) < radius * 2 then
put"We Have A Detection!"
end if
Ok, a little confusing but not really. You see the brackets with the 4 variablesi n it, recognize them? Yes they are indeed the variables we declared earlier in the tutorial, mad flashbacks! So we have the mx, and my, ballx and bally all in one package. So when all these meet, and the mouse is les than the radius, then we have a detection. WHAT THE... WHAT THE HECK IS THE *2? well that is easy, because since the radius is in the middle of the circle, we want the detection to happen when we touch the outside of the circle. So by adding the * 2, the program will multiply all around the ball, bringing the detection to tyhe outside of the ball
Now we have that covered, but what about wh a circle hits a line? Well, for this, instead of Math.Distance, we will put:
code:
An example would be:
code:
if Math.DistancePointLine (linex, liney, 600, 200, 600, 300) < radius then
Input.Pause
end if
Now, it is the same thing, in this case you aren't using mousewhere, except you have a line (you can use mousewhere if you want, it would still work)...So you have linex,and liney, and the coordinates for the line all in the same package. What you can do is just copy and paste the line into the brackets beside your to other variables. (Don,t use the color, or there will be to many parameters)
Pretty Easy? I think so!
Here is a little program I put together, not the best coding, but it works:
code:
setscreen ("offscreenonly;nobuttonbar;title:Math.Distance Tutorial;nocursor")
var x, y, radius : int
var x2, y2 : int
var xchange, ychange : int := 1
var xchange2, ychange2 : int := 1
var answer : string
radius := 20
x := 100
y := 20
x2 := 680
y2 := 20
loop
colorback (7)
cls
color (0)
drawfilloval (x, y, radius, radius, red)
drawoval (x, y, radius - 1, radius - 1, 0)
x += xchange
drawfilloval (x2, y2, radius, radius, red)
drawoval (x2, y2, radius - 1, radius - 1, 0)
x2 -= xchange2
if x > maxx - 20 or x < 20 then
xchange := -xchange
end if
if x2 > maxx - 20 or x2 < 20 then
xchange2 := -xchange2
end if
%The Detection
if Math.Distance (x, y, x2, y2) < radius * 2 then
Input.Pause
color (0)
locate (22, 18)
put "The Balls Have Collided All Because Of Math.Distance!"
end if
%The Detection
drawfilloval (x, y, radius, radius, red)
drawline (600, 0, 600, maxy, 12)
x += xchange
if x > maxx - 20 or x < 20 then
xchange := -xchange
end if
if Math.DistancePointLine (x, y, 600, 0, 600, maxy) < radius then
Input.Pause
color (0)
locate (22, 18)
put "The Balls Have Collided All Because Of Math.Distance!"
end if
delay (5)
View.Update
end loop
So I guess that is it for this tutorial on Math.Distance, Happy Programming |