<< Chapter < Page Chapter >> Page >

Equations for rotating an object in 2D space

By using the information on the Spatial transformations web page along with other information on the web, we can conclude that the equations required to rotate a point around the origin in 2Dspace are shown in Figure 6 .

Figure 6 . Rotation equations for a point in 2D space.
x2 = x1*cos(alpha) - y1*sin(alpha) y2 = x1*sin(alpha) + y1*cos(alpha)

I won't attempt to derive or justify these equations. I will simply use them. If you need more information on the topic, simply Google 2D transform s and you will probably find more information than you have the time to read.

In Figure 6 , the coordinates of the original point are given by x1 and y1 , and the coordinates of the rotated point are given by x2 and y2 . The angle alpha is a counter-clockwise angle around the origin.

Houston, we have a problem

We still have a problem however. The equations in Figure 6 are for rotating a point around the origin. Our objective is to rotate a pointaround any arbitrary anchor point in 2D space.

We could probably modify the equations in Figure 6 to accomplish this. However, there is another way, which is easier to implement. It can beshown that the same objective can be achieved by translating the anchor point to the origin, rotating the object around the origin, and then translating therotated object back to the anchor point. Since we already know how to translate a point in 2D space, this is the approach that we will use.

You must be very careful

I do want to point out, however, that you really have to think about what you are doing when you rotate geometric objects, particularly when you combinerotation with translation. For example, rotating an object around the origin and then translating it does not produce the same result as translatingthe object and then rotating the translated object around the origin.

Clone the original Point2D object

Listing 14 begins by calling the new GM01.Point2D.clone method to create a clone of the object on which the rotate method was called. Theclone, referred to by newPoint , will be rotated and returned, thus preserving the original object.

Following that, Listing 14 declares working variables that will be used later.

Incoming parameters

The GM01.Point2D.rotate method in Listing 14 requires two incoming parameters. The first parameter is a referenceto a GM01.Point2D object that specifies the anchor point around which the geometric object is tobe rotated. The second parameter is the rotation angle in degrees, counter-clockwise around the origin.

Translate the anchor point to the origin

Listing 15 gets a Vector2D object that represents the displacement vector from the origin to the anchor point.

Listing 15 . Translate the anchor point to the origin.
GM01.Vector2D tempVec = new GM01.Vector2D(anchorPoint.getColMatrix());newPoint = newPoint.addVectorToPoint(tempVec.negate());

The negative of the displacement vector is used to translate the clone ( newPoint ) object, thus translating the anchor point to the origin.

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