Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
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  
  How to make a bullet shoot towards the mouse?  (Read 982 times)
0 Members and 1 Guest are viewing this topic.
Offline Jacob Pickens

JGO Ninja


Medals: 32
Projects: 5
Exp: 10-12 months


Do things you don't know how.


« Posted 2014-05-02 01:45:12 »

The title explains it all.

Live development of whatever I feel like making! http://www.twitch.tv/jacob_pickens
Online BurntPizza

« JGO Bitwise Duke »


Medals: 289
Exp: 5 years



« Reply #1 - Posted 2014-05-02 01:47:56 »

Apply force in direction of mouse. Integrate.

EDIT: That's for "homing missile" behavior. Unless you mean, "how to make gun point in direction of mouse"
Offline Jacob Pickens

JGO Ninja


Medals: 32
Projects: 5
Exp: 10-12 months


Do things you don't know how.


« Reply #2 - Posted 2014-05-02 01:48:44 »

What if I'm not using a physics engine?

Live development of whatever I feel like making! http://www.twitch.tv/jacob_pickens
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Drenius
« Reply #3 - Posted 2014-05-02 01:49:21 »

http://www.java-gaming.org/index.php/topic,26757.
Online BurntPizza

« JGO Bitwise Duke »


Medals: 289
Exp: 5 years



« Reply #4 - Posted 2014-05-02 01:49:59 »

What if I'm not using a physics engine?

Don't need one. The code to do that is ~10 lines.
Offline Jacob Pickens

JGO Ninja


Medals: 32
Projects: 5
Exp: 10-12 months


Do things you don't know how.


« Reply #5 - Posted 2014-05-02 01:50:33 »

Well then, if it isn't too big of a deal, could i have that code?

Live development of whatever I feel like making! http://www.twitch.tv/jacob_pickens
Online BurntPizza

« JGO Bitwise Duke »


Medals: 289
Exp: 5 years



« Reply #6 - Posted 2014-05-02 01:51:44 »

I still can't tell (and you haven't clarified) if you mean you want "point gun at mouse position" or "mouse-following homing missile bullets."
Offline Jacob Pickens

JGO Ninja


Medals: 32
Projects: 5
Exp: 10-12 months


Do things you don't know how.


« Reply #7 - Posted 2014-05-02 01:52:40 »

Basically, When you click the mouse, I want the bullet to move in that direction. When you move the mouse, the bullets direction will never change.

Live development of whatever I feel like making! http://www.twitch.tv/jacob_pickens
Offline Drenius
« Reply #8 - Posted 2014-05-02 02:00:32 »

1  
2  
vec.x = mouse.x-gun.x;
vec.y = mouse.y-gun.y;

Is the vector to the mouse. Normalize it and you have the direction as a vector.
Online BurntPizza

« JGO Bitwise Duke »


Medals: 289
Exp: 5 years



« Reply #9 - Posted 2014-05-02 02:00:48 »

Basically, When you click the mouse, I want the bullet to move in that direction. When you move the mouse, the bullets direction will never change.

So that would be gun aiming, more or less.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
class Bullet {
    float velocityX, velocityY, positionX, positionY;
}

/**
    Sets Bullet [b] to fly towards [x, y] from it's current location at [speed].

*/

private void directBulletToPoint(Bullet b, float x, float y, float speed) {
    float dx = x - b.positionX;
    float dy = y - b.positionY;
    float len = Math.sqrt(dx*dx+dy*dy);
    b.velocityX = dx / len * speed;
    b.velocityY = dy / len * speed;
}


Simple vector stuff:
Get displacement vector
Normalize
Multiply by speed

Programmed in post, may contain errors.
Also somebody correct me if I screwed something up.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #10 - Posted 2014-05-02 03:01:24 »

<a href="http://www.youtube.com/v/xp6ibuI8UuQ?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/xp6ibuI8UuQ?version=3&amp;hl=en_US&amp;start=</a>

Offline Gibbo3771
« Reply #11 - Posted 2014-05-02 03:59:01 »

Tutorial

This is for Cocos2D but the code is easy enough to translate.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline trollwarrior1
« Reply #12 - Posted 2014-05-02 04:40:20 »

I'm pretty sure I already answered exact same topic a lot of times already in the past.. I remember at least 2 of these topics..
Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 139
Projects: 4
Exp: 3 years



« Reply #13 - Posted 2014-05-02 05:56:22 »

Yeah this is one of the more asked questions, the corresponding inquiries being "rotate arm" and "get angle".
Offline ags1

JGO Wizard


Medals: 75
Projects: 3
Exp: 5 years


Make code not war!


« Reply #14 - Posted 2014-05-02 10:04:45 »

Shooting towards cursor position I can understand, but shooting toward mouse position seems insoluble to me... :-)

Offline GNecro1
« Reply #15 - Posted 2014-05-02 11:57:43 »

I do it like this.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
   // your game tick
   public void tick() {
      if (Clicked) {
         int targetX2 = mse.x;
         int targetY2 = mse.y;
         double xDistance = mse.x - x;
         double yDistance = mse.y - y;

         int targetX = (mse.x - width / 2);
         int targetY = (mse.y - height / 2);
      }
      int pathX = (int) (game.planet.x + game.planet.width / 2 - width / 2 - x);
      int pathY = (int) (game.planet.y + game.planet.height / 2 - height / 2 - y);

      double distance = Math.sqrt(pathX * pathX + pathY * pathY);
      double directionX = pathX / distance;
      double directionY = pathY / distance;

      double movementX = directionX * 3;
      double movementY = directionY * 3;
      x += (int) movementX;
      y += (int) movementY;

   }

   // MouseListener (something that extends MouseListener)
   public void mousePressed(MouseEvent e) {
      if (e.getButton() == 1) {
         Clicked = true;
      }
   }

   public void mouseReleased(MouseEvent e) {
      if (e.getButton() == 1) {
         Clicked = false;
      }
   }

   // MouseMotionListener (something that extends MouseMotionListener)
   public void mouseDragged(MouseEvent e) {
      mse = new Point(e.getX(), e.getY());
   }

   public void mouseMoved(MouseEvent e) {
      mse = new Point(e.getX(), e.getY());
   }


Did it help?

Java freak! Cheesy
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.

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

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

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

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

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

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

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

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

toopeicgaming1999 (103 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
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!