Week3 Math: The knowledge in vectors that we need to survive.

·         Please download and unzip the Week3.zip file in the Files/WeeklyExamples folder.


Note:
For all examples in this series:

1.       Make sure to hit <Run> button but pay attention and perform ALL interactions in the editor Window (the Scene Window and not the Game Window). I did not code any UI to support run time manipulation, use the Unity GameObject-Transform manipulator to change the scene and observe math at work.

2.       When manipulating the objects in the Scene Window, pay attention to the Hierarchy tab:

a.       TheWorld: all relevant objects are organized as children of TheWorld

b.      TheWorld.cs: unless otherwise specified, all interesting source code is in TheWorld.cs.

3.       NOTE: these examples are designed to show the math and NOT abstraction. Putting all relevant math in one function is not always the best way to design and implement software. This becomes especially true when you work on MP3, you should break the math apart and hide them behind appropriate object behavior classes.

Here are the examples.

1.       Speed: This is the only example where the math is abstracted into behavior class. The intent/hope is, the concept being discuss is relative straightforward, and, breaking the math apparat does not present as much of a challenge or hindrance in your learning/understanding.

a.       Unity Editor: TheWorld has three objects P1, P2, and Traveler

                                                              i.      P1 and P2: defines the line  that the Traveler travels on

                                                            ii.      Traveler: Reference to P1, P2, has a slider for controller the traveling time.

b.      Traveler behavior (Source/Model/Traveller.cs):

                                                               i.      Has instance variables:

1.       t: Time it should take to cover the entire distance between P1 and P2

2.       D: The distance between P1 and P2

3.       velocity: this is a normalized vector pointing from P1 towards P2

4.       P1, P2: the two end points. User (you) can manipulate these in the Scene Window when the game runs

                                                             ii.      CheckForReset()

1.       v: is a vector from traveler to P1

2.       if v.magnitude [or distance between traveler and P1] is greater than distance between P1/P2, reset traveler position to P1.

                                                            iii.      ComputeVelocity()

1.       velocity (the instance variable) is p2 – p1

2.       at this point, velocity’s magnitude (or length) is the distance between the two points: D

3.       normalize velocity for convenience and consistency

                                                           iv.      Update():

1.       Check for reset,

2.       Check for current velocity: dependent on the time-slider bar setting

3.       CurrentPosition = CurrentPosition + (D/t) * velocity;  // Speed = D/t

c.       What you can do: observe Traveller always travel from P1 to P2 in the time specified by the TimeSlider.

                                                               i.      In the Scene Window, move P1 and P2 positions

                                                             ii.      Change the time slider (in the Game Window)

                                                            iii.      Note, in this case, the time slider’s unit is “Number of Update”, around 1/60 second. So, observe when the time slider says 120, it takes around 2 seconds for the traveler from move from P1 to P2.

 

2.       LineToPoint: distance and projected position of a point onto a line

a.       Unity Editor: has P1, P2, PA, Projected, and Plane.

                                                               i.      P1, P2: define the line segment

                                                             ii.      PA: the red sphere representing the point we want to project onto the line segment

                                                            iii.      Projected: projecting PA onto line segment P1-P2, the size reflects how far PA is from the line segment.

                                                           iv.      Plane: there to give a sense and orientation of where is the ground. Otherwise, it may get confusing.

b.       TheWorld:

                                                               i.      All math in the Update() function. Details are in these illustrations (slides 3 to 6).

c.       What you can do: (in the Scene Window!!)

                                                               i.      Observe:

1.       the red line is ALWAYS perpendicular to the black line. Seeing things in 3D can be tricky, make sure you manipulate the camera to see what is happening.

2.       the size of the black sphere (Projected) is proportional to the length of the red line.

                                                             ii.      Interaction:

1.       Select and manipulate the position of P1, P2, and PA.

2.       You cannot move Projected (this is moved by the math in MyWorld.Update()).

 

3.       LineSphere: line-segment sphere intersections

a.       Unity Editor: has P1, P2, TheSphere, Pa, Pb, Plane

                                                               i.      P1, P2: define the line segment

                                                             ii.      TheSphere: The sphere to intersect the line segment with

                                                            iii.      Pa, Pb: the collision points (control by the math)

                                                           iv.      Plane: give a sense of orientation.

b.      TheWorld:

                                                               i.      All math in the Update() function. Details are in these illustrations (slides 7 to the end).

