[SOLOVED] How to get the bullets to go towards mouse?

Im creating a side scroller that i need help with. I want the bullets to shoot towards the mouse. However, the current code I have only allows for shooting diagonally:

[quote]Bullet.Java:
[/quote]


public class Bullet extends Entity{

	public Bullet(int x, int y, int type) {
		super(x, y);
		this.type = type;
		Image();
	}
	
	public void update() {
		y += velY;
		x += velX;
		
		if (type == 0) { currentImage = lead_Bullet;}
		if (type == 1) { currentImage = laser_Bullet;}
	}
	
	public void draw(Graphics2D g2d) {
		g2d.drawImage(currentImage, x, y, null);
		if (Game.debugMode) { g2d.draw(getBounds());  }
	}

	public int velX;
	public int velY;
	
	public int type=0;
	public Image currentImage;
	
	// Characters
	private Image lead_Bullet;
	private Image laser_Bullet;
	
	
	public void Image() {
		ImageIcon q1 = new ImageIcon(this.getClass().getResource("/misc/lead bullet.png"));
		lead_Bullet = q1.getImage();
		ImageIcon q2 = new ImageIcon(this.getClass().getResource("/mobs/player old 1/player_right_old1.png"));
		laser_Bullet = q2.getImage();
	}
	 public Rectangle getBounds() {
		return new Rectangle (x,y,9,9);
	 }
}

[quote]Shooting Code:
[/quote]

public static void shoot() {shot = true;}
	public void BulletUpdate() {
		if (shot==true) {
			shotTimer++;
			Bullet tempBullet = new Bullet(Game.player.x, Game.player.y, 0);
			Bullets.add(tempBullet);
			if (Object_Control.msx > x) {
				tempBullet.velX=Environment.speed;
			}
			if (Object_Control.msx < x) {
				tempBullet.velX=-Environment.speed;
			}
			
			if (Object_Control.msy > y) {
				tempBullet.velY=Environment.speed;
			}
			if (Object_Control.msy < y) {
				tempBullet.velY=-Environment.speed;
			}
			System.out.print(shot);
		}
		if (shotTimer > 1) {
			shot = false;
			shotTimer = 0;
		}
	}

Didn’t you just post this same exact question?

ok… so your starting your bullet where your player is…

what i dont see in your code is any logic to determine where the mouse is.

I believe the code your looking for is in the input section of the LWJGL wiki web site. (assuming thats what your using)

[udpate]

actually let me take that back … your draw method makes me think you may be using a version of JOGL … in which case you need to use standard java mouse listener to get the x /y of the mouse.

j.

Thats not OpenGL. Its… I Swing. I always get Swing and AWT messed up though.

To make something travel from one point (player) t another (mouse);


public Vector2 moveTo(Point start, Point dest, float speed)
{
float angle= atan2(start.y-dest.y, start.x-dest.x);
Vector2 velocity = new Vector2(Math.cos(angle)*speed,Math.sin(angle)*speed);
return velocity; 
}

You can also do this with pure vectors and no trig but most people are ok with trig.

doh your totally right!

awt / swing

Here is a link to a previous post I made helping someone else with the same problem:

thankyou!