Talk:3d Math functions

From Unify Community Wiki
Jump to: navigation, search

The current script actually fails to compile. I believe the last function is wrong?

void VectorsToTransform(ref GameObject gameObjectInOut, Vector3 positionVector, Vector3 directionVector, Vector3 normalVector)

should really be

public static void VectorsToTransform(ref GameObject gameObjectInOut, Vector3 positionVector, Vector3 directionVector, Vector3 normalVector)

and it should be inside of the Math3D class.

Also there is no reason for this class to extend from MonoBehavior. The class should extend from nothing given it's nature.

Then the Init function should be changed to this to fix the resulting compile errors

	public static void Init(){
 
		tempChild = (new GameObject("Math3d_TempChild")).transform;
		tempParent = (new GameObject("Math3d_TempParent")).transform;
 
                tempChild.gameObject.hideFlags = HideFlags.HideAndDontSave;
                MonoBehaviour.DontDestroyOnLoad(tempChild.gameObject);
 
                tempParent.gameObject.hideFlags = HideFlags.HideAndDontSave;
                MonoBehaviour.DontDestroyOnLoad(tempParent.gameObject);
 
		//set the parent
		tempChild.parent = tempParent;
	}


I would post these changes myself but. I figure I should let the original author do this in case I am assuming something too far and am actually wrong.

small perfomance improvement

this is a pretty minor performance enhancement if it's only being called a couple times per frame, but if it were called on 1000s of vectors per frame it could add up.

original version:

	//increase or decrease the length of vector by size
	public static Vector3 AddVectorLength(Vector3 vector, float size){
 
		//get the vector length
		float magnitude = Vector3.Magnitude(vector);
 
		//change the length
		magnitude += size;
 
		//normalize the vector
		Vector3 vectorNormalized = Vector3.Normalize(vector);
 
		//scale the vector
		return Vector3.Scale(vectorNormalized, new Vector3(magnitude, magnitude, magnitude));		
	}

optimized - remove a call to Normalize, remove a Vector3 new().

	//increase or decrease the length of vector by size
	public static Vector3 AddVectorLength(Vector3 vector, float size){
 
		//get the vector length
		float magnitude = Vector3.Magnitude(vector);
 
		//calculate new vector length
		float newMagnitude = magnitude + size;
 
		//calculate the ratio of the new length to the old length
		float scale = newMagnitude / magnitude;
 
		//scale the vector
		return vector * scale;
	}

Implemented

All suggestions have been implemented. Thanks for the feedback.

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox