I have my sprite which I want to rotate 90 degrees on ever touch. I dont want the rotation to be instant I would like it to be fade 90 degrees if you get what I mean. I get the coding its just the logic I struggle with, thanks Toby.
Use delta to interpolate to 90degrees. like so:
something like:
degrees += delta * 1; //1 = speed
rotate(Math.toRadians(degrees));
And stop rotation once degrees >= 90
In the render method
float rot = 0;
if(rot<90) {rot++;}
sprite.setRotation(Math.toDegrees(rot));
That is a bad idea as it will provide different results on different machines.
Plus Math.toDegrees() in that case is not necessary.
Make sure you interpolate properly. And to correct my code
Since the sprite class uses degrees not radians this should work:
degrees += delta * 1; //1 = speed
rotate(degrees);
Ah, I suppose you’re talking about the delta
private float degrees = 0.0f;
private void rotateSprite(float delta) {
if(Gdx.input.isTouched()) {
degrees += delta * 1;
if(degrees != 90.0f) {
AssetLoader.shapesGridSprite.rotate((float) Math.toRadians(degrees));
}else degrees = 0.0f;
}
}
I have this and what happens is if I want it to rotate I have to hold the screen down.
Change
(float) Math.toRadians(degrees)
to:
degrees
I didn’t know sprite didn’t rotate in radians.
And instead of checking if its clicked set a boolean to true the first time its activated. Then set it to false when angle is reached
Okay so mark 2, when I press once, the sprite just constantly rotates fast and doesn’t stop, although looking cool its not what I wanted.
private float degrees = 0.0f;
boolean rotating = false;
private void rotateSprite(float delta) {
if(Gdx.input.isTouched() & !rotating) {
rotating = true;
}
if(rotating) {
degrees += delta * 1;
if(degrees != 90.0f) {
AssetLoader.shapesGridSprite.rotate(degrees);
}else {
degrees = 0.0f;
rotating = false;
}
}
change
if(rotating) {
degrees += delta * 1;
if(degrees != 90.0f) {
AssetLoader.shapesGridSprite.rotate(degrees);
}else {
degrees = 0.0f;
rotating = false;
}
}
to
if(rotating) {
degrees += delta * 1;
if(degrees < 90.0f) {
AssetLoader.shapesGridSprite.rotate(degrees);
} else {
AssetLoader.shapesGridSprite.setRotation(90);
degrees = 0.0f;
rotating = false;
}
}
So thank you that was a little better, I am trying to have it rotate like this. I have got a shape with 4 sides and when I touch the screen I want it to rotate like like what they have.
Word to the wise: this is called tweening, and if you plan on doing lots of it there are purpose-built libraries to do it well. Check out the Universal Tween Engine:
http://www.aurelienribon.com/blog/projects/universal-tween-engine/
Demo: http://www.aurelienribon.com/universal-tween-engine/gwt/demo.html
For rotation like that change the speed and add a variable that degenerates as its times goes by.
I think I’ve provided you a good start! You can figure out the rest.
Thanks for the help it’s just I really struggle at the logic, could you put it into code or even pseudo code that would be really appreciated.
Thanks, Merry Christmas
Any help?
Maybe scene2d actions would be easier for you.