2014-12-09 14:30:07 (edited by nyanchan 2014-12-09 14:32:19)

Hi.
I want to create vector normalization function in BGT, but have an error. It says that object handle for vector object isn't supported.
Is it really impossible to use vectors as parameters or return value? Here is the current script.
vector@ normalize(vector@ pv){
//Normalizes the specified vector
float shrink=1/pv.length();
return vector(pv.x*shrink,pv.y*shrink);
}
void main(){
vector test(3,3);
vector ret=normalize(test);
alert("Result","The normalized test vector's x value is "+ret.x+", and y value is "+ret.y+"");
exit();
}
I'm experimenting an advanced object movement that requires a lot of physical calculation.

I don't speak as good as I write, and I don't listen as good as I speak.

2014-12-09 21:46:27

This is a little weird, but I think the vector object is setup to let you bypass handles, by overloading opAssign and opCmp.
You can probably remove the @ from the function, but if you keep it, this line:
vector ret=normalize(test);
Probably needs the @:
vector@ ret=normalize(test);

But I think, for vectors, you can ignore handles entirely.

I was confused, originally, but all of these functions appear to work:

 vector rotate(vector p, vector o, double theta) {
 vector r;
r.x = (cosine(theta) * (p.x-o.x)) - (sine(theta) * (p.y-o.y)) + o.x;
r.y = (sine(theta) * (p.x-o.x)) + (cosine(theta) * (p.y-o.y)) + o.y;
 return r;
 }// Rotate.
vector get_vector(double x, double y, double z=0) {vector ret(x, y, z); return ret;}


vector cross(vector a, vector b) {
 vector c;
c.x = (a.y*b.z) - (a.z*b.y);
c.y = (a.z*b.x) - (a.x*b.z);
c.z = (a.x*b.y) - (a.y*b.x);
 return c;
}

double dot(vector a, vector b) {
return (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
}

It does present problems, if, for example, you need a vector to be null. I got around the uncertainty by assigning a vector I would never use to represent null, but this only works if you are sure you'll never need that specific vector.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2014-12-09 23:41:05

Wait.  Does bgt not support the c-style pointer dereference?
In C/C++, you'd handle interoperating between pointres and functions taking and returning by value like this:

result = normalize(*vec);
*vec = whatever(some_param1);

Does BGT not have an equivalent?  Because from where I'm standing it's a lot better than using a magic vector value.  It should at least crash instead of invisibly doing the wrong thing if you accidentally use a null handle, but maybe the language doesn't allow it.

My Blog
Twitter: @ajhicks1992

2014-12-10 02:12:03

BGT's handles and C-style pointers are different, not in especially clear ways. I think it's more analogous to C#, but I haven't actually paid enough attention to C# to be sure.

Handles function like pointers to objects, and most of the time this is pretty stable: use handles for everything, except initializing the object that you will then attach to a handle.
Vectors and arrays can get away with being treated as primitives, for all sorts of fun confusion like the above.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2014-12-10 04:14:46

I got curious.  You can do that operator overloading yourself and it turns out that Bgt has better documentation for Angelscript than the Angelscript manuals.  I know because I looked at using Angelscript once (briefly, because I read the manual and it was, shall we say, special).
I can't find anything definitive on the semantics of handles, though I'm kind of thinking of them as Python-but-with-syntax.  I'm assuming they're at least good enough to handle circular references.  On account that there seems to be no special new syntax, I think that the problem here might simply be that the function is trying to return by value but is configured to return by reference.  The fact that I'm a really advanced C++ programmer and cannot definitively answer this question is disturbing me.
As for the thing here, though, if it's going to be a lot, consider not returning anything by value.  You could restructure so that the result of any vector-to-vector operation puts its answer in the first result or so that all functions take a third parameter.  In terms of performance, it might or might not help; measuring is your god when optimizing.
But on the other hand-um, well, you can do these calculations in pure Python for hundreds and hundreds of units and still run in realtime, and if you're considering going beyond a certain point you should just use ODE or Box2d and leave BGT because getting that math right is hard even for people who fully understand what's going on.  Since Python is actually slower than BGT, or at least as far as I know, I'd not worry about it too much.

My Blog
Twitter: @ajhicks1992

2014-12-12 04:33:59

Hmm, weird thing, but thanks for help.

I don't speak as good as I write, and I don't listen as good as I speak.