c.       What you can do: (in the Scene Window)

                                                               i.      Observe;

1.       The black spheres (Pa, Pb) are always located at the positions where the red sphere (TheSphere) touches the black line.

                                                             ii.      Interaction:

1.       Select any of the P1, P2, or TheSphere and move them. When TheSphere covers one of P1/P2, only Pa shows.

2.       Scale TheSphere

3.       Make sure to move the camera around to observe the response

4.        Quaternion: 4 numbers representing an axis and an angle. A rotation is an operation that rotates one vector into anther

o   Goals: show function of Quaternion AND an application of cross product

o   Shows absolute, from init to current.

o   Unity Editor: has Init, Final, and Plane

§  Init: Initial orientation (rotation), we only care about rotation in this example

§  Final: Final orientation (rotation)

§  Plane: to give sense of orientation

o   Observe: (let’s observe before looking at code), again, in Editor/Scene Window

§  Select and manipulate the rotation of Final, and observe how the Init object can be rotated in steps into the Final’s orientation.

·         Note: rotation in the Y-axis cannot be handled.

·         We will resolve this when learning about Matrix

o   TheWorld:

§  Create kNumSteps copies of intermediate “Arrow” objects (flat cylinder with a Head)

§  In Update()

·         Cross Init.up and Final.up to find the plane that is perpendicular to both vectors

·         Dot Init.up and Final.up to find the angle between the two vectors

·         Construct deltas of Quaternion to rotate from Init.up.

·         Note: the very last one, in blue, is rotated based on Quaternion.FromToRotation(), all of our Cross/Dot operations are captured by this one function.

5.       Quaternion Relative rotation with Quaternion in Unity (From previous to current)

o   Exactly same behavior as the above, the only difference is the quaternion operator calculation:

§  Example 3a: Quaternion q = Quaternion.AngleAxis(i * delta, n);

·         Rotation from Init’s to the current

§  Example 3b: Quaternion q = Quaternion.AngleAxis(delta, n);

·         Rotation incrementally with respect to the current

o   Here is a detailed explanation on what is going on.

 

6.       PlaneNormal: some insights into the plane equation

a.       Note: using our own shader

                                                               i.      Create by: RMBàCreateàShaderàStandard Surface Shader

1.       Make sure to store in the Material folder (for organization sake)

2.       Name this new shader: ShaderWithNoCull

                                                             ii.      Double click on the shader file click to bring to IDE (scary looking!! … we will attach this … SOON!)

1.       Only code: line 11: “Cull off”

2.       We want to be always see our plane, no backface culling

                                                            iii.      What is backface culling?

                                                           iv.      All materials in scene are assigned this shader

b.      Unity Editor: has ThePlane, PtOnPlane, AxisFrame

                                                               i.      ThePlane: this is the green plane where we want to study its equation. This is a Unity Quad

                                                             ii.      PtOnPlane: This is a black sphere, to verify the equation of the plane, we will make sure this sphere always lie on the plan along the closest distant from the origin (alone the normal direction of the plane)

                                                            iii.      AxisFrame: help orientate ourselves, avoid being confused in context-less 3D world

c.       Observe: start to run and note

                                                               i.      A small white line from the Green Plane. This white line is the plane normal, always perpendicular to the plane from the plane towards the positive normal vector direction

                                                             ii.      The black sphere always lies on the plane (infinite plane) defined by the equation we derived

                                                            iii.      A black line connecting the black sphere to the origin. Note, this black line is always perpendicular to the plane (in exact the same direction as the white line).

d.      Interaction:

                                                               i.      You can only manipulate the green plane (ThePlane), rotate it, or move it.

e.      Logic/Math: in TheWorld.cs, all in Update() function

                                                               i.      n is the plane normal

1.       Notice, we say n is the negative-Z axis (Remember, Cylinder is defined with height in the y-direction, now, we see the Quad has Z-being its normal direction).

2.       Make sense, as default Quad lies on the X/Y plane, and the “front”points in the negative –z direction

a.       Create a Quad, see it is on the X/Y plane, if you set z-scale does not change the quad size

b.      With the default shader/material, if you see from the positive Z-direction, it is “culled”, and from the negative direction, it is white

                                                             ii.      Two ways of getting normal

1.       -transform.forward

2.       OR, (identical results)

a.       Convert localRotation to matrix è m

