GSP221 iLab 3: Using Vectors for Graphics and Physics in 2D
The first Lab taught us how to construct, translate, and render an object as a set of points. In the second Lab, we were introduced to how vector math is done in C++, with just a general reference to game objects.In this third Lab, we finally put both concepts together, where we represent our object’s vertices through vectors and then use the vector math we learned in Lab 2, control the position, velocity, and acceleration of our game objects. The concepts of position, velocity, and acceleration are studies under the branch of physics termed kinematics.You will create a 2D triangle in which the vertices are represented by vectors that can be translated using vector addition. You will then simulate the triangle moving under the influence of acceleration and then stopping when it collides with a vertical wall. You can simulate gravity with a constant acceleration in the –y direction (of -9.8 m/s if you want to use meters as your units). To detect the moment of collision, you will implement a parametric ray that intersects with a line.Using Visual Studio and your code from Week 2, do the following:Place the attached Triangle2D.h file in the Geometry project (see below for the Handout link). This creates a Triangle2D class similar to Triangle3D, except one replaces Point3D with Vector2D for the representation of the vertices. This includes a constructor that takes three Vector2D to define a triangle. This also has a Translate( Vector2D T ) method, and implements it using vector addition (it does not access the individual coordinates).Implement the .cpp file for Physics.h in the Handout and use it for this lab. The only method that does anything interesting is dX() method, which computes the next position of the triangle using the current acceleration and the effect of gravity.Implement Linear.cpp for Linear.h. This provides a Ray2D class and a Line2D. Use these for collision detection between a Triangle2D and a vertical wall near the right hand board of the window. Use a ray to provide a geometric representation of the motion of the triangle. In particular, define the ray.origin as the physics.Position() and ray.direction as physics.Velocity(). Compute the intersection between this ray and the line representing the wall. RayIntersect.t should be the time it will take for the triangle to reach the wall if it were traveling in a straight line; that is, if there is no gravity. When this t is position = position ; }void Physics::Set_Velocity( Vector2D velocity ) { this->velocity = velocity ; }void Physics::Set_Acceleration( Vector2D acceleration ) { this->acceleration = acceleration ; }Vector2D Physics::Gravity() { return gravity ; }Scalar Physics::Speed() { return velocity.Length() ; }Vector2D Physics::Position() { return position ; }Vector2D Physics::Velocity() { return velocity ; }Vector2D Physics::Acceleration() { return acceleration ; }The main part of the code is here: Vector2D Physics::dX( double dt ) {Vector2D dX ; Scalar delta_t = (Scalar) dt ; Vector2D initial_velocity = velocity ;Then (i) Update velocity (ii) Use the updated velocity and initial velocity to find dx (ii) Use dx to update position.return dX ; }STEP 2: Do the Linear.cpp FileBack to top This step will essentially reduce the student to figuring out two lines of code, explained below. The rest will be copying and pasting. The two lines of code are based on the formula for t up above.Write down this code:void Ray2D::Init() { tmin = 0.0 ; tmax = Scalar( Math::_HUGE ) ; }Ray2D::Ray2D() { Init() ; }Ray2D:: Ray2D( Vector2D origin, Vector2D direction ) { Init() ; this->origin = origin ; this->direction = direction ; }/ Line /Line::Line( Vector2D point, Vector2D normal ) { this->point = point ; this->normal = normal ; this->normal.Normalize() ; }Scalar Line::Distance( Vector2D point ) { return normal * ( point – this->point ) ; }/ Intersection /RayIntersectLine Ray_Intersect_Line( Ray2D ray, Line line ) { RayIntersectLine result ; double numerator, denominator ;numerator = Write the line of code for the numerator. Remember, * means dot product denominator = Write the line of code for the denominatorHint: line.normal is the vector normal to line. Here are the type declarations for ray and line: Ray2D ray; Line line; You do not have to type these declarations anywhere, but this is just to give you an idea of their types. Consider: point, origin, normal and direction. See the pictures above of ray and line for their various components.Here is the rest of the code. This first tests whether the numerator or denominator is small. Then it returns the value of t, among other things. if ( Within_Epsilon( denominator ) ) { if ( Within_Epsilon( numerator ) )\ { result.type = RayIntersectLine::Coincident ; result.t = 0.0 ; } else { result.type = RayIntersectLine::Parallel ; result.t = Scalar( Math::_HUGE ) ; } } else { result.t = Scalar( numerator / denominator ) ; result.normal = line.normal ; result.type = RayIntersectLine::Intersect ; }return result ; }The final result will be the triangle and the sphere moving:The triangle will stop upon hitting an imaginary vertical line on the right-hand side of the screen. The sphere will continue to move until it leaves the window screen:Now that you have completed the above steps, please do not forget to add the appropriate code to GSP221.cpp to complete the lab.




