Forums Register Login

Bullet movement along an angle

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
1
+Pie Number of slices to send: Send
 

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.)
+Pie Number of slices to send: Send
Mind you, that angleToTarget method wouldn't compile, so I assume the real code is slightly different.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Is there a better way to do this?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
changed this for above to this



Now everything works like a charm
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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
Why is the word "abbreviation" so long? And this ad is so short?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 4297 times.
Similar Threads
how do i draw a dotted straight line on an applet.
MouseEvent (mousePressed and mouseReleased) only works from left to right?
drawing arrows
java centre point (x,y) of line
Calculating the distance between two points problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 20:16:52.