Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (582) Games in Android Showcase (163) games submitted by our members Games in WIP (633) 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 414 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.

 Gibbo3771 (8 views) 2015-05-29 06:54:17 Ralphanese (20 views) 2015-05-28 16:52:52 theagentd (31 views) 2015-05-27 22:21:17 theagentd (35 views) 2015-05-27 22:21:08 CopyableCougar4 (25 views) 2015-05-27 19:24:50 MrMapcom (24 views) 2015-05-23 20:26:16 MrMapcom (32 views) 2015-05-23 20:23:34 Waterwolf (37 views) 2015-05-20 15:01:45 chrislo27 (44 views) 2015-05-20 03:42:21 BurntPizza (79 views) 2015-05-10 15:53:18
 Rayvolution 24x theagentd 20x Spasi 18x Riven 18x BurntPizza 17x Drenius 15x orangepascal 14x ra4king 13x opiop65 12x EgonOlsen 12x princec 12x DavidBVal 11x Husk 11x KevinWorkman 10x kevglass 9x scanevaro 8x
 Intersection 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: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:02
 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