Working with Vectors

BlitzMax Forums/BlitzMax Programming/Working with Vectors

Tibit(Posted 2005) [#1]
I'm doing some physics with vectors. Right now I use vectors for Location,Speed,Acceleration and Force.

If I where to write a function which for example creates an object at X,Y I could use a Vector instead:

Function CreateCar(X,Y)

Or

Function CreateCar(V:Vector)

To utilize this I would call the functions respecitvly:

CreateCar(X,Y)

and with vectors:

CreateCar(Location)

I think it would be nice to make my functions accept both because sometime I might want to not use vectors. Is there a smart way to do this with as little duplicate code as possible? Can I make a Function which accepts either a vector or a X,Y poss. I have a feeling there is a ultimate way of doing this in some advanced OO-way but I don't really know.

btw I have the vectormath working so if anyone would be interested in it I could post the library.


dynaman(Posted 2005) [#2]
Your going to need two different functions, blitz does not have function overloading which would have allowed this.


Tibit(Posted 2005) [#3]
Can you show how it would have been done with overlading(let's pretend it exsisted)? I have no idea but I'm sure it would be fun to know =)


Dreamora(Posted 2005) [#4]
exactly as above. 2 times the same function with different function header


dynaman(Posted 2005) [#5]
like you have above, it still would have been two different functions BUT they would have the same name.

Could have looked like this (psuedocode)

create(x,y)
localx = x
localy = y
end function

create(vectorthing)
localx = vectorthing.x
localy = vectorthing.y
end function

If you called the function with x and y it would run the first version, if you called it with a vector it would run the second version.


Tibit(Posted 2005) [#6]
Ah, I did know about overloading from Java, just forgot the technical term =)
Why can't I do this in BlitzMax? Is there any special reason?

Some ideas of mine:
Can't I use casting to check if the first parameter is a Vector then do.. or if an int then.. I think it would be possible to take an aproach like this but I'm afraid it will get messy.

Is there a way I can pass an array with 2 slots in to the function and by this make the Vector take as much space as my (X,Y). So that I in some way would do it like this:

Function CreateCar([X,Y],Color,Name$)

and when I use it I could do something like this:

CreateCar([X,Y],Color,Name$)
or with a vector
CreateCar(Location,Color,Name$)

The idea is to, inside the function, check if the argument is an array or a vector.

-
Perhaps I could make a function that accepts X,Y and returns a temporary vector. So that I could CreateCar(Poss(X,Y),Color,Name$)

I haven't tried any of these and I'm unsure if they would work. Is there a way to find a solution to this or should I just use two different but identical functions? Which I have a hard time accepting =)


dynaman(Posted 2005) [#7]
maybe you could have the function accept an argument of object, object. If the first "object" is an integer then you use the integer version, if not then assume (but you know what that does) it is a vector.

This should work for different types of objects, not sure how it would work for integers vs a real object though.

As for why function overloading is not in max, well, Mark just didn't put it in yet... (meant to sound funny, not rude)


Tibit(Posted 2005) [#8]
So it is planned?

And by accepting objects, that would work but the problem lies in having 2 parameters for X,Y and only one if it is a vector. Somehow I have to find a work-around for that or the function would be:
CreateCar(X,Y,Color,Name$)
~
CreateCar(Location,0,Color,Name$)


This is what I meant with messy, the " 0 " instead of Y when it's a vector.


tonyg(Posted 2005) [#9]
How about passing a Y value of -1 if you're using vectors?


FlameDuck(Posted 2005) [#10]
So it is planned?
Last we heard.


Tibit(Posted 2005) [#11]
Last we heard.

Great!

Tonyg, perhaps but If someone want to use negative X,Y possitions? Anyway, I'll look into it. If I find a solution I'll post it here.


Tibit(Posted 2005) [#12]
The solution was simple and clean enought!



Looks nice I think, it's easy too see what's going on. Am I right in that the garbage collector will take care of this temp vector from my Location-function? (I don't put the vectors in a list)


Sweenie(Posted 2005) [#13]
[Edit] Oops, you posted the same solution while I was typing. :)

Meanwhile perhaps you could do like this ?

Type Vector
Field x:float
Field y:float

Function Create:Vector(x_in:float,y_in:float)
Local newVector:Vector = New Vector
NewVector.x = x_in
NewVector.y = y_in
Return newVector
End Function

End Type

CreateCar(Myvector) or
CreateCar(Vector.Create(x,y))


John Pickford(Posted 2005) [#14]
Looks nice I think, it's easy too see what's going on. Am I right in that the garbage collector will take care of this temp vector from my Location-function? (I don't put the vectors in a list)


ooh good point. Now THERE is a concrete advantage of Garbage collection. In B3D you couldn't just create types instances like that without having to worry about clearing them up.

So for something like this you just need a Vectorise:Vector(X,Y) function which simply returns a new temp vector.