Results 1 to 1 of 1
Thread: Problem with angles
- 10-22-2011, 01:55 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 1
- Rep Power
- 0
Problem with angles
Hello,
As stated in the title, I am having a problem properly calculating the angles in relation to the player's sprite and where the mouse is on the screen.
It works fine as long as you keep the mouse a certain distance from the player, but as you get closer, the angle starts to change dramatically, and loads the wrong images.
Here is the code I have:
Any help is appreciated :)Java Code:package movePlayerGame; import java.awt.Image; import java.awt.event.*; import javax.swing.ImageIcon; import java.lang.Math; public class Player { private Image img; private double mouseX, mouseY, slopeX, slopeY; private double angle; private int x, y, mx, my; private Tiles tile; ImageIcon N = new ImageIcon(this.getClass().getResource("n.png")); ImageIcon E = new ImageIcon(this.getClass().getResource("e.png")); ImageIcon W = new ImageIcon(this.getClass().getResource("w.png")); ImageIcon S = new ImageIcon(this.getClass().getResource("s.png")); ImageIcon NE = new ImageIcon(this.getClass().getResource("ne.png")); ImageIcon NW = new ImageIcon(this.getClass().getResource("nw.png")); ImageIcon SE = new ImageIcon(this.getClass().getResource("se.png")); ImageIcon SW = new ImageIcon(this.getClass().getResource("sw.png")); boolean bne = false; boolean bnw = false; boolean bse = false; boolean bsw = false; boolean bn = false; boolean be = false; boolean bw = false; boolean bs = false; public Player(){ img = N.getImage(); x = 375; y = 275; } public void move(){ x += mx; y += my; } public int getX(){ return x; } public int getY(){ return y; } public Image getImage(){ return img; } public void mouseMoved(MouseEvent e) { mouseX = e.getX(); mouseY = e.getY(); slopeX = mouseX - x; slopeY = y - mouseY; angle = Math.toDegrees(Math.atan2(slopeX, slopeY)); if(angle >= -22.5 && angle < 22.5){ img = N.getImage(); } if(angle >= 22.5 && angle < 67.5){ img = NE.getImage(); } if(angle >= -67.5 && angle < -22.5){ img = NW.getImage(); } if(angle >= 67.5 && angle < 112.5){ img = E.getImage(); } if(angle >= -112.5 && angle < -67.5){ img = W.getImage(); } if(angle >= -157.5 && angle < -112.5){ img = SW.getImage(); } if(angle >= 112.5 && angle < 157.5){ img = SE.getImage(); } if(angle >= 157.5 || angle < -157.5){ img = S.getImage(); } } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_A) { mx = -1; } if (key == KeyEvent.VK_D) { mx = 1; } if (key == KeyEvent.VK_W) { my = -1; } if (key == KeyEvent.VK_S) { my = 1; } } public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_A) { mx = 0; } if (key == KeyEvent.VK_D) { mx = 0; } if (key == KeyEvent.VK_W) { my = 0; } if (key == KeyEvent.VK_S) { my = 0; } } }


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks