Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bullet movement along an angle

 
Antuan Sanders
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mind you, that angleToTarget method wouldn't compile, so I assume the real code is slightly different.
 
Antuan Sanders
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a better way to do this?
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
changed this for above to this



Now everything works like a charm
 
Antuan Sanders
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 260
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic