Rotate main actor based on X position (dynamically)
Hello, I'm stuck and need a little assistance. Essentially i'm trying to constrain the rotation of an actor to it's position on the scene. So essentially while it's moving to the right it should rotate to the right slightly (or roughly angle 75 on a circle, clockwise from 90), when it's in the center of the screen it would be 90, and then when on the left it would be 105 (counterclockwise from 90). I have tried using constrain self.rotation to player.x/number, but no matter what i put in there it's just slightly off or in the wrong direction. Any assistance would be greatly appreciated.
Comments
Constrain the actor's rotation to . . . . . > ((self.Position.X-512)*-1)/34.133333333333333
Here is a demo project to see what I mean - click and drag the actor left and right:
Link: http://www.mediafire.com/?nzs772nd41v55nc
The actor is placed in the middle of the screen (512 pixels across).
So when we say make it's rotation the same as it's X position the rotation is going to be 512 degrees.
So the first bit of maths is to remove this 512 degrees rotation offset, so we stick in a minus 512.
Now we have an actor in the middle of the screen and rotated to 0 degrees.
But if you drag him to the left he turns (or the upper part of the actor turns) to the right.
(GS's rotation seems to work backwards - counterclockwise rotation gives positive values !?)
So we need now to flip the whole calculation, so when we drag the actor left he turns left.
So multiply the whole sum by minus 1 (*-1) which flips all values - now a value of say plus 362 is minus 362, and minus 44 is now plus 44 - etc.
(continued . . . )
The only thing left to sort out is the fact that when we drag to the far left we drag our actor through 512 pixels.
Which will give us minus 512 degrees of rotation, but we only want 15 degrees.
(105° on the left / 90° in the middle / 75° on the right)
So we need to divide 512 by 34.133333333333333 to get 15.
So we stick a 'divide the whole lot by 34.133333333333333' on the end.
Bam ! Job done.
Obviously you now have to drag the origin of the actor right the the edges of the screen to generate 15 degrees of rotation, which looks a bit crap, so you need to dial down the 34.133333333333333 value a little so the actor rotates the 15 degrees a bit before the edge.
Or whatever, you get the idea, adjust to taste, sorry if any of this sounded patronising, I was working it out as I wrote it.
If I am ever stuck with something like this, I find the best thing is to stick a Display Text behaviour on your actor and as you play around with the maths - keep copying every modification you make over to the Display Text behaviour - so you can see what your changes are doing to the value the calculation is producing - and watch it change (for better or for worse) as you drag your actor around.
The 'how the hell do you reverse values' question stumped me for a while too, took days to work it out, I thought there would be some function type thing in the math functions, like an inverse command or something.