- Home /
Question regarding how Vector3 works
I'm currently learning C++ and as an exercise I tried to duplicate the Vector3 structure. However, when changing the x, y, or z variables, I was unable to have other variables such as magnitude and normalized change. This is because, to my knowledge, C++ does not support properties.
I thought Unity3D had its API classes and structures built using C++, so how do they support changing the magnitude and normalized variables when a component variable changes?
I know that understanding how the API works isn't vital for using Unity3D, but I'm curious and since this directly relates to the Unity API, I thought I would ask it here.
I thought Unity3D had its API classes and structures built using C++
A lot of it is built on $$anonymous$$ono. This question would be better posted on the forums though.
Answer by Bunny83 · Mar 25, 2012 at 01:58 AM
Just to clarify a little bit. The Vector3 struct only contains 3 data members: x, y, z and nothing else. The Vector3 struct itself is a Mono type, however if native code function get passed a Vector3, C++ will receive a pointer to 3 floats. The memory foodprint of the struct looks exactly the same in C++.
If you want to dig a bit deeper to understand how Unity's scripting environment works, just download ILSpy which is an open source C# / CIL reflector. Open UnityEngine.dll and / or UnityEditor.dll to see how the classes are implemented. A lot functions / properties map directly to native code so you can't go deeper than that, but like Eric mentioned a lot stuff is done directly in managed code.
Only a few methods of Vector3 are native code functions (Slerp, OrthoNormalize, RotateTowards). Everything else is pure managed code.
On the other hand most stuff in the Transform-class is native code, even the .position
property:
public Vector3 position
{
get
{
Vector3 result;
this.INTERNAL_get_position(out result);
return result;
}
set
{
this.INTERNAL_set_position(ref value);
}
}
This is the magnitude property:
public float magnitude
{
get
{
return Mathf.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
}
Answer by rutter · Mar 24, 2012 at 08:39 PM
Values such as magnitude ultimately depend on the individual components of the vector. Someone building a vector library has two main choices:
Each time someone asks for the magnitude, re-calculate it based on the current components
Each time someone changes a component, cache the current magnitude
The first case would be mostly driven by accessor method which calculate values on "get".
The second case would be mostly driven by mutator methods which calculate values on "set".
If you add caching or other "lazy" operations, it can get quite complicated.
Right, I'm aware of how it can be done in C++, but Unity doesn't seem to use those methods. In Unity, you can simply change the x component, for example, using "vec3.x = 100;" as long as you're not using C#, and the magnitude and normalized variables will change automatically.
A lot of people overlook one of the most powerful elements in C# properties: they are implicitly functions.
Supposing we wanted to implement the second method I described above, we could do something like this:
private float _x = 0f;
public float x
{
get
{
return _x;
}
set
{
_x = value;
Recalc(); //rebuild other values
}
}
To someone using your interface, x
looks just like a float which you can get/set normally, but setting it can also cause other operations to fire.
If Vector3 was developed in C#, that would definitely explain it, but my understanding was the Unity API was created with C++ and only the editor was created with C#. Am I wrong in that assumption?
Yes, you're wrong. Lots of the API is built on $$anonymous$$ono; look at the $$anonymous$$athf class for example, or the terrain engine, or OnGUI, etc.
@smeffles: It's actually the other way round ;) The Unity engine is written mostly in C++, but the API is written in managed code (UnityEngine.dll). The editor is also written in C++. The editor is actually just a extension of the engine itself. Each window in the editor has it's own device context. You can draw everything in any window inside the editor ;)
The UnityEditor.dll contains most classes that made up the editor. The editor is some kind of a "special Unity game / application" that runs on a modified version of the engine.
Note: that are just my interpretations of what i know from the outside about Unity. Don't take my words for granted.