Browse Source

Merge pull request #1313 from KellyThomas/master

Add c# samples to Vector Math
Chris Bradfield 7 years ago
parent
commit
a5cc7caecf
1 changed files with 115 additions and 18 deletions
  1. 115 18
      tutorials/math/vector_math.rst

+ 115 - 18
tutorials/math/vector_math.rst

@@ -63,10 +63,16 @@ coordinate notation. For example, in Godot the origin is the top-left
 corner of the screen, so to place a 2D node named ``Node2D`` 400 pixels to the right and
 corner of the screen, so to place a 2D node named ``Node2D`` 400 pixels to the right and
 300 pixels down, use the following code:
 300 pixels down, use the following code:
 
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
 
     $Node2D.position = Vector2(400, 300)
     $Node2D.position = Vector2(400, 300)
 
 
+ .. code-tab:: csharp
+
+    var node2D = (Node2D) GetNode("Node2D");
+    node2D.Position = new Vector2(400, 300);
+
 Godot supports both :ref:`Vector2 <class_Vector2>` and
 Godot supports both :ref:`Vector2 <class_Vector2>` and
 :ref:`Vector3 <class_Vector3>` for 2D and 3D usage respectively. The same
 :ref:`Vector3 <class_Vector3>` for 2D and 3D usage respectively. The same
 mathematical rules discussed in this article apply for both types.
 mathematical rules discussed in this article apply for both types.
@@ -75,7 +81,8 @@ mathematical rules discussed in this article apply for both types.
 
 
 The individual components of the vector can be accessed directly by name.
 The individual components of the vector can be accessed directly by name.
 
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
 
     # create a vector with coordinates (2, 5)
     # create a vector with coordinates (2, 5)
     var a = Vector2(2, 5)
     var a = Vector2(2, 5)
@@ -84,14 +91,28 @@ The individual components of the vector can be accessed directly by name.
     b.x = 3
     b.x = 3
     b.y = 1
     b.y = 1
 
 
+ .. code-tab:: csharp
+
+    // create a vector with coordinates (2, 5)
+    var a = new Vector2(2, 5);
+    // create a vector and assign x and y manually
+    var b = new Vector2();
+    b.x = 3;
+    b.y = 1;
+
 - Adding vectors
 - Adding vectors
 
 
 When adding or subtracting two vectors, the corresponding components are added:
 When adding or subtracting two vectors, the corresponding components are added:
 
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
 
     var c = a + b  # (2, 5) + (3, 1) = (5, 6)
     var c = a + b  # (2, 5) + (3, 1) = (5, 6)
 
 
+ .. code-tab:: csharp
+
+    var c = a + b;  // (2, 5) + (3, 1) = (5, 6)
+
 We can also see this visually by adding the second vector at the end of
 We can also see this visually by adding the second vector at the end of
 the first:
 the first:
 
 
@@ -106,11 +127,17 @@ Note that adding ``a + b`` gives the same result as ``b + a``.
 
 
 A vector can be multiplied by a **scalar**:
 A vector can be multiplied by a **scalar**:
 
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
 
     var c = a * 2  # (2, 5) * 2 = (4, 10)
     var c = a * 2  # (2, 5) * 2 = (4, 10)
     var d = b / 3  # (3, 6) / 3 = (1, 2)
     var d = b / 3  # (3, 6) / 3 = (1, 2)
 
 
+ .. code-tab:: csharp
+
+    var c = a * 2;  // (2, 5) * 2 = (4, 10)
+    var d = b / 3;  // (3, 6) / 3 = (1, 2)
+
 .. image:: img/vector_mult1.png
 .. image:: img/vector_mult1.png
 
 
 .. note:: Multiplying a vector by a scalar does not change its direction,
 .. note:: Multiplying a vector by a scalar does not change its direction,
@@ -158,20 +185,34 @@ Normalization
 preserving its direction. This is done by dividing each of its components
 preserving its direction. This is done by dividing each of its components
 by its magnitude:
 by its magnitude:
 
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
 
     var a = Vector2(2, 4)
     var a = Vector2(2, 4)
     var m = sqrt(a.x*a.x + a.y*a.y)  # get magnitude "m" using the Pythagorean theorem 
     var m = sqrt(a.x*a.x + a.y*a.y)  # get magnitude "m" using the Pythagorean theorem 
     a.x /= m
     a.x /= m
     a.y /= m
     a.y /= m
 
 
+ .. code-tab:: csharp
+
+    var a = new Vector2(2, 4);
+    var m = Mathf.Sqrt(a.x*a.x + a.y*a.y);  // get magnitude "m" using the Pythagorean theorem 
+    a.x /= m;
+    a.y /= m;
+
 Because this is such a common operation, ``Vector2`` and ``Vector3`` provide
 Because this is such a common operation, ``Vector2`` and ``Vector3`` provide
 a method for normalizing:
 a method for normalizing:
 
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
 
     a = a.normalized()
     a = a.normalized()
 
 
+ .. code-tab:: csharp
+
+    a = a.Normalized();
+
+
 .. warning:: Because normalization involves dividing by the vector's length,
 .. warning:: Because normalization involves dividing by the vector's length,
              you cannot normalize a vector of length ``0``. Attempting to
              you cannot normalize a vector of length ``0``. Attempting to
              do so will result in an error.
              do so will result in an error.
@@ -196,14 +237,28 @@ Godot, the :ref:`Vector2 <class_Vector2>` class has a ``bounce()`` method
 to handle this. Here is a GDScript example of the diagram above using a
 to handle this. Here is a GDScript example of the diagram above using a
 :ref:`KinematicBody2D <class_KinematicBody2D>`:
 :ref:`KinematicBody2D <class_KinematicBody2D>`:
 
 
