-4

I'm creating a car driving game for iOS, but there's a problem:

I added these lines to my ViewController.m:

_carView.transform = CGAffineTransformMakeRotation(steeringTemp-45);

steeringTemp is a float variable wich is changed by the left and right steering buttons.

But when I run the app and press the steering button, the car is rotating in an ugly way. It seems like the center point is changing everytime and the car is rotating like a triangle. I tried to set the origin to the center of the _carView, which is an image view, but it didn't work.

Jongware
  • 22,200
  • 8
  • 54
  • 100
user3050673
  • 1
  • 1
  • 2
  • 1
    Wild guess from the fact that you're using the number 45: The angle should be defined in radians, not degrees... – T. Benjamin Larsen Dec 23 '13 at 12:55
  • You will have to provide us with more information. As it stands, the question is way to broad to be answered. – Dirk Dec 23 '13 at 12:55
  • 4
    "the Car is rotating like a Triangle" means little, as does "Ugly". Explain how what is occurring is not what you expected in geometric terms. Also see http://stackoverflow.com/questions/5725099/how-to-use-cgaffinetransformmakerotation - subtracting 45 from a value in radians doesn't make sense. – Pete Kirkham Dec 23 '13 at 12:55
  • 2
    As others have posted out, you have not explained your problem very clearly. "in an ugly way" is not descriptive. The phrase "like a triangle" doesn't make sense. "It didn't work" does not tell us anything useful. Be complete and descriptive. Also post all your code for manipulating your view, not just a single line that calculates a new transform. – Duncan C Dec 23 '13 at 16:49

1 Answers1

4

First of all let me tell you that you should be using radians, and not degrees. You can use this macro:

#define DEGREES_TO_RADIANS(angle) (angle * M_PI / 180.0)

And wrap it around your values:

CGAffineTransformMakeRotation(DEGRESS_TO_RADIANS(steeringTemp-45));

Second usually transforms go wrong if you:

  • Are rotating with the wrong anchorPoint (usually happens with clock pointers)
  • Don't pay attention to the order of transforms

Anchor Point

You need to make sure that your anchor point to the place you wanted. The anchorPoint defaults to the center, but you can set it to any point you like in the layer that is backed by the view. The anchor point has relative coordinates, so it goes from 0 to 1. (0,0) is your left top corner and (1,1) is your bottom right corner.

You can set it like this:

_carView.layer.anchorPoint = CGPointMake(0, 0);

Transforms order is important

Additionally, be careful if you apply more transforms you should pay attention to the order of those transforms. The previous transforms will always affect the next transforms. A rotation followed by a translation is way different than a translation followed by a rotation.

Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
  • Thanks for your Answer! But if I put this into my Script, it becomes the same, just a little Bitty faster. :( – user3050673 Dec 23 '13 at 14:24
  • 1
    With the information you gave to us it is hard to find the problem :/. The radians is the only problem that we can see with your question, the rest are just some things that you probably are doing wrong. Give us more info and we might help you further. – Tiago Almeida Dec 23 '13 at 14:48
  • A full 360 turn in radians is 2*PI. You can get the rest from there. For more info please check the Wikipedia page on that subject -> http://en.wikipedia.org/wiki/Radian – Tiago Almeida Dec 24 '13 at 09:45