
-----------------------------------
upthescale
Tue Jun 27, 2006 12:48 pm

OVal detection
-----------------------------------
I am makign a fish game, where i have to eat bigger fish, and if bigger fish eat u  you lose. But the other fish are on a random size, they will always be an oval, there y size is the random part. Whats the best way to do a hit  detection with ovals?

-----------------------------------
MysticVegeta
Tue Jun 27, 2006 12:59 pm


-----------------------------------
how about Math.Distance??

-----------------------------------
upthescale
Tue Jun 27, 2006 1:12 pm


-----------------------------------
I am trying,  but that is usually for circles, not ovals

-----------------------------------
the_short1
Tue Jun 27, 2006 2:05 pm


-----------------------------------
..yes you can.. math.distance can be used... but not directly..
http://en.wikipedia.org/wiki/Ellipse
you the properties of the ellipse... and some advanced math concepts.. if you havnt learned about ellipses in math.. i would recomend making them circles instead..  i can do it .,., but tahts calculus and discreet under my belt ;) . . and i dont have time to write it for you.. srry

-----------------------------------
Mazer
Wed Jun 28, 2006 2:05 pm


-----------------------------------
You could try to work your way through [url=http://www.vbforums.com/showpost.php?s=85c60b5e66e15c22a544ff13c2f3af56&p=2230047&postcount=5]this.

If that's too complicated and/or the ovals in question are long enough you might want to try treating them as rectangles for a rough approximation of collision detection (or better yet, a series of smaller rectangles that make up a rough oval shape).

-----------------------------------
MysticVegeta
Wed Jun 28, 2006 3:08 pm


-----------------------------------
Here is a demo I made to detect for oval collision: 

Advantages are: Well that you have oval detection now. 
Disadvantages are: Its too slow, Its only valid for integral x,y values of collision. ie: If the ovals only collide when x not= n and y not= n, where n is an integer, then it wont work.

var x1 := Rand.Int (1, maxx)
var x2 := Rand.Int (1, maxx)

var y1 := Rand.Int (1, maxy)
var y2 := Rand.Int (1, maxy)

var c := Rand.Int (20, 200)
var c1 := Rand.Int (20, 200)
var c2 := Rand.Int (20, 200)
var c3 := Rand.Int (20, 200)

drawfilloval (x1, y1, c, c1, brightred)
drawfilloval (x2, y2, c2, c3, brightblue)

fcn collide (x1 : int, y1 : int, x2 : int, y2 : int, c : int, c1 : int, c2 : int, c3 : int, p : int, q : int) : boolean
    /* GENERAL EQUATION OF AN ELLIPSE IS:
     (X - H) ^ 2   +   (Y - K) ^ 2
     -----------      -------------  = 1
     A^2               B^2

     A is Radius # 1.
     B is Radius # 2.
     (H, K) is the centre. Here, it cant be negative, but it could if graphed on Cartesian plane.
     (X, Y) are non-constant variables.

     Strategy: Loop through all (x, y) plug in to see whether any of the coordinates satisfy equation for both ellipses
     */
    for x : 1 .. p
        for y : 1 .. q
            var eq1 := (((x - x1) ** 2) / (c ** 2)) + (((y - y1) ** 2) / (c1 ** 2))
            var eq2 := (((x - x2) ** 2) / (c2 ** 2)) + (((y - y2) ** 2) / (c3 ** 2))
            if (round (eq1) = 1 and round (eq1) = round (eq2)) then
                result true
            end if
        end for
    end for
    result false
end collide

if (collide (x1, y1, x2, y2, c, c1, c2, c3, maxx, maxy)) then
    put "These ovals collide"
else
    put "These ovals do not collide"
end if


-----------------------------------
BenLi
Wed Jun 28, 2006 3:59 pm


-----------------------------------
surprised that nobody said it yet... but you could always dumb it down to whatdotcolor...

-----------------------------------
MysticVegeta
Wed Jun 28, 2006 4:15 pm


-----------------------------------
And how do you suppose you would do that with whatdotcolor? There is no way you can do that with whatdotcolor unless ofcourse you are Andy. but it will be complicated since you cant really loop thourgh all the pixels of any of the ovals.

-----------------------------------
MysticVegeta
Wed Jun 28, 2006 4:42 pm


-----------------------------------
Hmm I am sort of wondering if we can use some geometry to do this, for eg: using major and minor radii. I have got this so far but it doesnt give correct answer all the time, there are 2 methods I used:

var x1 := Rand.Int (1, maxx)
var x2 := Rand.Int (1, maxx)

var y1 := Rand.Int (1, maxy)
var y2 := Rand.Int (1, maxy)

var c := Rand.Int (20, 200)
var c1 := Rand.Int (20, 200)
var c2 := Rand.Int (20, 200)
var c3 := Rand.Int (20, 200)

drawfilloval (x1, y1, c, c1, brightred)
drawfilloval (x2, y2, c2, c3, brightblue)

fcn collide2 (x1 : int, y1 : int, x2 : int, y2 : int, c : int, c1 : int, c2 : int, c3 : int) : boolean
    %  if (Math.Distance (x1, y1, x2, y2) 