Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (623) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 [Java 2D FINAL Projectile Angle?] *solved*  (Read 889 times) 0 Members and 1 Guest are viewing this topic.
GabrielBailey74
 « Posted 2013-01-17 01:09:48 »

Hello JGO, for the past few days I've been working on a new project.

Happy to say that the fancy side of the project is done, added a animated background, loading screen, audio, lighting etc.
Now I'm at the point where I've implemented a 'Game Screen', at the moment it's just a moveable player, a 'Gun' that rotates towards the mouse:

(Can't see it in this picture, but there is a 'Sights Cursor' for aiming present)

At the moment, when the mouse is pressed I am adding a 'Projectile' to a LinkedList<Projectile> like so:
 1  2  3  4  5  6  7  8 final int centerX = absX + width / 2;final int centerY = absY + height /2;final Projectile t = new Projectile(centerX, centerY);final double angle = Math.atan2(centerY - mX, centerX - mY); // mX standing for MouseXt.setAngle(angle);t.setVelX(8.0);t.setVelY(8.0);projectiles.add(t);

Final code Snippet is from the Projectile class, this method is polled in the Game Loop:
 1  2  3  4  5  6  7  8 /* Moves the Projectile correctly */public void tick() {   theta = Math.toRadians(angle);   nextX = x + (velX * PROJETILE_SPEED) * -Math.cos(theta + Math.PI / 2);   nextY = y + (velY * PROJETILE_SPEED) * -Math.sin(theta + Math.PI / 2);   x = nextX;   y = nextY;}

The Issue that I'm having is:
After I set the Projectiles angle (Calculated off of the mouse's coordinates), if I proceed to move the mouse while the projectile's in motion, it Override's the first angle I set for that projectile?

Notes:
I've tried in the 'tick()' method only initializing the 'theta' once, that didn't work, it still resets the final angle to where the mouse currently is.
Hope somebody can help me, I'm not too familiar with radians etc, the issue seems to be 'constantly updating the angle'.

HeroesGraveDev

JGO Kernel

Medals: 354
Projects: 11
Exp: 3 years

┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻

 « Reply #1 - Posted 2013-01-17 01:28:24 »

Make it a feature!!

EDIT: If you want to ignore ^that^ then try this:

 1  2 nextX = x + (velX * PROJETILE_SPEED) * -Math.cos(theta); // REMOVED "+ Math.PI / 2"    nextY = y + (velY * PROJETILE_SPEED) * -Math.sin(theta); // REMOVED "+ Math.PI / 2"

But you should seriously consider making this "glitch" a main feature of the gameplay. It could be quite interesting.

GabrielBailey74
 « Reply #2 - Posted 2013-01-17 01:43:54 »

Make it a feature!!

EDIT: If you want to ignore ^that^ then try this:

 1  2 nextX = x + (velX * PROJETILE_SPEED) * -Math.cos(theta); // REMOVED "+ Math.PI / 2"    nextY = y + (velY * PROJETILE_SPEED) * -Math.sin(theta); // REMOVED "+ Math.PI / 2"

But you should seriously consider making this "glitch" a main feature of the gameplay. It could be quite interesting.

Thanks for the reply , I 'almost' / 'temporarily' fixed it by increasing the velocity of it lolz, gives it no chance to modify it's angle before it's hit it's target.

EDIT
 1 + Math.PI / 2
is because that's what fixed it's angle.
If i remove that, the angle isn't relative to the mouses coordinates at all.
The Projectile just slams in some awkward angle o.O

HeroesGraveDev

JGO Kernel

Medals: 354
Projects: 11
Exp: 3 years

┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻

 « Reply #3 - Posted 2013-01-17 02:01:26 »

EDIT: Switch cos() and sin() around and then remove + Math.PI / 2.

GabrielBailey74
 « Reply #4 - Posted 2013-01-17 03:49:05 »

Thanks mate, everything's perfect naow with mah angles

I thought it would be like:
 1  2  3 void tick() {     if (x >= GameWindow.WIDTH) { visible = false;}

But I know I have to do more than that lol, it's cutting the projectiles off at weird distances o.O

Pages: [1]
 ignore  |  Print

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

 BurntPizza (28 views) 2015-04-23 03:42:11 theagentd (32 views) 2015-04-22 16:23:07 Riven (46 views) 2015-04-16 10:48:47 Duke0200 (57 views) 2015-04-16 01:59:01 Fairy Tailz (40 views) 2015-04-14 20:13:12 Riven (42 views) 2015-04-12 21:36:37 bus hotdog (58 views) 2015-04-10 02:39:32 CopyableCougar4 (64 views) 2015-04-10 00:51:04 BurntPizza (67 views) 2015-04-06 22:06:58 ags1 (65 views) 2015-04-02 10:58:48
 theagentd 23x BurntPizza 16x wessles 15x alwex 11x 65K 11x kingroka123 11x kevglass 8x Rayvolution 8x Roquen 7x chrislo27 7x Hanksha 7x Riven 7x Olo 7x ra4king 7x Ecumene 7x KevinWorkman 6x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources 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:27
 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