1
\$\begingroup\$

I have been trying to create a smooth movement technique that would allow me to move in all directions, not just the 8 cardinal directions. I have tried to use this doing trig, and while the coordinates are being calculated correctly, drawing anything onto graphics requires me to use integers. Because of the rounding, the movement is reduced to the 8 cardinal directions again. How can I get around the draw methods only using integers?

I calculate the movement by taking the current x position, and adding the product of the speed and sin/cos of the angle of movement

public void moveForward(Block b) {

        b.x += speed * Math.sin(angle);

        b.y += speed * Math.cos(angle);
}

Here is the Block class:

import java.awt.Graphics;

public class Block {
int x, y;
public Map() {
    x=0;
    y=0;
}

public void Draw(Graphics g) {
    g.drawRect(x,y,100,100);
}

And this is the panel class that draws the movement:

import java.awt.Graphics;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JPanel;
import javax.swing.KeyStroke;


public class Panel extends JPanel{
Block block;
Movement movement = new Movement(0);
boolean mforward,mback,rright,rleft;

public Panel() {
    block = new Block();
    setKeyBinds();
    game.start();
}

Thread game = new Thread(){
    public void run(){
        while(true){
        if(mforward)
            movement.moveForward(block);
        if(mback)
            movement.moveBack(block);
        if(rright)
            movement.addangle(1);
        if(rleft)
            movement.addangle(-1);
        repaint();
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {}
        }
    }
};

public void paintComponent(Graphics g){
    super.paintComponent(g);
    block.Draw(g);
    movement.Draw(g);
}

//Following code is for setting up keymapping

private void setKeyBinds() {
    getInputMap().put(KeyStroke.getKeyStroke("UP"), "MOVEUP");
    getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "MOVEDOWN");
    getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "MOVELEFT");
    getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "MOVERIGHT");
    getInputMap().put(KeyStroke.getKeyStroke("released UP"), "STOPUP");
    getInputMap().put(KeyStroke.getKeyStroke("released DOWN"), "STOPDOWN");
    getInputMap().put(KeyStroke.getKeyStroke("released LEFT"), "STOPLEFT");
    getInputMap().put(KeyStroke.getKeyStroke("released RIGHT"), "STOPRIGHT");
    getActionMap().put("MOVEUP", moveforward);
    getActionMap().put("MOVEDOWN", moveback);
    getActionMap().put("MOVELEFT", rotateleft);
    getActionMap().put("MOVERIGHT", rotateright);
    getActionMap().put("STOPUP", stopup);
    getActionMap().put("STOPDOWN", stopdown);
    getActionMap().put("STOPLEFT", stopleft);
    getActionMap().put("STOPRIGHT", stopright);
}
AbstractAction moveforward = new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        mforward=true;
    }

};
AbstractAction moveback = new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        mback=true;
    }

};
AbstractAction rotateleft  = new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        rleft=true;
    }

};
AbstractAction rotateright = new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
    rright=true;
    }

};
AbstractAction stopup = new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
mforward=false;
    }

};
AbstractAction stopdown = new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        mback=false;
    }

};
AbstractAction stopleft = new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        rleft=false;
    }

};
AbstractAction stopright = new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        rright=false;
    }

};

}
\$\endgroup\$
0
\$\begingroup\$

Use floats for your logic & cast or round then to integers as you pass them to the drawing methods.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.