Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Forgive me if me anything is unclear, this is a tricky thing for me to explain.

In an applet I draw a little ship gif. I have it so I can rotate the object and then increase or decrease the y axis to move the little guy around the screen. The problem it seems is that the coordinates for the ship are not in sync with those of the applet window?

For example: the boundaries for my applet window are 800 by 600. I move the ship by rotating it and then increasing or decreasing the y axis, but whenever the Y axis is exceeds 600 or is lower than 0 I get this weird trailing issue. Am I doing buffering right?

here are my "Main" and "ship" classes:

Main class:

    public class Main extends Applet implements Runnable, KeyListener
{
    ship player1;
    private Image i;
    private Graphics buffer;    
    AffineTransform affineTransform = new AffineTransform();    

    public void init()
    {
        this.setSize(800, 600);
        this.setBackground(Color.BLACK);
        addKeyListener(this);
    }

    @Override
    public void start() {
        player1 = new ship();
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void paint (Graphics g){
        Graphics2D g2d=(Graphics2D)g;
        player1.paint(g2d, this);
    }

    @Override
    public void update(Graphics g){
        Graphics2D g2d=(Graphics2D)g;
        if(i == null){
            i = createImage(this.getSize().width, this.getSize().height);
            buffer = i.getGraphics();   
        }
        buffer.setColor(getBackground());
        buffer.fillRect(0, 0, this.getWidth(), this.getHeight());       
        buffer.setColor(getForeground());
        paint(buffer);
        g2d.drawImage(i, 0, 0, this);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch(e.getKeyCode()){
        case KeyEvent.VK_RIGHT:
            player1.rotateRight();
            break;
        case KeyEvent.VK_LEFT:
            player1.rotateLeft();
            break;
        case KeyEvent.VK_UP:
            player1.moveUp();
            break;
        case KeyEvent.VK_DOWN:
            player1.moveDown(this);
            break;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }
    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void run() {
        while(true){
            repaint();
            try {
                Thread.sleep(17);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

ship class:

    public class ship {
    private Image sprite_ship;
    //private Image ship_engines_green;
    private int x = 300;
    private int y = 500;
    //private int dx = 10;
    private int dy = 10;
    private int my = 0;
    AffineTransform affineTransform = new AffineTransform(); 
    double rotation;

    public ship() {
        sprite_ship = Toolkit.getDefaultToolkit().getImage("ship.gif");
        //ship_engines_green = Toolkit.getDefaultToolkit().getImage("ship_engines_green.gif");
    }

    public void moveDown(Main M){
        y += dy;        
        System.out.println("Y axis: "+y);
        System.out.println("X axis: "+x);
    }       

public void moveUp(){
        y -= dy;        
        System.out.println("Y axis: "+y);
        System.out.println("X axis: "+x);
    }

public void rotateRight(){
    rotation += 5;
    System.out.println("Rotation : "+rotation);
}   
public void rotateLeft(){
    rotation -= 5;
    System.out.println("Rotation : "+rotation);
}
    public void paint(Graphics g, Main M){
        int spriteHeight = sprite_ship.getHeight(null);
        int spriteWidth = sprite_ship.getWidth(null);
        Graphics2D g2d=(Graphics2D)g;
        affineTransform = g2d.getTransform();
        g2d.rotate(Math.toRadians(rotation), x+spriteWidth/2, y+spriteHeight/2);
        g2d.drawImage(sprite_ship,x,y,M);
        rotation = 0;       
    }
}

much thanks for any suggestions or if anyone can point out something I'm doing that's terribly misguided.

share|improve this question
1  
Can you post a screen shot of the problem? –  Luis Estrada Mar 2 '12 at 19:15
    
Unfortunately I don't have enough "reputation" points. –  user1028270 Mar 2 '12 at 21:22
add comment

1 Answer

up vote 1 down vote accepted

I ran a few tests and your ship drawing code is incorrect.

Currently you are rotating the whole Graphics2D because you get the AffineTransform from the Graphics2D.

You need to create a new AffineTransform for the ship or keep one as a variable in your ship class.

public void paint(Graphics2D g, Main M) {
    int spriteHeight = sprite_ship.getHeight(null);
    int spriteWidth = sprite_ship.getWidth(null);

    AffineTransform affineTransform = new AffineTransform();
    affineTransform.rotate(Math.toRadians(rotation), x + spriteWidth / 2, y
            + spriteHeight / 2);
    affineTransform.translate(x, y);

    g.drawImage(sprite_ship, affineTransform, M);
}
share|improve this answer
    
Thanks a lot for the help. I'm going to try this today. –  user1028270 Mar 9 '12 at 13:24
    
@user1028270, if this helps, (which i know it does cause i tested it) please mark as Answer. –  Luis Estrada Mar 9 '12 at 16:16
    
I played with the code above. Now I'm not sure how how I can make it so I can rotate the ship and then make it move in the direction it is pointing. Using the code above it just moves up and down on the main window's y axis. –  user1028270 Mar 9 '12 at 16:54
    
You need to move the ship in the direction of your rotation. So if the speed of the ship is 'S' and the rotation is 'R' to move you need to translate your X by S*cos(R) and your Y by S*sin(R) where R is in radians. –  Luis Estrada Mar 9 '12 at 20:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.