setScale to unit scale

Minor issues with the Ogre API that can be trivial to fix
User avatar
zarfius
Gnome
Posts: 367
Joined: Wed Jan 03, 2007 12:44 pm
Location: Brisbane, Australia
x 13

setScale to unit scale

Post by zarfius »

Just a quick one. Probably the most common calculation I do in my code all over the place is this:

Real unitScale = 5;
sceneNode->setScale(Vector3::UNIT_SCALE * unitScale);

It would be nice if there was an overridden method to make this simpler, even if only to save typing ;)

setScale(Real unitScale);
Craftwork Games - hand crafted entertainment.
http://www.craftworkgames.com/
User avatar
so0os
Bugbear
Posts: 833
Joined: Thu Apr 15, 2010 7:42 am
Location: Poznan, Poland
x 33

Re: setScale to unit scale

Post by so0os »

I'd say it should be forced to vectors automagically (explicit constructor?)
Sos Sosowski :)
http://www.sos.gd
User avatar
zarfius
Gnome
Posts: 367
Joined: Wed Jan 03, 2007 12:44 pm
Location: Brisbane, Australia
x 13

Re: setScale to unit scale

Post by zarfius »

so0os wrote:I'd say it should be forced to vectors automatically (explicit constructor?)
Sorry, I'm not sure what you mean by explicit constructor? Do you mean changing the Vector3 class so that numbers are automatically converted to Vector3? In other words, this:

Code: Select all

Vector3 v = 5;
would have the same effect as:

Code: Select all

Vector3 v(5,5,5);
To be honest, I think that's overkill because you'd have to modify a lot more code (Vector2, Vector3, etc) and it might have an negative effect on existing overloaded methods. Also, it doesn't make sense to me to have a unit vector in many other situations. For example:

Code: Select all

Vector3 scale = 5; // makes sense
Vector3 position = 5; // doesn't make sense
Vector3 normal = 5; // doesn't make sense
Vector3 direction = 5; // doesn't make sense
You get the idea.
Craftwork Games - hand crafted entertainment.
http://www.craftworkgames.com/
User avatar
so0os
Bugbear
Posts: 833
Joined: Thu Apr 15, 2010 7:42 am
Location: Poznan, Poland
x 33

Re: setScale to unit scale

Post by so0os »

zarfius wrote:

Code: Select all

Vector3 v = 5;
Apparently, this works.
Sos Sosowski :)
http://www.sos.gd
User avatar
zarfius
Gnome
Posts: 367
Joined: Wed Jan 03, 2007 12:44 pm
Location: Brisbane, Australia
x 13

Re: setScale to unit scale

Post by zarfius »

so0os wrote:
zarfius wrote:

Code: Select all

Vector3 v = 5;
Apparently, this works.
Fair enough, I stand corrected. I mostly use C# with Mogre these days so I didn't have time to test it.

While we are on the topic I just remembered a way to do it in C# using a relatively new feature called extension methods. Basically, they allow you to add functionality to existing classes without the need to recompile any dependencies. For example, you can add an extension to a SceneNode like this:

Code: Select all

        public static void SetScale(this SceneNode sceneNode, float unitScale)
        {
            sceneNode.SetScale(Vector3.UNIT_SCALE * unitScale);
        }
Then somewhere else in your code you can call your new method directly from any scene node like this:

Code: Select all

sceneNode.SetScale(5.0f);
More information can be found here: http://msdn.microsoft.com/en-us/library/bb383977.aspx

The only downside is that extension methods only work in later versions of the .NET framework and currently my Mogre.dll is compiled against .NET framework 2.0 for reason's I won't go into. Anyway, at least there is a possible solution for everyone :)
Craftwork Games - hand crafted entertainment.
http://www.craftworkgames.com/