Results 1 to 6 of 6
  1. #1
    nav123 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default Turret tank movement

    I have to make a java game for my school project. I am done with the tank and it moves all over the place. The only thing i am not able to figure out is the movement of turret. The turret need to point towards the mouse and move accordingly.

    Someone help ASAP. :(

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello and welcome to the forum. Unfortunately I doubt if any one can provide you with any helpful advice until we know a lot more about your current program, your current problem, and what you've tried to do to solve the problem and how it's not working. Usually the more specific the problem and the more useful information presented in the question, the better the answer.

    Please see the link in my signature below on how to ask smart questions as it will provide you with useful tips on how to give us the information we need to begin to be able to help you.

    Much luck.

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    you'll need to read up on this:
    How to Write a Mouse-Motion Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

    to track the mouse position. then simply set the position of your turret according to the mouse position

  4. #4
    nav123 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    package guiexample;

    import java.awt.MouseInfo;
    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.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import sun.audio.*;

    class TankController1 implements KeyListener, MouseListener, MouseMotionListener {

    private Tank tank = null;
    private TankView view = null;
    int n, check = 0;
    double w, h;

    public void keyPressed(KeyEvent e) {
    System.out.println(tank.ra);
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
    tank.ra += 0.1;
    if (tank.ra < 2 * Math.PI) {
    System.out.println(tank.ra);
    tank.ra = 360;
    }
    } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
    tank.setVy(0);
    tank.ra -= 0.1;
    if (tank.ra < 2 * Math.PI) {
    System.out.println(tank.ra);
    tank.ra = 360;
    }
    } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
    tank.setVy(tank.getVy() + 0.2);
    System.out.println("y= " + tank.getY());
    } else if (e.getKeyCode() == KeyEvent.VK_UP) {
    tank.setVy(tank.getVy() - 0.2);
    }

    public Tank getTank() {
    return tank;
    }

    public void setTank(Tank tank) {
    this.tank = tank;
    }

    public TankView getView() {
    return view;
    }

    public void setView(TankView view) {
    this.view = view;
    }

    public void mouseMoved(MouseEvent e) {
    /* System.out.println("y= " + tank.getY());
    System.out.println("m y= " + MouseInfo.getPointerInfo().getLocation().y);
    System.out.println("m x= " + MouseInfo.getPointerInfo().getLocation().x);*/
    //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void doUpdateGameLogic() {
    tank.setY(tank.getY() + tank.getVy());
    }
    }
    This is all i have right now. I am using MVC architecture for this program.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,573
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by nav123 View Post
    This is all i have right now. I am using MVC architecture for this program.

    Java Code:
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
    tank.ra += 0.1;
    if (tank.ra < 2 * Math.PI) {
    System.out.println(tank.ra);
    tank.ra = 360;
    }
    } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
    tank.setVy(0);
    tank.ra -= 0.1;
    if (tank.ra < 2 * Math.PI) {
    System.out.println(tank.ra);
    tank.ra = 360;
    }
    Going from radians to degrees spontaneously is never correct. Also, I think it makes more sense if you'd set the angle to zero if it happens to be less than zero (to be more symmetrical when you rotate the other way around.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    nav123 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Java Code:
    g.setColor(Color.red);
    g.drawLine((int) (tank.getX() + 15), (int) (tank.getY() + 14), (MouseInfo.getPointerInfo().getLocation().x), MouseInfo.getPointerInfo().getLocation().y);
    This is for the laser on my tank. The line still follows the mouse even if it's out of the JFrame. Is there another way of getting the x and y location of the mouse or am i doing anything wrong?

Similar Threads

  1. Chess Game - Movement
    By danborgir in forum Advanced Java
    Replies: 1
    Last Post: 03-14-2011, 11:45 AM
  2. Key/mouse movement
    By falkon114 in forum New To Java
    Replies: 3
    Last Post: 02-21-2011, 02:12 AM
  3. artificial intelligence for a tank
    By newbie79 in forum New To Java
    Replies: 9
    Last Post: 12-22-2010, 04:27 PM
  4. Sprite Movement
    By Curtiz in forum Java Gaming
    Replies: 1
    Last Post: 04-26-2010, 01:31 PM
  5. Movement of balls
    By BlitzA in forum New To Java
    Replies: 8
    Last Post: 01-09-2008, 03:30 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •