Moving Player

I am making a simple shooter game in Java for my Computer Science Final.

Currently Players can only move up and down. I was wondering how i could get them to move freely in the window.

i was thinking i might have forward and backwards with rotation.

If there is a certain function i should use please give an example of how to use it for example for using the simple drawImage


drawImage(image, xPosition, yPosition, width, height, null);

if it matters i use booleans to control player movement


public class MyThingy
{
    int x;
    int y;

    public void draw(Graphics g)
    {
        g.drawImage(myImage,x,y,null);
    }

    public void move(int xChange, int yChange)
    {
        x += xChange;
        y += yChange;
    }
}

public processInput(int keyCode)
{
    if (keyCode == KeyEvent.VK_LEFT)
    {
        myThing.move(-5,0);
    }
    else if (keyCode == KeyEvent.VK_RIGHT)
    {
        myThing.move(5,0);
    }
    if (keyCode == KeyEvent.VK_UP)
    {
        myThing.move(0,-5);
    }
    else if (keyCode == KeyEvent.VK_DOWN)
    {
        myThing.move(0,5);
    }
}

If you don’t understand the above, it’s time to go back to basics. Ask your teacher a lot more questions.

ok i like that better for move the player but i want to know how to rotate the player.

so i can hold up and left and it will turn on a lets say 5 degree angle.

I found this for a rotate script but it doesnt rotate from the center point of the player.


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClass extends JPanel {

  public void paint(Graphics g) {

    g.fillRect(0, 0, 20, 20);

    Graphics2D g2 = (Graphics2D) g;

    g2.translate(50, 50);
    g2.rotate(30.0 * Math.PI / 180.0);

    g2.scale(2.0, 2.0);

    g.setColor(Color.red);

    g.fillRect(0, 0, 20, 20);

  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}

ok i figured it out but now im a bit confused.

heres my code to rotate


public void rotatePlayer(Graphics g)
	{
		Graphics2D g2 = (Graphics2D) g;

	    g2.translate(60, 45);
	    g2.rotate(45.0 * Math.PI / 180.0);
	}

my question is what do i past to ‘g’ when i call upon ‘rotatePlayer’

[quote]my question is what do i past to ‘g’ when i call upon ‘rotatePlayer’
[/quote]
You will need to use an AffineTransform.


import java.awt.geom.AffineTransform;
AffineTransform xForm = new AffineTransform();


xForm.setToIdentity();
xForm.translate(position.getX(), position.getY());
xForm.rotate(facing, width / 2, height / 2);
g.drawImage(img, xForm, null);

He doesn’t have to learn an AT, he can do exactly what he just did - rotate the graphics context.

gfadmin: translate to the center of your sprite, call rotate by the rotation amount, then translate back to the origin and finally draw the image.

[quote]translate to the center of your sprite, call rotate by the rotation amount, then translate back to the origin and finally draw the image.
[/quote]
this is trans = (Graphics2D).getTransform() ; …do g.translate g.rotate… (Graphics2D).setTransform(trans)
:slight_smile:

Are you telling me that it ends up just calling that or that this is what you have to do?

Because you can definitely just call these:
http://java.sun.com/javase/6/docs/api/java/awt/Graphics2D.html#translate(double,%20double)
http://java.sun.com/javase/6/docs/api/java/awt/Graphics2D.html#rotate(double)

In fact, you can just call:
http://java.sun.com/javase/6/docs/api/java/awt/Graphics2D.html#rotate(double,%20double,%20double)

And combine both.

Using Graphics2D direct transforms have issues that it can seriously damage the whole screen graphics if transforms are misused or in failure because it shares the peer-transform.
As in fact I don’t have much to conceal between both methods, either choose Graphisc2D or AffineTransform to your fit. I DO prefer making AffineTransform on singular drawImage() callbacks.

What must be clear is the context of the matricial transform, that is if running from the peer graphics, (i.e. g.translate() , g.rot…) ensure to store the original context and translate correctly, because you have to restore the original state after making your painting as follows to getTransform() <-> setTransform(). Whenever an exception occurs between getTransform() <-> setTransform() you have to catch it and restore back with setTransform(toTheoriginalContextMatrixTransform). … :persecutioncomplex:

Ah, well that makes sense. Coming from an OpenGL background, I’m used to worrying about not corrupting the matrix and being smart about glPushMatrix, glPopMatrix, and glLoadIdentity. So it doesn’t seem unusual to me to have to transform back afterwards.