Slick Forums

Discuss the Slick 2D Library
It is currently Wed Jun 19, 2013 12:06 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Rotating an animation
PostPosted: Thu Jun 09, 2011 11:49 pm 
Offline

Joined: Thu Jun 09, 2011 11:47 pm
Posts: 1
Is there any easy way to rotate an animation?
Ive been using the rotation movement system in the BasicSlickGame tutorial of spiegel, found on the documentation wiki of this site. It only works with still images, I cant apply the .rotate to an animation, is there any way to apply it to animations?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2011 3:48 am 
Offline
Regular
User avatar

Joined: Wed Apr 27, 2011 3:29 pm
Posts: 197
Location: United State of California
The trick is to rotate the graphics context first, then draw your animation.

Code:
package fg;


import org.newdawn.slick.Animation;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Test extends BasicGame {
    private Animation animation;
    private float rotation = 0.0f;
    float x = 100.0f;
    float y = 200.0f;

    public static void main(String args[]) {
        Test test = new Test("Test");
        try {
            AppGameContainer app = new AppGameContainer(test);
            app.setDisplayMode(640, 480, false);
            app.start();
        } catch (SlickException e) {
            e.printStackTrace();
        }
    }

    public Test(String title) {
        super(title);
    }

    @Override
    public void init(GameContainer container) throws SlickException {
        Image[] images = new Image[3];
        images[0] = new Image("images/0.png");
        images[1] = new Image("images/1.png");
        images[2] = new Image("images/2.png");
        animation = new Animation(images, 200);
    }

    @Override
    public void update(GameContainer container, int delta) throws SlickException {
        rotation += .2f * delta;
    }

    public void render(GameContainer container, Graphics g) throws SlickException {
        //store the current state of transform for later
        g.pushTransform();
        //rotate the display around the center of the animation
        g.rotate(x + animation.getWidth() / 2, y + animation.getHeight() / 2, rotation);
        animation.draw(x, y);
        //undo rotation so further drawing is not rotated
        g.popTransform();
        g.drawString("howdy!", 50.0f, 50.0f);
    }

}


This code would need to be refined based on how you are managing your animations/entities of course. Comment out the popTransform() line to illustrate what happens when you forget to undo the rotation applied to the graphics context.


Top
 Profile  
 
PostPosted: Sun Apr 29, 2012 12:11 pm 
Offline

Joined: Sun Apr 29, 2012 10:38 am
Posts: 13
If I understand this correctly, if I have several animated sprites, I can't simply rotate them, I need to manipulate the graphic context in order to do so?

Isn't that a bit complicated? Or do I have a wrong understanding of when/how to use animations?


Top
 Profile  
 
PostPosted: Tue May 01, 2012 1:24 am 
Offline
Regular
User avatar

Joined: Wed Apr 27, 2011 3:29 pm
Posts: 197
Location: United State of California
All it amounts to is 3 extra lines per animation. Some of which you would have to do anyway if there was such a thing as animation.rotate(). See the following:

Quote:
g.pushTransform();
g.rotate(x + animation.getWidth() / 2, y + animation.getHeight() / 2, rotation);
animation.draw(x, y);
g.popTransform();


If it's that much of a hassle, you can create some sort of entity class that will handle the rotation automatically based on the value of a variable.

_________________
Current Testing Machine:
Windows 7 x64, 4GB RAM DDR3, 3.3ghz AMD Athlon II X3 455, AMD Radeon HD 6800 series.


Top
 Profile  
 
PostPosted: Thu May 03, 2012 10:37 am 
Offline

Joined: Sun Apr 29, 2012 10:38 am
Posts: 13
thx!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot], SquidKid and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group