Best wat to convert user defined vector class to unity's vector class

I’m writing a physics engine as part of a university project, with requirements of every single class in the project to be written from scratch. I was wondering how could I convert a user defined vector class to unity’s vector class so it can be applied to the transform component. At first I thought of writing a method that essentially copies all the vector components from my class to unity’s class. However, is there any better approach than this?

You probably want to create a user-defined conversion operator like in the article. Then you can easily cast your Vector class to Unity’s anywhere in your code. You’ll still end-up writing the method to copy everything, but it’s really only 3 floats in the end.

Start by changing the names and all your functions to their Unity equivalents. Then just replace completely. I would use Pre processor to toggle the new and old version until you’re sure everything is right.

A bit of nitpicking. It won’t be “casting”, it will be a conversion, with a cost.

Ain’t going to work with his assignment restrictions.

1 Like

I missed that part. I guess it depends on what you want to do. If you’re supposed to write everything yourself, isn’t converting to an internal type cheating anyway? If not, can you simply make your class a container for a Unity object?

You could also simply write the transformations, they aren’t terrible difficult. The vector code is also open source so you don’t really even need to do the algebra, just copy the algorithm. Don’t just copy the code though, clean it up and make it minimal for your classes.

He’ll need to talk to the engine at some point. The engine doesn’t take custom types. And the assignment doesnt’ seem to be “write an FreeDOS protected mode software rasterizzer from scratch”.

1 Like

Fair point, he’ll definitely need to convert at some point. The only reason I mentioned it is that he wanted to use the transformation function, so I’m not entirely sure how much of the vector class he can actually use and what the point of the exercise is. It’s strange that he’s supposed to write vector classes from scratch, but then just convert and use the unity maths anyway? I felt the point of the assignment could have been to teach how and why things like dot/cross products are used… I was more just trying to make the poster aware that they can find the unity source code to all that vector math so they could use it as a learning tool.

They just say it’s to be “applied to the transform component”. I assume they mean setting position, rotation and scale, which they have to calculate themselves to meet assessment requirements.

1 Like