Jump to content

Why is Vector3 a class?


Davaris
 Share

Recommended Posts

There is no difference between a class and a struct, other than whether the members are private or public by default, respectively.

52t__nvidia.png nVidia 530M cpu.gif Intel Core i7 - 2.3Ghz 114229_30245_16_hardware_memory_ram_icon.png 8GB DDR3 RAM Windows7_Start.gif Windows 7 Ultimate (64x)

-----

IconVisualStudio16.png Visual Studio 2010 Ultimate google-Chrome.png Google Chrome PhotoshopLinkIndicator.png Creative Suite 5 icon28.gif FL Studio 10 MicrosoftOfficeLive.png Office 15

-----

csharp.png Expert cpp.png Professional lua_icon.png Expert BMX Programmer

-----

i-windows-live-messenger-2009.pngskype-icon16.pngaim_online.pnggmail.pngicon_48x48_prism-facebook.pngtunein-web.pngyahoo.giftwitter16.png

Link to comment
Share on other sites

Well, in a struct you cannot declare default values for members which must be fully assigned in the constructor (but the constructor parameters can have a default value with NET 4.0), so I see it mostly unuseful too, except very rare cases in which you have only few variables and fewer methods (well a vector class has very few variables so it could be used).

?? FRANCESCO CROCETTI ??

http://skaredcreations.com

Link to comment
Share on other sites

There must be a reason why Unity and Microsoft chose structs over classes.

 

I just checked C# 3.0 Pocket Reference:

 

Structs

A struct is similar to a class, with the following key differences:

• A struct is a value type, whereas a class is a reference type.

• A struct does not support inheritance (other than implicitly

deriving from object).

 

A struct can have all the members a class can, except:

• A parameterless constructor

• A finalizer

• Virtual members

 

A struct is used instead of a class when value type semantics

are desirable. Good examples of structs are numeric types,

where it is more natural for assignment to copy a value rather

than a reference. Because a struct is a value type, each

instance does not require instantiation of an object on the

heap. This can be important when creating many instances

of a type, for example, with an array.

 

At any rate you may want to investigate why they chose structs over classes, because MS and Unity hire the best of the best, so I assume there is a very good reason for why they both made the same decision.

 

Anyway its up to you guys. I just thought I'd point it out. :P

Win 7 Pro 64 bit

AMD Phenom II X3 720 2.8GHz

GeForce 9800 GTX/9800 GTX+

4 GB RAM

Link to comment
Share on other sites

How can they be the best of the best if they choose C#? With C++ you can have value type classes too, so there is no such limitation that classes can be only references.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

How can they be the best of the best if they choose C#? With C++ you can have value type classes too, so there is no such limitation that classes can be only references.

 

But this is C# not C++. Also who are you referring to MS, or Unity? DirectX can be used in C++ or C#. Unity can be used in C++ and C#.

Win 7 Pro 64 bit

AMD Phenom II X3 720 2.8GHz

GeForce 9800 GTX/9800 GTX+

4 GB RAM

Link to comment
Share on other sites

It doesn't matter what language it is, but class must be value type also.

 

Not in C#. See my quote about the difference between class and struct.

Win 7 Pro 64 bit

AMD Phenom II X3 720 2.8GHz

GeForce 9800 GTX/9800 GTX+

4 GB RAM

Link to comment
Share on other sites

We have a very good reason for the vectors being classes. I can assure you our code is extremely efficient, and for the implementation that Lazlo and I have worked on, the results are extremely user-friendly and quick.

 

We have yet to notice any speed issues or bottlenecks due to the nature of our math classes; however, if any such decreases or bottlenecks are discovered in the future, we will research the necessary optimizations.

52t__nvidia.png nVidia 530M cpu.gif Intel Core i7 - 2.3Ghz 114229_30245_16_hardware_memory_ram_icon.png 8GB DDR3 RAM Windows7_Start.gif Windows 7 Ultimate (64x)

-----

IconVisualStudio16.png Visual Studio 2010 Ultimate google-Chrome.png Google Chrome PhotoshopLinkIndicator.png Creative Suite 5 icon28.gif FL Studio 10 MicrosoftOfficeLive.png Office 15

-----

csharp.png Expert cpp.png Professional lua_icon.png Expert BMX Programmer

-----

i-windows-live-messenger-2009.pngskype-icon16.pngaim_online.pnggmail.pngicon_48x48_prism-facebook.pngtunein-web.pngyahoo.giftwitter16.png

Link to comment
Share on other sites

My understanding of C# structs is that they will be allocated on the stack rather than the heap which in some situations may offer some speed increases.

 

