2016-12-10 4 views
0

Ich muss eine Rotationsfunktion erstellen, die verwendet wird, um Elemente zu drehen, es funktioniert fast abgesehen von dem Versuch, -Sin zu tun. Es scheint keine Funktion zu geben, die dies erlaubt.Erstellen von Rotationsmatrix

Rotation matrix

Matrix.createRotation = function (rotation) { 

    return new Matrix(Math.cos(rotation), Math.sin(rotation), 0, 
     Math.sin(rotation), Math.cos(rotation), 0, 0, 0, 1); 
}; 
+0

Was ist mit etwas wie '(-1) * Math.sin (Rotation)'? Sollte das nicht funktionieren? Wie in der Funktion sollte so sein, 'zurück neue Matrix (Math.cos (Rotation), (-1) * Math.sin (Rotation), 0, Math.sin (Rotation), Math.cos (Rotation), 0 , 0, 0, 1); ' – TheNavigat

+0

Ja, du hast Recht, es funktioniert. – Quad117

+0

In Ihrem Beispiel negieren Sie nicht einmal das Ergebnis von 'Math.sin (Rotation)'. Es sollte '-Math.sin (Rotation)' sein - es ist schneller als Multiplikation mit -1. Wie erwartest du '-sin (Rotation)' wenn du das Ergebnis von 'sin (rotation) nicht negierst? – plasmacel

Antwort

1

Sie haben das Ergebnis von Math.sin(rotation) als -Math.sin(rotation) zu negieren:

Matrix.createRotation = function (rotation) 
{ 
    return new Matrix(
     Math.cos(rotation), -Math.sin(rotation), 0, 
     Math.sin(rotation), Math.cos(rotation), 0, 
     0, 0, 1 
    ); 
}; 

Beachten Sie, dass -Math.sin(rotation) ist schneller als (-1)*Math.sin(rotation).

Verwandte Themen