3D Mathematics

From NeoAxisWiki

Jump to: navigation, search

Contents

Code Snippets: 3D Mathematics

Introduction

3D Math is fun, or should be fun, but only if you know how to do it right in Neoaxis.


Quaternion calculation

float angle = 30; 
float halfAngle = angle * 0.5f; 
Quat rot = new Quat( new Vec3( 0, 0, MathFunctions.Sin( halfAngle ) ), 
    MathFunctions.Cos( halfAngle ) ); 

or in simple way:

Quat rot = new Angles (0, 0, 30).ToQuat();

[Contributor] betauser

[Organizer] tintin


Quaternion interpolation

float time = XXX; //from 0 to 1 
Quat rot = Quat.Slerp( oldRot, newRot, time ); 

[Code Contributor] betauser

[Organizer] tintin

Local to world coordinate

/// <summary> 
/// Converts local translation to world translation 
/// </summary> 
/// <param name="translation">The translation in local space</param> 
/// <param name="orientation">the current orientation of the object to be translated</param> 
/// <returns>Vector3 with the converted translation</returns> 
protected Vec3 GetLocalToWorld(Vec3 translation, Vec3 orientation) 
{ 
    float x, y, z;

    //store the Coordinates in x, y and z 
    x = translation.X; 
    y = translation.Y; 
    z = translation.Z; 

    //do the actual translation 
    translation.X = x * orientation.Y + y * orientation.X + z * -orientation.X; 
    translation.Y = x * -orientation.X + y * orientation.Y + z * -orientation.Y; 
    translation.Z = x * orientation.Z + y * orientation.Z + z * (1 - orientation.Z);  

    //return the translation 
    return translation; 
 
}

[Code Contributor] Qudeid

[Organizer] tintin

Personal tools