Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (592) Games in Android Showcase (168) games submitted by our members Games in WIP (645) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Moving a Bullet  (Read 421 times) 0 Members and 1 Guest are viewing this topic.
InfiniteCoder

Senior Newbie

 « Posted 2013-07-07 01:35:09 »

Well, I recently started making a little top-down shooting game and I need a bit of help with the bullets!
I basically want them to fire in the direction of the mouse. Any help? Sorry for horribleness, I didn't know how to ask question
Longarmx
 « Reply #1 - Posted 2013-07-07 01:41:34 »

So you want to move the bullet from the player to the mouse?

You basically want to use some trig to figure this out:
 1  2  3  4  5  6  7  8  9  10  11 `// These get the delta positions between the mouse and the playerint distX = Mouse.getX() - player.getX();int distY = Mouse.getY() - player.getY();// This determines the angle of the bullet using an inverse tangent function.double angle = Math.atan2(distX, distY); // I should also note that this returns the angle in radians.// These set the bullet's speed in the x and y axis, along with its rotation.bullet.setDx(Math.sin(angle));bullet.setDy(Math.cos(angle));bullet.rotate(angle);`

I hope this is what you were asking for. If you need any other help, just ask.

EDIT: Also, try this.
Also this.

InfiniteCoder

Senior Newbie

 « Reply #2 - Posted 2013-07-07 02:46:31 »

Ha ha thanks, But I figured it out by myself
If anyone wants to know how, I basically did this:
(r is the player which is a circle)
 1  2  3  4 `double angle = Math.atan2((double)r.getCenterY() - (double)container.getInput().getMouseY(), (double)r.getCenterX() - (double)container.getInput().getMouseX()) - Math.PI;bullets.add(new Bullet(new Circle(r.getCenterX(), r.getCenterY(), 5), angle));r.setX((float)(r.getX()+Math.cos(b.getAngle())*b.getSpeed()));r.setY((float)(r.getY()+Math.sin(b.getAngle())*b.getSpeed()));`
HeroesGraveDev

JGO Kernel

Medals: 359
Projects: 11
Exp: 3 years

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

 « Reply #3 - Posted 2013-07-07 03:03:15 »

I hate to quote myself*, but:
Vector solution:

 1  2  3  4  5  6 `//Vector2f is any vector implementation with 2 floats.//position, velocity and target are both Vector2f.//Position and target are already definedvelocity = target.subtract(position);velocity.normalize();`

Non-Vector solution (still uses vector math):

 1  2  3  4  5  6  7  8  9  10  11 `//An x in the variable name is the xcoord. y is ycoord.//all variables are floats/doubles//p is position, d is velocity, t is target. Position and target are already defined.dx = tx-px;dy = ty-py;float magnitude = Math.sqrt(dx*dx+dy*dy);dx /= magnitude;dy /= magnitude;`

If you used the Search bar at the top left of the page, you perhaps would've found this yourself.

(http://www.java-gaming.org/topics/rotating-bullet-bullet-shot-need-help/29482/msg/270690/view.html#msg270690)

*Disclaimer: I don't hate to quote myself.

Pages: [1]
 ignore  |  Print

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

 deepthought (2 views) 2015-06-30 15:36:52 Za\'Anzabar (17 views) 2015-06-29 05:44:54 TritonDreyja (34 views) 2015-06-24 17:10:40 CopyableCougar4 (29 views) 2015-06-23 00:34:45 BurntPizza (32 views) 2015-06-21 20:36:46 cookiecompiler (77 views) 2015-06-11 15:42:53 cookiecompiler (39 views) 2015-06-11 15:41:14 NegativeZero (66 views) 2015-06-11 09:49:18 Fairy Tailz (89 views) 2015-06-11 01:59:47 chrislo27 (61 views) 2015-06-06 18:12:44
 princec 37x theagentd 19x BurntPizza 19x Riven 18x wessles 18x opiop65 18x CopyableCougar4 17x nsigma 16x EgonOlsen 15x KaiHH 13x SauronWatchesYou 12x KevinWorkman 11x sunburn 11x NegativeZero 10x SHC 10x DavidBVal 10x
 How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How 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:14
 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