Translating Graphic back to Origin after Rotation

In summary: Your Name]In summary, the user is developing a game for iPhone and is having issues with rotating and translating a graphic within a UIView. They have the correct order of operations for rotating and scaling, but may need to adjust the translation part by subtracting the current position of the graphic rather than the frame size.
  • #1
MaTHFRo
7
0
I'm developing a game for the iPhone, and I have a situation where I have a graphic that the user can drag around the screen and there are some buttons the user can push to both rotate it and enlarge it. The graphic is located within a UIView, which is just a rectangular shape that fits tightly around it.

The thing I'm having trouble with is rotating the graphic, and then translating it so it fits within the UIView correctly. When the user pushes the button to rotate, the first thing I'm doing is calculating the correct size the UIView frame needs to be to fit the rotated object. Then I do the following:

Code:
- (CGAffineTransform) transform
{  
        CGAffineTransform transform = CGAffineTransformIdentity;
        transform = CGAffineTransformRotate(transform, (M_PI * self.graphicRotationAngle) / 180);
        transform = CGAffineTransformScale(transform, self.graphicSize.width / self.graphic.size.width, self.graphicSize.height / self.graphic.size.height);
        transform = CGAffineTransformTranslate(transform, self.graphicSize.width - self.frame.size.width, self.graphicSize.height - self.frame.size.height);

        return transform;
}


It makes sense to me that in order to translate the object back to the upper left corner (0,0), i have to subtract the new size of the frame width and height value, minus the non rotated images width and height, and that should give me the translation I need. Yet it's not working, do I have the correct order of operations?

Any help would be appreciated.
 
Physics news on Phys.org
  • #2

Thank you for sharing your situation with us. I understand the challenges of developing a game and dealing with complex graphics and transformations. I have reviewed your code and it seems that you have the correct order of operations for rotating and scaling the graphic. However, I noticed that the translation part may need some adjustments.

When translating an object, the coordinates are relative to the object's current position. This means that if you want to move the object back to the upper left corner (0,0), you need to subtract the current position of the object, not the size of the frame. In your code, you are subtracting the frame size, which may not be the correct value for translation.

I suggest trying the following:

- (CGAffineTransform) transform
{
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformRotate(transform, (M_PI * self.graphicRotationAngle) / 180);
transform = CGAffineTransformScale(transform, self.graphicSize.width / self.graphic.size.width, self.graphicSize.height / self.graphic.size.height);
transform = CGAffineTransformTranslate(transform, -self.graphic.frame.origin.x, -self.graphic.frame.origin.y);

return transform;
}

In this code, we are subtracting the current position of the graphic from the translation values, which should give you the correct translation to move the object back to the upper left corner.

I hope this helps and please let me know if you have any further questions or concerns.
 

Related to Translating Graphic back to Origin after Rotation

What is meant by "Translating Graphic back to Origin after Rotation"?

Translating Graphic back to Origin after Rotation refers to the process of moving an object or image back to its original position and orientation after it has been rotated around a point. This is often necessary in computer graphics and animation to maintain the correct position and appearance of an object.

Why is it important to translate a graphic back to its original position after rotation?

Translating a graphic back to its original position after rotation is important to maintain the accuracy and integrity of the image. If an object is not translated back to its original position, it may appear distorted or out of place, which can affect the overall quality and realism of the graphic.

What are the steps involved in translating a graphic back to its origin after rotation?

The first step is to determine the coordinates of the object's original position. Then, the coordinates of the rotated object are calculated using the rotation formula. Next, the difference between the original and rotated coordinates is calculated and this difference is used to translate the object back to its original position. Finally, the object is redrawn in its original position.

Can translation back to origin be applied to any type of rotation?

Yes, translation back to origin can be applied to any type of rotation, whether it is a simple 2D rotation or a more complex 3D rotation. The process may vary slightly depending on the type of rotation and the software being used, but the basic principles remain the same.

Are there any limitations to translating a graphic back to its origin after rotation?

One limitation to consider is that if the object is rotated multiple times, each rotation will need to be accounted for in the translation back to origin process. Additionally, if the object is rotated around a different point, the translation process will need to take into account the new origin point. This can become more complex and time-consuming for complex rotations and may require advanced mathematical calculations.

Similar threads

  • Math Proof Training and Practice
2
Replies
46
Views
5K
  • Mechanics
Replies
17
Views
4K
Back
Top