Vector Question
-
I am wondering about how different parts of vectors are accessed. What I mean is how does fuse know which piece of my vector I am asking about. Specifically, I have a vector called playerpos = {400,300}
print(playerpos.x) prints 400. Is x always assigned to the first value? Does it continue indefinately through x,y,z....? If so what comes after z
Thanks
-Gil -
Believe it or not w! (I think it was for width)
You can also access them using r,g,b and a or array indices [0] to [3]
-
I often wonder the same. There are times when I make something and don't understand how Fuze can understand, but it does. Or it doesn't understand something which I think it should.
-
@pianofire I did not know about r,g,b. Now, I can improve some code, where i used x,yz, w on color vectors😂
-
I knew rgba and xyzw, but I wasn't aware of the array indices, which is good to know, because I sometimes use vectors in my own functions for other properties than color or position :) (for example, for padding, border and margin)
-
@pianofire Can the rotation of an object be stored in a vector, or is it only position?
-
@Something There are no built in functions to store a rotation in a 4 element Fuze vector. However I have had much success using a quaternion representation to store 3D rotations in Fuze's 4 element vectors.
I use the following code as a blackbox to store and manipulate rotations as vectors in all my shared programs:
// Concatenate 2 Quaternions (rotations) Function QuatMult(a, b) vector c = a.w * b + b.w * a + Cross(a, b) c.w -= Dot(a, b) Return c // Rotate a vector v by a unit quaternion q Function QuatRot(q, v) Return QuatMult(QuatMult(q, v), {-q.x, -q.y, -q.z, q.w}) Function AxisAngleToQuat(Axis, Angle) float Mag = Length(Axis) If Mag > 0 Then Axis *= Sin(Angle / 2) / Mag Axis.w = Cos(Angle / 2) Else Axis = {0, 0, 0, 1} EndIf Return Axis
You can create them from an axis-angle representation, combine them in any order and apply them to any point/vector.
To apply the rotation to a 3D model in Fuze, you can call ObjectPointAt() to reset the rotation followed by RotateObject() with an axis-angle representation.
I do this in my 'Rubik's Cube Toy' program, using the formula:
axis = {q.x, q.y, q.z} mag = Length(axis) angle = ATan2(mag, q.w) * 2
while taking care to not call RotateObject() if mag is 0.
All of these formulas I obtained from Wikipedia:
https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation -
This post is deleted!