<< Chapter < Page Chapter >> Page >

The source code for the getLength method is shown in Listing 2 . This code computes the length of the vector as the square root of the sum of thesquares of the X and Y components of the vector. Other than that code, there is no code in this program that warrants further explanation.

You can view a complete listing of the program in Listing 23 near the end of the module.

The program named VectorAdd04

This program illustrates the addition of a Vector object to a Point object producing a new Point object. Both points and the vector are drawn on the screen as shown in Figure 7 .

Figure 7 Screen output from the program named VectorAdd04.

Missing image.

The only thing that is new in this program is the call to the new addVectorToPoint method of the GM2D04.Point class to add the vector to the point producing a new point.

The source code for the addVectorToPoint method is shown in Listing 3 . You can view a complete listing of the program in Listing 24 near the end of the module.

A very powerful capability

Don't be fooled by the apparent simplicity of the addVectorToPoint method. The ability to add a vector to a point provides a powerful new capability to the game-math library. As you will see in the next module, thiscapability makes it possible not only to translate geometrical objects from one location in space to another, but also makes it possible to animate geometricalobjects.

Documentation for the GM2D04 library

Click here to download a zip file containing standard javadoc documentation for the library named GM2D04 . Extract the contents of the zip file into an empty folder and open the filenamed index.html in your browser to view the documentation.

Although the documentation doesn't provide much in the way of explanatory text (see Listing 19 and the explanations given above) , the documentation does provide a good overview of the organization and structure of the library. You may find it helpful in that regard.

Homework assignment

The homework assignment for this module was to study the Kjell tutorial through Chapter 3 - Vector Addition.

The homework assignment for the next module is to study the Kjell tutorial through Chapter 5 - Vector Direction .

In addition to studying the Kjell material, you should read at least the next two modules in this collection and bring your questions about that material tothe next classroom session.

Finally, you should have begun studying the physics material at the beginning of the semester and you should continue studying one physics moduleper week thereafter. You should also feel free to bring your questions about that material to the classroom for discussion.

Run the programs

I encourage you to copy the code from Listing 19 through Listing 24 . Compile the code and execute it in conjunction with the game-math library provided in Listing 19 . Experiment with the code, making changes, and observing the results of your changes. Make certain that youcan explain why your changes behave as they do.

Summary

In this module you learned

  • How to add two or more vectors
  • About the head-to-tail rule in vector addition
  • About the vector addition parallelogram
  • About the relationship between the length of the sum of vectors and the lengths of the individual vectors in the sum
  • How to add a vector to a point
  • How to get the length of a vector
  • How to represent an object in different coordinate frames

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask