Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) 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 930 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: 382
Projects: 11
Exp: 4 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

 ivj94 (584 views) 2018-03-24 14:47:39 ivj94 (48 views) 2018-03-24 14:46:31 ivj94 (382 views) 2018-03-24 14:43:53 Solater (62 views) 2018-03-17 05:04:08 nelsongames (109 views) 2018-03-05 17:56:34 Gornova (159 views) 2018-03-02 22:15:33 buddyBro (703 views) 2018-02-28 16:59:18 buddyBro (92 views) 2018-02-28 16:45:17 xxMrPHDxx (493 views) 2017-12-31 17:17:51 xxMrPHDxx (733 views) 2017-12-31 17:15:51
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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