I think the recommendation is for types that should be treat like value types to be defined as structs (Usually mathematical types like points, vectors etc) and to be kept under 16 bytes (don't quote me on this).

 

I think the main speed increase will be seen in tight loops where you need to allocate a lot of these types at once as there will be no need to allocate the memory on the heap and resolve the references.

 

Other than that the actual code for the type will not change so unless you are seeing any performance problems using these types, changing them from classes to structs now is just premature optimisation.

Link to comment
Share on other sites

Not from my experience. Unity seems to only be C# or Javascript (their own version of it)

 

http://unity3d.com/support/documentation/Manual/Plugins.html

 

Plugins - Pro only feature

 

Unity has extensive support for C, C++ or Objective-C based Plugins. Plugins will work in standalones only. They are disabled when building a Web Player for security reasons.

 

This is supposed to be pro only, but I have been told there is a way to do it with the free version.

Win 7 Pro 64 bit

AMD Phenom II X3 720 2.8GHz

GeForce 9800 GTX/9800 GTX+

4 GB RAM

Link to comment
Share on other sites

Other than that the actual code for the type will not change so unless you are seeing any performance problems using these types, changing them from classes to structs now is just premature optimisation.

 

I'm thinking more in terms of assumptions people make, when they're building their games. With pass by value, or pass by reference, you can make changes to pass by value, without altering the original value. But if its pass by reference, then you alter the source.

 

So if there is one standard used by other engines and we are the only ones doing something different, then it can isolate us - if you get my drift. Anyway I don't know how much of a big deal it is, when it comes to doing conversions. I guess it depends on how complex the game part of your engine is.

Win 7 Pro 64 bit

AMD Phenom II X3 720 2.8GHz

GeForce 9800 GTX/9800 GTX+

4 GB RAM

Link to comment
Share on other sites

Unity C plugins can't call engine functions, only the engine can call the plugin functions, which is kinda useless since usually your own plugins and functions are supposed to call engine functions.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

Back on topic: Our vectors

  • inherit from an abstract class
  • implement property accessors
  • have instanced utility math functions
  • define custom operators
  • have custom constructors

All of which a struct is not allowed to do in C#/.NET.

Link to comment
Share on other sites

I'm pretty sure Structs in C# / .Net CAN...

 

  • Implement Property accessors,
  • define custom operators, and
  • have custom constructors

 

In fact, I have the Visual Studio object browser open now and I can see a number of Structs that implement at least one or more of these features: E.G. System.Drawing.Point, System.Drawing.Color and System.Drawing.Rectangle.

Link to comment
Share on other sites

I just looked up XNA and Vector3 is struct there as well.

 

Anyway as I said above, for fundamentals like Vector3, Point, string, etc, it might be an idea to see what the consensus is on the popular 3D engines using C#. As most programmers have built up libraries and game code, they won't want to maintain two versions if LWs C# is the odd man out.

 

Not that I am saying LW C# is the odd man out. The only ones I have checked is DirectX, Unity and XNA.

 

EDIT:

I just found this. Perhaps it is an argument for keeping Vector3 as a class?

 

http://dotnetperls.com/static-constructor

 

You want to determine the performance hit for static constructors in the C# programming language. Microsoft and many developers warn that static constructors on a type impose a substantial overhead. Static constructors are also called type initializers, because they refer to types, not instances. Here we test static constructors in the C# language and determine if they are useful.

 

=== Static constructor performance test in C# ===

 

Class 1: Has static constructor

Time: 3208 ms

 

Class 2: No static constructor

Time: 319 ms

Win 7 Pro 64 bit

AMD Phenom II X3 720 2.8GHz

GeForce 9800 GTX/9800 GTX+

4 GB RAM

Link to comment
Share on other sites

With the new dynamic vector design, such things are not possible with structs:

  • Inheriting from the FloatArrayManaged class.
  • Defining internal constructors in which not all variables are assigned before control is returned to the caller.

I do feel the importance of efficiency, but the design advantage gives development speed.

 

In other words: If you're worried so much about minor performance issues such as this one over development speed, have a look into C++ or C, which run faster but develop slower.

Link to comment
Share on other sites

In other words: If you're worried so much about minor performance issues such as this one over development speed, have a look into C++ or C, which run faster but develop slower.

 

My chief concern is standards, so people can use libraries they have built up without having to maintain different versions for different engines. However if the speed difference is as great as claimed, then I'd think again.

 

As for C++, I've been there and done that and now I prefer languages that are easier and more enjoyable to use. Case in point, if Python wasn't so slow, I'd be using that instead of C#.

Win 7 Pro 64 bit

AMD Phenom II X3 720 2.8GHz

GeForce 9800 GTX/9800 GTX+

4 GB RAM

Link to comment
Share on other sites

Python really is enjoyable to use. So much so, that it got me over a long episode where I was burned out from programming (C++) for a couple of years.

 

I haven't used Python for game programming though, because I keep reading comments that it is too slow. However people are working on speeding it up:

 

http://psyco.sourceforge.net/

Win 7 Pro 64 bit

AMD Phenom II X3 720 2.8GHz

GeForce 9800 GTX/9800 GTX+

4 GB RAM

Link to comment
Share on other sites

Python would be fine as a scripting language for programmers, as long as you never intend to give modders access to it because with it, they could do terrible things to a user's machine. :P

Win 7 Pro 64 bit

AMD Phenom II X3 720 2.8GHz

GeForce 9800 GTX/9800 GTX+

4 GB RAM

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...