Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (595)
Games in Android Showcase (168)
games submitted by our members
Games in WIP (646)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Asteroids: Shoot at a target question  (Read 1413 times)
0 Members and 1 Guest are viewing this topic.
Offline Alex.Jeffery

Senior Newbie

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


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.
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 PositionPlayer PositionResult
(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
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?


Offline 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.)
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.
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);

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 zero
double xVelBullet = speed*dx/d;
double yVelBullet = speed*dy/d;

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


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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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  Smiley

Thanks for the help!

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

Offline 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  Smiley

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.

deepthought (47 views)
2015-06-30 15:39:44

deepthought (50 views)
2015-06-30 15:39:09

deepthought (62 views)
2015-06-30 15:36:52

Za\'Anzabar (34 views)
2015-06-29 05:44:54

TritonDreyja (46 views)
2015-06-24 17:10:40

CopyableCougar4 (46 views)
2015-06-23 00:34:45

BurntPizza (51 views)
2015-06-21 20:36:46

cookiecompiler (93 views)
2015-06-11 15:42:53

cookiecompiler (55 views)
2015-06-11 15:41:14

NegativeZero (79 views)
2015-06-11 09:49:18
How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14 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‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!