1

My problem might be simple. I've spent most of today thinking of an algorithm(That will pretty probably be a couple if statements) that will determine the direction the mouse is pointing and shoot a bullet in that direction. I already tested the bullets to make sure they shoot by shooting in a defined direction.

how would i go about calculating if the mouse is on the left side of the player, the right side,top side, bottom side, or if it's on the corners of the player ?

Solved: Thanks for all your help but after a day of thinking I came up with a way my self. What I did is use the if statements to determine when I press the mouse down, is it going to be colliding with the top part of the player,bottom,right,left,or corners. Anyway, here is my code. P.S. I used the variable x1 as the mousex, y1 as mousey, x as playerx, and y as player y. The only other variable I have is dx and dy but you should know what those do.

//top
if (x1 > x && x1 < x + 40 && y1 > y - 250 && y1 < y){
        dy = -1;
    dx = 0;
}
//right
if (x1 > x + 40 && x1 < x + 250 && y1 > y && y1 < y + 40){
    dx = 1;
    dy = 0;
}
//bottom
if (x1 > x && x1 < x + 40 && y1 > y+40 && y1 < y+250){
    dy = 1;
    dx = 0;
}
//left
if (x1 < x && x1 > x - 250 && y1 > y && y1 < y + 40){
    dx = -1;
    dy = 0;
}
//top right corner
if (x1 > x + 40 && x1 < x + 250 && y1 > y - 250 && y1 < y){
    dx = 1;
    dy = -1;
}
//top left corner
if (x1 < x && x1 > x - 250 && y1 > y - 250 && y1 < y){
    dx = -1;
    dy = -1;
}
//bottom right corner
if (x1 > x + 40 && x1 < x + 250 && y1 > y + 40 && y1 < y + 250){
    dx = 1;
    dy = 1;
}
//bottom left corner
if (x1 < x && x1 > x - 250 && y1 > y + 40 && y1 < y + 250){
    dx = -1;
    dy = 1;
} 
mad_manny
  • 1,081
  • 16
  • 28
36redsoxfan
  • 250
  • 2
  • 5
  • 15
  • 2
    What is your actual question? If you know where the mouse is (x and y coordinates), and you know where the gun is, some simple geometry and math should determine an angle or x and y velocity for the bullet. – DGH May 11 '12 at 21:45
  • My actual question is how would i go about calculating if the mouse is on the left side of the player, the right side,top side, bottom side, or if it's on the corners of the player – 36redsoxfan May 11 '12 at 21:47
  • 2
    Simple algebra will answer that for you -- you don't even need trigonometry if all you're looking at is the general direction. What have you tried and where in your code are you stuck? – Hovercraft Full Of Eels May 11 '12 at 21:48
  • well... I've been doing trial and error so I don't really have the code since I've deleted it many times but I do know where I'm messing up. I can get the corners to work but i have know idea how to do the sides since some of the properties of the corner if statements fit into the ones for the sides of the player. – 36redsoxfan May 11 '12 at 21:56
  • 1
    *"but I do know where I'm messing up."* For better help sooner, post one [SSCCE](http://sscce.org/) of your best attempt. As it stands, I'm considering voting to close this as 'not a real question'. – Andrew Thompson May 11 '12 at 22:19

1 Answers1

1

You have to implement Mouse move actionlistener if you want to implement shooting while the mouse button is pressed.

Simple equation of line will do it.

Solution: Get the initial point (x0,y0) when button is pressed. While mouse moves when pressed, get (x1,y1) point where the mouse is moving (this constantly changes) - get the line equation - (you have 2 points so find slope and then use one point to get the equation of the line).

Now the direction the bullet fires is the perpendicular to this line through (x1,y1). So you can find the equation of this perpendicular line when the other equation is known. Now to know whether it has to be fired up or down is relative to finding out which side the gun is pointed (direction be stored in a variable)

After all this, when mouse still moves, old point will now be (x1,y1) and new point will be (x2,y2) and you keep implementing these changes.

Subs
  • 529
  • 2
  • 9
  • Well, if you want to know how to find gun aligned up (N), down (S), right (E) or left (W) - First at initial position display the gun at some constant position when game starts (say N), then when the player scrolls (or you can implement with some buttons or keys) you can change the direction to NE or E or SE or S etc or at any angle (degrees) and store the angle information in a variable – Subs May 11 '12 at 22:35