-::
 
 
-    var collision = move_and_collide(velocity * delta)  # object "collision" contains information about the collision
+.. tabs::
+ .. code-tab:: gdscript GDScript
+
+    # object "collision" contains information about the collision
+    var collision = move_and_collide(velocity * delta)
     if collision:
     if collision:
         var reflect = collision.remainder.bounce(collision.normal)
         var reflect = collision.remainder.bounce(collision.normal)
         velocity = velocity.bounce(collision.normal)
         velocity = velocity.bounce(collision.normal)
         move_and_collide(reflect)
         move_and_collide(reflect)
-        
+
+ .. code-tab:: csharp
+    
+    // KinematicCollision2D contains information about the collision
+    KinematicCollision2D collision = MoveAndCollide(_velocity * delta);
+    if (collision != null)
+    {
+        var reflect = collision.Remainder.Bounce(collision.Normal);
+        _velocity = _velocity.Bounce(collision.Normal);
+        MoveAndCollide(reflect);
+    }
+
 Dot product
 Dot product
 ~~~~~~~~~~~
 ~~~~~~~~~~~
 
 
@@ -227,11 +282,17 @@ and
 However, in most cases it is easiest to use the built-in method. Note that
 However, in most cases it is easiest to use the built-in method. Note that
 the order of the two vectors does not matter:
 the order of the two vectors does not matter:
 
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
 
     var c = a.dot(b)
     var c = a.dot(b)
     var d = b.dot(a)  # these are equivalent
     var d = b.dot(a)  # these are equivalent
 
 
+ .. code-tab:: csharp
+
+    float c = a.Dot(b);
+    float d = b.Dot(a);  // these are equivalent
+
 The dot product is most useful when used with unit vectors, making the
 The dot product is most useful when used with unit vectors, making the
 first formula reduce to just ``cosθ``. This means we can use the dot
 first formula reduce to just ``cosθ``. This means we can use the dot
 product to tell us something about the angle between two vectors:
 product to tell us something about the angle between two vectors:
@@ -257,14 +318,23 @@ the player using ``P - A`` and normalize it. If the angle between this
 vector and the facing vector is less than 90°, then the zombie can see
 vector and the facing vector is less than 90°, then the zombie can see
 the player.
 the player.
 
 
-In GDScript it would look like this:
+In code it would look like this:
 
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
 
     var AP = (P - A).normalized()
     var AP = (P - A).normalized()
     if AP.dot(fA) > 0:
     if AP.dot(fA) > 0:
         print("A sees P!")
         print("A sees P!")
-        
+
+ .. code-tab:: csharp
+
+    var AP = (P - A).Normalized();
+    if (AP.Dot(fA) > 0)
+    {
+        GD.Print("A sees P!");
+    }
+
 Cross product
 Cross product
 ~~~~~~~~~~~~~
 ~~~~~~~~~~~~~
 
 
@@ -281,19 +351,34 @@ If two vectors are parallel, the result of their cross product will be null vect
 
 
 The cross product is calculated like this:
 The cross product is calculated like this:
 
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
 
     var c = Vector3()
     var c = Vector3()
     c.x = (a.y * b.z) - (a.z * b.y)
     c.x = (a.y * b.z) - (a.z * b.y)
     c.y = (a.z * b.x) - (a.x * b.z)
     c.y = (a.z * b.x) - (a.x * b.z)
     c.z = (a.x * b.y) - (a.y * b.x)
     c.z = (a.x * b.y) - (a.y * b.x)
 
 
-In GDScript, you can use the built-in method:
+ .. code-tab:: csharp
+
+    var c = new Vector3();
+    c.x = (a.y * b.z) - (a.z * b.y);
+    c.y = (a.z * b.x) - (a.x * b.z);
+    c.z = (a.x * b.y) - (a.y * b.x);
 
 
-::
+
+
+With Godot, you can use the built-in method:
+
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
 
     var c = a.cross(b)
     var c = a.cross(b)
 
 
+ .. code-tab:: csharp
+
+    var c = a.Cross(b);
+
 .. note:: In the cross product, order matters. ``a.cross(b)`` does not
 .. note:: In the cross product, order matters. ``a.cross(b)`` does not
           give the same result as ``b.cross(a)``. The resulting vectors
           give the same result as ``b.cross(a)``. The resulting vectors
           point in **opposite** directions.
           point in **opposite** directions.
@@ -306,9 +391,10 @@ or surface in 3D space. If we have the triangle ``ABC`` we can use vector
 subtraction to find two edges ``AB`` and ``AC``. Using the cross product,
 subtraction to find two edges ``AB`` and ``AC``. Using the cross product,
 ``AB x AC`` produces a vector perpendicular to both: the surface normal.
 ``AB x AC`` produces a vector perpendicular to both: the surface normal.
 
 
-Here is a function to calculate a triangle's normal in GDScript:
+Here is a function to calculate a triangle's normal:
 
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
 
     func get_triangle_normal(a, b, c):
     func get_triangle_normal(a, b, c):
         # find the surface normal given 3 vertices
         # find the surface normal given 3 vertices
@@ -317,6 +403,17 @@ Here is a function to calculate a triangle's normal in GDScript:
         var normal = side1.cross(side2)
         var normal = side1.cross(side2)
         return normal
         return normal
 
 
+ .. code-tab:: csharp
+
+    Vector3 GetTriangleNormal(Vector3 a, Vector3 b, Vector3 c)
+    {
+        // find the surface normal given 3 vertices
+        var side1 = b - a;
+        var side2 = c - a;
+        var normal = side1.Cross(side2);
+        return normal;
+    }
+
 Pointing to a Target
 Pointing to a Target
 --------------------
 --------------------