File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Game Development and the fly likes Bullet movement  along an angle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Bullet movement  along an angle" Watch "Bullet movement  along an angle" New topic
Author

Bullet movement along an angle

Antuan Sanders
Greenhorn

Joined: Aug 11, 2012
Posts: 10
Good afternoon. Once again i'm in need of your help... After reading 1000 pages on learning java I went back to one of my old, horribly gimped, top down shooters... I decided to, instead of making the bullets just go strait, I would have the bullets fly toward the players position. Naturally, at the time the bullet is fired. In other words, not a complete lock on. Clever me, I added this method to the Enemy class.



then Id'ed this to update the movement of the bullet in the bullet class





but when the bullet comes out, the calculations are obviously off as the bullet doesn't always go toward the player. If it helps here is the bullet constructors.


I checked all the numbers up as far as player x/y and enemy x/y and they all check out... not sure but i think maybe the algorithm is wrong for this.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4391
    
    8

At first glance (and I really haven't worked this out properly) it looks like you have sin and cos round the wrong way. You're calculating the angle to the horizontal, right?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Antuan Sanders wrote:but when the bullet comes out, the calculations are obviously off as the bullet doesn't always go toward the player.


If that means that sometimes the bullet goes exactly in the opposite direction to what it should have gone, i.e. 180° off, then that looks like a sign error somewhere. Or maybe you mixed up y1 and y2, got them in the wrong order, or something like that.

(By the way there are methods in the Math class which can convert between degrees and radians so that you don't have to do it, you probably didn't know that back when you originally wrote the code.)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4391
    
    8

Mind you, that angleToTarget method wouldn't compile, so I assume the real code is slightly different.
Antuan Sanders
Greenhorn

Joined: Aug 11, 2012
Posts: 10
The bullet travels along the y axis toward the player. The enemies will spawn off screen then fly to a location, normally - 50 on the y axis and a random x less than 200. This bullet will spawn at the enemies location and move toward the player. Does this information help?

the bullet seems to come out in another direction, not opposite but then, sometime spot on. When i print out the angle its correct. Ill look a bit deeper here and see if I can come up with some more info.
Antuan Sanders
Greenhorn

Joined: Aug 11, 2012
Posts: 10
Is there a better way to do this?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4391
    
    8

Depending on how you're doing the calculation you might be getting hit by integer arithmetic (I can't tell from the code you've posted). Are you rounding to pixel values every step? If so there's a limited number of directions it can take. You need to make sure you're tracking the "real" position as a double and only rounding to draw the position.
Antuan Sanders
Greenhorn

Joined: Aug 11, 2012
Posts: 10
changed this for above to this



Now everything works like a charm
Antuan Sanders
Greenhorn

Joined: Aug 11, 2012
Posts: 10
Paul Clapham wrote:
Antuan Sanders wrote:but when the bullet comes out, the calculations are obviously off as the bullet doesn't always go toward the player.


If that means that sometimes the bullet goes exactly in the opposite direction to what it should have gone, i.e. 180° off, then that looks like a sign error somewhere. Or maybe you mixed up y1 and y2, got them in the wrong order, or something like that.

(By the way there are methods in the Math class which can convert between degrees and radians so that you don't have to do it, you probably didn't know that back when you originally wrote the code.)
Oh and indeed, they were backwards. Thanks for that, it became part of the solution.
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
Another option is not to use sine and cosines etc. I just use a simple x & y offset for any item. If you know the location of your target and location of your bullet origin then you can easily calculate how much you need to add/subtract to the x & y positions of the bullet. I personally find integer maths much easier to understand than sine and cosine. I'm sure it would be faster too.

Another option is to have a random offset from your target (the players ship) and this offset can be larger at lower levels. This would make the aliens more accurate as the game progresses. I suspect that having accurate aliens would make the game easier to play because all you have to do is keep moving and the aliens would always be just shooting at wherever you were.

PS this game (Alien Swarm ) uses this type of movement for everything.

Mike


Arcade : Alien Swarm
Board : Chess - Checkers - Connect 4 - Othello
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bullet movement along an angle