Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  Moving a Bullet  (Read 342 times)
0 Members and 1 Guest are viewing this topic.
Offline 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 Sad
Online 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 player
int 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. Smiley

EDIT: Also, try this.
Also this.

Offline InfiniteCoder

Senior Newbie





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

Ha ha thanks, But I figured it out by myself  Grin
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()));
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 320
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 defined

velocity = 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. Pointing

(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. Grin

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

rwatson462 (54 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (85 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (79 views)
2014-12-05 12:39:02

SHC (89 views)
2014-12-03 16:27:13

CopyableCougar4 (97 views)
2014-11-29 21:32:03

toopeicgaming1999 (155 views)
2014-11-26 15:22:04

toopeicgaming1999 (153 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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
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!