Character rotation problem in tiled map
Hi I am trying to rotate the character in a tiled map. Its is successful but theres problem with moving diagonally. When i pressed for example up and right keypressed, when its moving it will rotate as i wanted but when i released the buttons, it either rotate to the up or right. I want the image to rotate diagonally and stay as it is when i released the buttons.
Here are my source code...I noe its very long but really need someone help...There are 3 java files. (Sorry for dis long and ridiculous codes...)
1) Game.java
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
publicclass Gameextends Canvasimplements KeyListener
{
private BufferStrategy strategy;
private GameMap map=new GameMap();
private Player player;
privateboolean left,right,up,down;
Image ship = Toolkit.getDefaultToolkit().getImage("res/up1.PNG");
public Game()
{
Frame frame =new Frame("Pirate Game");
frame.setLayout(null);
setBounds(0,30,480,510);
frame.add(this);
frame.setSize(480,510);
frame.setResizable(false);
// exit the game
frame.addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.addKeyListener(this);
addKeyListener(this);
frame.setVisible(true);
createBufferStrategy(2);
strategy = getBufferStrategy();
player =new Player(ship, map, 1.5f, 1.5f);
// start the game loop
gameLoop();
}
publicvoid gameLoop()
{
boolean gameRunning =true;
long last = System.nanoTime();
// keep looking while the game is running
while (gameRunning)
{
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
// clear the screen
g.setColor(Color.black);
g.fillRect(0,0,480,480);
// render our game objects
g.translate(0,0);//placing the map to desired location on the frame
map.paint(g);
player.paint(g);
// flip the buffer so we can see the rendering
g.dispose();
strategy.show();
// pause a bit so that we don't choke the system
try{ Thread.sleep(4);}catch (Exception e){};
long delta = (System.nanoTime() - last) / 1000000;
last = System.nanoTime();
for (int i=0;i<delta / 5;i++)
{
logic(5);
}
if ((delta % 5) != 0)
{
logic(delta % 5);
}
}
}
publicvoid logic(long delta){
// check the keyboard and record which way the player
// is trying to move this loop
float dx = 0;
float dy = 0;
if (left)
{
dx -= 1;
}
if (right)
{
dx += 1;
}
if (up)
{
dy -= 1;
}
if (down)
{
dy += 1;
}
// if the player needs to move attempt to move the entity
// based on the keys multiplied by the amount of time thats
// passed
if ((dx != 0) | (dy != 0))
{
player.move(dx * delta * 0.0015f,dy * delta * 0.0015f);
}
}
publicvoid keyTyped(KeyEvent e){}
publicvoid keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_LEFT)
{
left =true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
{
right =true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN)
{
down =true;
}
if (e.getKeyCode() == KeyEvent.VK_UP)
{
up =true;
}
}
publicvoid keyReleased(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_LEFT)
{
left =false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
{
right =false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN)
{
down =false;
}
if (e.getKeyCode() == KeyEvent.VK_UP)
{
up =false;
}
}
publicstaticvoid main(String args[])
{
new Game();
}
}
2) GameMap.java
import javax.swing.*;
import java.awt.*;
import java.util.*;
publicclass GameMap
{
int width = 15;
int height =15;
staticfinalint TILE_SIZE = 32;
int[][] A ={{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,3,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,2,2,2,2,2,2,2,2,2,2,2,2,2,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
Image sea = Toolkit.getDefaultToolkit().getImage("res/sea.PNG");
Image rock = Toolkit.getDefaultToolkit().getImage("res/rock.PNG");
publicvoid paint(Graphics g)
{
for(int across = 0; across >< width ; across++)
{
for(int vert = 0; vert < height ; vert++)
{
if (A[across][vert] == 1)
{
g.drawImage(rock,across*TILE_SIZE,vert*TILE_SIZE,null);
}
else
{
g.drawImage(sea,across*TILE_SIZE,vert*TILE_SIZE,null);
}
}
}
}
publicboolean blocked(float x,float y)
{
return A[(int) x][(int) y] == 1;
}
}
3) Player.java
import java.awt.Graphics2D;
import java.awt.Image;
publicclass Player{
privatefloat x;
privatefloat y;
private Image image;
private GameMap map;
privatefloat ang;
privatefloat size=0.3f;
public Player(Image image, GameMap map,float x,float y)
{
this.image = image;
this.map = map;
this.x = x;
this.y = y;
}
publicboolean move(float dx,float dy)
{
// new position
float nx = x + dx;
float ny = y + dy;
//check collision
if (validLocation(nx, ny)){
x = nx;
y = ny;
// and calculate the angle we're facing based on our last move
ang = (float) (Math.atan2(dy, dx) + (Math.PI / 2));
returntrue;
}
// if it wasn't a valid move don't do anything apart from tell the caller
returnfalse;
}
publicboolean validLocation(float nx,float ny)
{
if (map.blocked(nx - size, ny - size))
{
returnfalse;
}
if (map.blocked(nx + size, ny - size))
{
returnfalse;
}
if (map.blocked(nx - size, ny + size))
{
returnfalse;
}
if (map.blocked(nx + size, ny + size))
{
returnfalse;
}
returntrue;
}
publicvoid paint(Graphics2D g){
int xp = (int) (map.TILE_SIZE * x);
int yp = (int) (map.TILE_SIZE * y);
// rotate the sprite based on the current angle and then
// draw it
g.rotate(ang, xp, yp);
g.drawImage(image, (int) (xp - 16), (int) (yp - 16),null);
g.rotate(-ang, xp, yp);
}
}

