Reputation:
I am trying to move a circle in a straight line. But my Code is not giving expected results. I m using mouseMotionListener to constantly get the target points and x_pos and y_pos are co-ordinated for my circle. I am using trigonometric function sin cos and atan, to move the object in straight line --its a logic i have seen here from a question posted on stack overflow-- but it is not giving me the expected results, am i doing something wrong plz help:
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.*;
public class Snake extends JPanel {
static Snake snake = new Snake();
static Co_Ordinates co = new Co_Ordinates();
static int x_pos = 10, y_pos = 500;
static int slope;
int delta_x,delta_y;
double direction;
double x_inc,y_inc;
public Snake() {
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(Color.green);
g2d.fillArc(x_pos, y_pos, 20, 20, 0, 360);
try {
Thread.sleep(10);
} catch (Exception e) {
}
/*if(x_pos==co.x_Cur){
if(co.y_Cur>=y_pos)
y_pos++;
else if(co.y_Cur<=y_pos)
y_pos--;
}
else if(y_pos==co.y_Cur){
if(co.x_Cur>=x_pos)
x_pos++;
else if(co.x_Cur<=x_pos)
x_pos--;
}
*/
//slope=((co.y_Cur - y_pos) / (co.x_Cur - x_pos));
//y_pos = slope*x_pos+y_pos;
//x_pos++;
//System.out.println("...");
if(x_pos!=co.x_Cur&&y_pos!=co.y_Cur){
int delta_x = co.x_Cur - x_pos;
int delta_y = co.y_Cur - y_pos;
direction = Math.atan(delta_y / delta_x); // Math.atan2(deltaY, deltaX) does the same thing but checks for deltaX being zero to prevent divide-by-zero exceptions
double speed = 5.0;
x_inc = (speed * Math.cos(direction));
y_inc = (speed * Math.sin(direction));
x_pos+=x_inc;
y_pos+=y_inc;
}
//x_pos = co.x_Cur;
repaint(10);// parameters
}
public void move() {
}
public static void main(String args[]) {
JFrame jf = new JFrame();
jf.add(co);
jf.addMouseMotionListener(co);
jf.addMouseListener(co);
jf.add(snake);
jf.setSize(600, 600);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
class Co_Ordinates extends JPanel implements MouseMotionListener, MouseListener {
static int slope;
static Snake sn = new Snake();
static int x_Cur=sn.x_pos-20;
static int y_Cur=sn.y_pos-40;
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
x_Cur = e.getX() - 20;
y_Cur = e.getY() - 40;
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}// Class Listener ends here
Upvotes: 0
Views: 1102
Reputation: 2435
In the code where you used Math.atan
should be changed to Math.atan2
.
atan
only gives angle values between -90deg and 90deg.
atan2
gives angle values between -180deg and 180deg.
I have modified that part of the code as below.
atan
to atan2
Please try with this one.
// if(x_pos!=co.x_Cur&&y_pos!=co.y_Cur){
delta_x = co.x_Cur - x_pos;
delta_y = co.y_Cur - y_pos;
direction = Math.atan2(delta_y, delta_x);
double speed = Math.sqrt(delta_x*delta_x + delta_y*delta_y);
speed = Math.min(speed, 5.0);
x_inc = (speed * Math.cos(direction));
y_inc = (speed * Math.sin(direction));
x_pos+=x_inc;
y_pos+=y_inc;
//}
Upvotes: 1