Hello,
i don't know if am able to post here or not, but i finally find the "correct" way to rotate an object and make it move toward the its rotation direction, and i decided to share it in case someone else has find himself facing the same problem i have for this whole week.
[if you found any error please correct me]
so,
1st thing you need to know is the "head" of your object, if you draw a rectangle and its rotation angle is still 0, which mean you didn't rotate it yet, then the head is filled rectangle like this picture show
i don't know if am the only one who made that error, but i was thinking that the head is the upper line
anyway, here is a very simple example on how to rotate a square and make it move forward and backward (car movement )
this is the Hero class, the object that we will rotate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
package rot;
public class Hero {
private double x, y, a; private int w, h;
public Hero(double x, double y, double a, int w, int h) {
this.x = x; this.y = y; this.a = a; this.w = w; this.h = h; }
public double getX() { return x; }
public double getY() { return y; }
public double getA() { return a; }
public int getW() { return w; }
public int getH() { return h; }
public void setA(int aa) {
a = Math.toRadians(aa); }
public void moveForward() { x += Math.cos(a); y += Math.sin(a); } public void moveBackword() { x -= Math.cos(a); y -= Math.sin(a); }
}
|
this is the Board class, it have the game loop, the game control key and the game logic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| package rot;
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.geom.AffineTransform;
import javax.swing.JPanel;
public class Board extends JPanel implements Runnable {
private Thread loop;
private Hero hero;
private int tmpAngle;
private boolean moveForward, moveBackward;
public Board() { init(); addKeyListener(new Controll()); setFocusable(true); setBackground(new Color(0, 50, 100)); setDoubleBuffered(true); setFocusable(true);
}
private void init() { hero = new Hero(400, 300, 0, 70, 50); tmpAngle = 0; moveForward = moveBackward = false;
loop = new Thread(this); loop.start();
}
public void paint(Graphics g) { super.paint(g);
Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); AffineTransform old = g2d.getTransform();
g2d.setColor(Color.white);
g2d.rotate(hero.getA(), hero.getX() + hero.getW() / 2, hero.getY() + hero.getH() / 2); g2d.drawRect((int) hero.getX(), (int) hero.getY(), hero.getW(), hero.getH());
g2d.fillRect((int) hero.getX() + hero.getW(), (int) hero.getY() + 10, 10, 30);
g2d.setTransform(old);
}
public void play() {
if (tmpAngle > 360) { tmpAngle = 0; } else if (tmpAngle < 0) { tmpAngle = 360;
}
hero.setA(tmpAngle);
if (moveForward) {
hero.moveForward(); } else if (moveBackward) { hero.moveBackword(); }
}
@Override public void run() {
while (true) { repaint(); play(); try { loop.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); }
}
}
private class Controll extends KeyAdapter {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == e.VK_UP) { moveForward = true; } if (e.getKeyCode() == e.VK_DOWN) { moveBackward = true; } if (e.getKeyCode() == e.VK_LEFT) { tmpAngle -= 5; } if (e.getKeyCode() == e.VK_RIGHT) { tmpAngle += 5; }
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == e.VK_UP) { moveForward = false; } if (e.getKeyCode() == e.VK_DOWN) { moveBackward = false; } if (e.getKeyCode() == e.VK_LEFT) {
tmpAngle -= 0; } if (e.getKeyCode() == e.VK_RIGHT) { tmpAngle += 0; }
} }
} |
and finally this is the frame class, the one that have the main class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
package rot;
import javax.swing.JFrame;
public class RotateME extends JFrame {
private final int width = 800, height = 600;
public RotateME() {
add(new Board());
setTitle("rotate me please"); setSize(width, height); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(true); setVisible(true);
}
public static void main(String[] args) { new RotateME();
}
}
|
i hope what i said is correct and can be helpful for new developers like me
thank you