Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (775) Games in Android Showcase (230) games submitted by our members Games in WIP (856) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Libgdx Rotate a sprite 90 degress  (Read 3461 times) 0 Members and 1 Guest are viewing this topic.
Ging3rtoby
 « Posted 2014-12-24 18:27:52 »

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.
KudoDEV

JGO Ninja

Medals: 79
Exp: 6 years

Game Dev Hobbyist

 « Reply #1 - Posted 2014-12-24 18:31:21 »

Use delta to interpolate to 90degrees. like so:

something like:
 1  2 `degrees += delta * 1; //1 = speedrotate(Math.toRadians(degrees));`

And stop rotation once degrees >= 90

Mr.CodeIt

Senior Devvie

Medals: 2
Projects: 1

 « Reply #2 - Posted 2014-12-24 18:33:01 »

In the render method
 1  2  3 `float rot = 0;if(rot<90) {rot++;}sprite.setRotation(Math.toDegrees(rot));`
KudoDEV

JGO Ninja

Medals: 79
Exp: 6 years

Game Dev Hobbyist

 « Reply #3 - Posted 2014-12-24 18:34:47 »

In the render method
 1  2  3 `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:
 1  2 `degrees += delta * 1; //1 = speedrotate(degrees);`

Mr.CodeIt

Senior Devvie

Medals: 2
Projects: 1

 « Reply #4 - Posted 2014-12-24 18:37:15 »

Ah, I suppose you're talking about the delta
Ging3rtoby
 « Reply #5 - Posted 2014-12-24 18:38:45 »

 1  2  3  4  5  6  7  8  9 `    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.
KudoDEV

JGO Ninja

Medals: 79
Exp: 6 years

Game Dev Hobbyist

 « Reply #6 - Posted 2014-12-24 18:41:24 »

Change

 1 `(float) Math.toRadians(degrees)`

to:
 1 `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

Ging3rtoby
 « Reply #7 - Posted 2014-12-24 18:46:42 »

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.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 `    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;         }       }`
KudoDEV

JGO Ninja

Medals: 79
Exp: 6 years

Game Dev Hobbyist

 « Reply #8 - Posted 2014-12-24 18:52:07 »

change
 1  2  3  4  5  6  7  8  9 `if(rotating) {          degrees += delta * 1;          if(degrees != 90.0f) {             AssetLoader.shapesGridSprite.rotate(degrees);         }else {            degrees = 0.0f;            rotating = false;         }       }`

to
 1  2  3  4  5  6  7  8  9  10 `if(rotating) {    degrees += delta * 1;    if(degrees < 90.0f) {        AssetLoader.shapesGridSprite.rotate(degrees);    } else {        AssetLoader.shapesGridSprite.setRotation(90);        degrees = 0.0f;        rotating = false;    }}`

Ging3rtoby
 « Reply #9 - Posted 2014-12-24 18:57:00 »

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.

BurntPizza

« JGO Bitwise Duke »

Medals: 486
Exp: 7 years

 « Reply #10 - Posted 2014-12-24 18:57:51 »

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/
https://github.com/libgdx/libgdx/wiki/Universal-Tween-Engine

Demo: http://www.aurelienribon.com/universal-tween-engine/gwt/demo.html
KudoDEV

JGO Ninja

Medals: 79
Exp: 6 years

Game Dev Hobbyist

 « Reply #11 - Posted 2014-12-24 19:01:13 »

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.

Ging3rtoby
 « Reply #12 - Posted 2014-12-25 16:48:05 »

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
Ging3rtoby
 « Reply #13 - Posted 2014-12-27 14:58:26 »

Any help?
Nate

« JGO Bitwise Duke »

Medals: 167
Projects: 4
Exp: 14 years

Esoteric Software

 « Reply #14 - Posted 2014-12-27 16:11:51 »

Maybe scene2d actions would be easier for you.

Pages: [1]
 ignore  |  Print

 hadezbladez (24 views) 2018-11-16 13:46:03 hadezbladez (30 views) 2018-11-16 13:41:33 hadezbladez (15 views) 2018-11-16 13:35:35 hadezbladez (10 views) 2018-11-16 13:32:03 EgonOlsen (1888 views) 2018-06-10 19:43:48 EgonOlsen (1909 views) 2018-06-10 19:43:44 EgonOlsen (1265 views) 2018-06-10 19:43:20 DesertCoockie (1709 views) 2018-05-13 18:23:11 nelsongames (1395 views) 2018-04-24 18:15:36 nelsongames (2010 views) 2018-04-24 18:14:32
 Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org