Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (748)
Games in Android Showcase (226)
games submitted by our members
Games in WIP (834)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Libgdx Rotate a sprite 90 degress  (Read 2916 times)
0 Members and 1 Guest are viewing this topic.
Offline 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.
Offline 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 = speed
rotate(Math.toRadians(degrees));


And stop rotation once degrees >= 90

Offline 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));
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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 = speed
rotate(degrees);

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

Offline 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;
         }
       }
Offline 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;
    }
}

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

http://gamejolt.com/games/arcade/shapedown/29861/
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
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
Offline 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.

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

Any help?
Offline 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  
 
 

 
xxMrPHDxx (345 views)
2017-12-31 17:17:51

xxMrPHDxx (111 views)
2017-12-31 17:15:51

xxMrPHDxx (160 views)
2017-12-28 18:11:33

Ecumene (362 views)
2017-09-30 02:57:34

theagentd (505 views)
2017-09-26 18:23:31

cybrmynd (557 views)
2017-08-02 12:28:51

cybrmynd (488 views)
2017-08-02 12:19:43

cybrmynd (462 views)
2017-08-02 12:18:09

Sralse (425 views)
2017-07-25 17:13:48

Archive (1347 views)
2017-04-27 17:45:51
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!