b.      Get the second column from the matrix.

                                                            iii.      With the normal vector, the plane equation can be completed if we can find any point that is located on the plane

1.       center: is transform.localPosition, this point is on the plane

2.       d: now we can compute the D for the plane equation

                                                           iv.      White line: drawn from the center position (this shows the normal)

                                                             v.      PtOnPlane:

1.       Plane equation says, travel from the origin, in the normal direction by d-distance, you will be on the plane.

2.       Draw a black line to PtOnPlane

f.        What we learned:

                                                               i.      Once we have the normal, how to compute the d (use any point that is on the plane)

                                                             ii.      How to show the normal vector on the plane (the white line)

                                                            iii.      Verify the plane equation, that, d is the distance along the normal vector direction from the origin.

 

7.       PlaneToPtDist: Distance between a plane and a point

a.       Note: reusing the ShaderWithNoCull in Material for the plane

b.      Unity Editor: has ThePlane, ThePoint, Projected, Normal

                                                               i.      ThePlane: we are working with

                                                             ii.      ThePoint: the position to project onto ThePlane

                                                            iii.      Projected: the projected position

                                                           iv.      Normal: a cylinder to be shown as the projection path from ThePoint towards Projected.

c.       Observe: hit run, go into the Scene Window,

                                                               i.      Black sphere is ThePoint,

                                                             ii.      White cylinder shows perpendicular project from ThePoint onto ThePlane. This cylinder is ALWAYS perpendicular to ThePlane.

                                                            iii.      Red Sphere on the plane is the projected position, the size is proportional to the length of the white cylinder

d.      Interaction:

                                                               i.      ThePlane: rotate/move

                                                             ii.      ThePoint: move. It is easy to move the point outside of the projected position of ThePlane, in such case, you can scale ThePlane up, and in the Hierarchy Tab, double click on ThePoint, or the Normal to observe that the  Normal is always perpendicular to ThePlane. The Projected may get very small that it becomes difficult to observe.

e.      The Math: all in MyWorld.cs::Update() function

                                                               i.      Compute: n, d    // the plane equation

                                                             ii.      Compute: h,       // the perpendicular distance between ThePoint and ThePlane

                                                            iii.      Projected’s position can be computed based on: newPosition = pt + v * distant

                                                           iv.      Look at how I compute the Normal, this is quite related to LineSegment implementation in MP3. Not explain here until MP3 solution time.

 

8.       PlaneLine: Plane line intersection

a.       Note: reusing the ShaderWithNoCull in Material for the plane

b.      Unity Editor: has ThePlane, P1, P2, Pt, Normal

                                                               i.      ThePlane: we are working with

                                                             ii.      P1, P2: Define the line segment that we want to intersect ThePlane with

                                                            iii.      Pt: Intersection position between P1, P2 and ThePlane

                                                           iv.      Normal: a cylinder to be shown the normal vector of ThePlane

c.       Observe: hit run, go into the Scene Window,

                                                               i.      The two black spheres define the Black line.

                                                             ii.      Red Sphere is Pt, always on ThePlane. Note, if the line segment is too short, the red sphere may be outside of the line segment, but, always along the direction of the line intersecting the infinite plane represented by ThePlane.

                                                            iii.      White cylinder always perpendicular to ThePlane.

                                                           iv.      Red Sphere on the plane is the projected position, the size is proportional to the length of the white cylinder

d.      Interaction:

                                                               i.      ThePlane: rotate/move

                                                             ii.      P1, P2: move

e.      TheMath: All in MyWorld.cs::Update() function

                                                               i.      Compute: n, d   // the plane equation

                                                             ii.      Compute: V        // the line direction

                                                            iii.      Check to see if n and d are almost parallel (no intersection)

                                                           iv.      Compute t1: the distance one must travel alone the line segment, from P1, to reach the plane

                                                             v.      Pt:  position is computed with newPt = beingPt + distant * Dir

                                                           vi.      PlaneNormal: drawn at the center of the plane

 

9.       ReflectVector: Reflecting of a vector

a.       Continuation from the previous example.

b.      Only change: P1 is a larger sphere than P2, indicating line segment direction

c.       Hit run and observe the red, reflecting, line

d.      NOTE:

                                                               i.      V: is from P2 to P1 (pointing AWAY from the intersection position)!!

                                                             ii.      Important point: on reflection formula, all vectors are supposed to POINT AWAY from the intersection point!