Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (537)
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  
  [SOLOVED] How to get the bullets to go towards mouse?  (Read 305 times)
0 Members and 1 Guest are viewing this topic.
Offline Genius Coder

Junior Member


Medals: 1
Exp: 4 years



« Posted 2014-01-20 17:00:25 »

Im creating a side scroller that i need help with. I want the bullets to shoot towards the mouse. However, the current code I have only allows for shooting diagonally:

Quote
Bullet.Java:
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  
public class Bullet extends Entity{

   public Bullet(int x, int y, int type) {
      super(x, y);
      this.type = type;
      Image();
   }
   
   public void update() {
      y += velY;
      x += velX;
     
      if (type == 0) { currentImage = lead_Bullet;}
      if (type == 1) { currentImage = laser_Bullet;}
   }
   
   public void draw(Graphics2D g2d) {
      g2d.drawImage(currentImage, x, y, null);
      if (Game.debugMode) { g2d.draw(getBounds());  }
   }

   public int velX;
   public int velY;
   
   public int type=0;
   public Image currentImage;
   
   // Characters
  private Image lead_Bullet;
   private Image laser_Bullet;
   
   
   public void Image() {
      ImageIcon q1 = new ImageIcon(this.getClass().getResource("/misc/lead bullet.png"));
      lead_Bullet = q1.getImage();
      ImageIcon q2 = new ImageIcon(this.getClass().getResource("/mobs/player old 1/player_right_old1.png"));
      laser_Bullet = q2.getImage();
   }
    public Rectangle getBounds() {
      return new Rectangle (x,y,9,9);
    }
}



Quote
Shooting Code:
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  
public static void shoot() {shot = true;}
   public void BulletUpdate() {
      if (shot==true) {
         shotTimer++;
         Bullet tempBullet = new Bullet(Game.player.x, Game.player.y, 0);
         Bullets.add(tempBullet);
         if (Object_Control.msx > x) {
            tempBullet.velX=Environment.speed;
         }
         if (Object_Control.msx < x) {
            tempBullet.velX=-Environment.speed;
         }
         
         if (Object_Control.msy > y) {
            tempBullet.velY=Environment.speed;
         }
         if (Object_Control.msy < y) {
            tempBullet.velY=-Environment.speed;
         }
         System.out.print(shot);
      }
      if (shotTimer > 1) {
         shot = false;
         shotTimer = 0;
      }
   }

I love making games...
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2014-01-20 17:03:40 »

Didn't you just post this same exact question?

Offline jmguillemette
« Reply #2 - Posted 2014-01-20 18:25:22 »

ok.. so your starting your bullet where your player is..

what i dont see in your code is any logic to determine where the mouse is.

I believe the code your looking for is in the input section of the LWJGL wiki web site. (assuming thats what your using)

[udpate]

actually let me take that back .. your draw method makes me think you may be using a version of JOGL .. in which case you need to use standard java mouse listener to get the x /y of the mouse.



j.

-=Like a post.. give the author a medal!=-
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #3 - Posted 2014-01-20 18:31:22 »

Thats not OpenGL. Its... I Swing. I always get Swing and AWT messed up though.

Offline StumpyStrust
« Reply #4 - Posted 2014-01-20 18:42:47 »

To make something travel from one point (player) t another (mouse);

1  
2  
3  
4  
5  
6  
public Vector2 moveTo(Point start, Point dest, float speed)
{
float angle= atan2(start.y-dest.y, start.x-dest.x);
Vector2 velocity = new Vector2(Math.cos(angle)*speed,Math.sin(angle)*speed);
return velocity;
}


You can also do this with pure vectors and no trig but most people are ok with trig.

Offline jmguillemette
« Reply #5 - Posted 2014-01-20 18:53:35 »

doh your totally right!

awt / swing

-=Like a post.. give the author a medal!=-
Offline DavidW

Junior Member


Medals: 3
Exp: 7 years



« Reply #6 - Posted 2014-01-20 19:18:43 »

Here is a link to a previous post I made helping someone else with the same problem:

http://www.java-gaming.org/topics/angle-of-slope-for-mouse-movement/26409/msg/231592/view.html#msg231592

Hello!
Offline Genius Coder

Junior Member


Medals: 1
Exp: 4 years



« Reply #7 - Posted 2014-01-21 16:36:31 »

thankyou!

I love making games...
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.

CogWheelz (17 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (18 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (42 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

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

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
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!