I have two objects - "Spaceship" and "Planet", obtained from the base "Obj". I have defined several classes - Circle, Triangle, Rectangle, etc., which all inherit from the Shape class.
For collision detection purposes, I want to give Obj a "form":
Dim MyShape as Shape
So in the "Spaceship" I can:
MyShape = new Triangle(blah,blah)
and in the "Planet" I can:
MyShape = new Circle(blah,blah)
I have a method (overloaded several times) that checks for conflicts between different forms, for example:
public shared overloads function intersects(byval circle1 as circle, byval circle2 as circle) as boolean
and
public shared overloads function intersects(byval circle as circle, byval Tri as triangle) as boolean
This works fine when I call a function using derived classes, for example:
dim A as new circle(blah, blah) dim B as new triangle(blah, blah) return intersects(A,B)
But when I call it using MyShape, I get an error because the method is passed a “Shape” (not a derived type) for which the method has no overload.
I could solve this by doing something like:
Public Function Translate(byval MyShape1 as Shape, byval MyShape2 as Shape )as boolean if shape1.gettype = gettype(circle) and shape2.gettype=gettype(circle) then ''//do circle-circle detection if shape1.gettype = gettype(triangle) and shape2.gettype=gettype(circle) then ''//do triangle-circle detection End Function
But that seems messy. Is there a better way?