Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (538) Games in Android Showcase (132) games submitted by our members Games in WIP (601) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Asteroids: Shoot at a target question  (Read 1311 times) 0 Members and 1 Guest are viewing this topic.
Alex.Jeffery

Senior Newbie

 « Posted 2008-06-17 03:31:33 »

Hi,

I have been making a clone of Asteroids in Java. You can see my progress here Play Asteroids

I am having some trouble with getting the flying saucers to shoot at the player.
I have done some research and found that by taking the players position away from the flying saucers position and using the Math.atan2 function on the result I should get the rotation in radians needed to point towards the player from the flying saucers position.

Here is that in code.
 1  2  3  4  5  6  7  8  9  10 `double x1 = saucer.x.get();double y1 = -saucer.y.get();               double x2 = player.x.get();double y2 = -player.y.get();               double x = x2 - x1;double y = y2 - y1;               double angle = Math.atan2(y, x);`

I tried this and it did not work. So I did some test calculations to see why.
 Saucer Position Player Position Result (100, 100) (100, 90) 3.14 (100, 100) (110, 100) 1.57 (100, 100) (100, 110) 0 (100, 100) (90, 100) -1.57

This would rotate an object to point in the direction I wanted except the Y axis was upside down.
So if I multiplied the Y by -1 it comes out ok.

I then calculate the velocity for the bullet the saucer fires at the player as such
 1  2 `xVelocity += Math.sin(angle) * ACCELERATION;yVelocity -= Math.cos(angle) * ACCELERATION;`

I tried this in my game and the saucers do not shoot at the player.
Any tips on what to try next?

Thanks!

dishmoth
 « Reply #1 - Posted 2008-06-17 09:17:28 »

My first guess would be that you need to switch around the cos and sin in the velocity calculation, like so.  (That's usually the way things work with atan2.)
 1  2 `xVelocity += Math.cos(angle) * ACCELERATION;yVelocity -= Math.sin(angle) * ACCELERATION;`

However, that doesn't seem to agree with the "Result" angles that you've given in your example, so I'm not confident that it will solve the problem.

Also, you should be able to remove the minus signs from y1, y2 and yVelocity since they should all cancel out.

If you put a debugging line in the code to print out (x1,y1), (x2,y2), (x,y), angle, and (xVelocity,yVelocity), it should make it clearer what's going on.

For what it's worth, I believe that the following two bits of code should both make a bullet fly towards a target.
 1  2  3  4  5 `double dx = xTarget - xBullet;double dy = yTarget - yBullet;double angle = Math.atan2(dy,dx);double xVelBullet = speed*Math.cos(angle);double yVelBullet = speed*Math.sin(angle);`

 1  2  3  4  5  6 `double dx = xTarget - xBullet;double dy = yTarget - yBullet;double d = Math.sqrt(dx*dx + dy*dy);d = Math.max(d, 1.0e-6); // avoid dividing by zerodouble xVelBullet = speed*dx/d;double yVelBullet = speed*dy/d;`

gouessej
 « Reply #2 - Posted 2008-06-17 09:24:13 »

Hi!

Please don't forget to add your game on the Java Game Tome. I tested it. Please clarify what you mean by providing a schema with the data you know and what you want to compute from these data, then I will try to help you.

Alex.Jeffery

Senior Newbie

 « Reply #3 - Posted 2008-06-18 02:37:14 »

I tried the bullet maths you suggested and it works.
The little flying saucers are quite dangerous now

Thanks for the help!

I added my game to the Java Game Tome as well.

gouessej
 « Reply #4 - Posted 2008-06-18 10:56:02 »

I tried the bullet maths you suggested and it works.
The little flying saucers are quite dangerous now

Thanks for the help!

I added my game to the Java Game Tome as well.

Thanks. I will validate your addition as soon as possible. It is a pleasure for me to see more and more people adding their games and I will have to improve the website to drive the experience better.

Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 rwatson462 (30 views) 2014-12-15 09:26:44 Mr.CodeIt (20 views) 2014-12-14 19:50:38 BurntPizza (42 views) 2014-12-09 22:41:13 BurntPizza (76 views) 2014-12-08 04:46:31 JscottyBieshaar (37 views) 2014-12-05 12:39:02 SHC (51 views) 2014-12-03 16:27:13 CopyableCougar4 (48 views) 2014-11-29 21:32:03 toopeicgaming1999 (115 views) 2014-11-26 15:22:04 toopeicgaming1999 (103 views) 2014-11-26 15:20:36 toopeicgaming1999 (30 views) 2014-11-26 15:20:08
 LiquidNitrogen 35x basil_ 32x Rayvolution 30x HeroesGraveDev 26x BurntPizza 18x appel 17x kevglass 17x kpars 15x gouessej 14x Gibbo3771 14x princec 13x Riven 12x SHC 10x Ecumene 10x pitbuller 9x VIrtueeL 9x
 Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org