Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
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  
  Shooting towards the mouse  (Read 260 times)
0 Members and 1 Guest are viewing this topic.
Offline Genius Coder

Junior Devvie


Medals: 1
Exp: 4 years



« Posted 2014-01-18 13:25:23 »

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:

I love making games...
Offline opiop65

JGO Kernel


Medals: 159
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2014-01-18 14:20:40 »

You're going to need to use some trig to rotate bullets.

Online trollwarrior1
« Reply #2 - Posted 2014-01-18 14:33:42 »

Here is my Rotation class. It basically hides some of the ugly looking maths and all.

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  
public class Rotation {

   public static int angle(vec2 pivot, vec2 point) {

      float xdiff = pivot.x - point.x;
      float ydiff = pivot.y - point.y;
     
      float angle = (float) ((Math.atan2(xdiff, ydiff)) * 180 / Math.PI);
     
      return -(int)angle;
   }
   
   public static float angle(float x0, float y0, float x1, float y1) {
      return angle(new vec2(x0, y0), new vec2(x1, y1));
   }
   
   public static vec2 point(vec2 pivot, vec2 point, float rotation) {
     
      float rot = (float)(1f / 180 * rotation * Math.PI);
     
      float x = point.x - pivot.x;
      float y = point.y - pivot.y;
     
      float newx = (float)(x * Math.cos(rot) - y * Math.sin(rot));
      float newy = (float)(x * Math.sin(rot) + y * Math.cos(rot));
     
     
      newx += pivot.x;
      newy += pivot.y;
     
      return new vec2(newx, newy);
   }
   
}


angle() method will give you an angle between 2 points or something like that. Use this method to determine the rotation between player and the mouse.

point() method will rotate certain point around another point. After determining rotation between mouse and player, you will need to use this method to find by how much x and y should increase each frame.
To find the x, y speeds, you will need to do this:
1  
Rotation.point(new vec2(0,0), new vec2(0, -speed), player.rotation);

The x and y will be the deltas to add to your bullets positions.

Here is vec2 class. pretty simple...
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public class vec2 {

   public static vec2 NULL = new vec2(0, 0);
   
   
   public float x, y;
   
   public vec2(float x, float y) {
      this.x = x;
      this.y = y;
   }
   
   public int getX() {
      return (int) x;
   }
   
   public int getY() {
      return (int) y;
   }
}
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.

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

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

toopeicgaming1999 (10 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (27 views)
2014-11-25 11:26:43

Gibbo3771 (24 views)
2014-11-24 19:59:16

trollwarrior1 (37 views)
2014-11-22 12:13:56

xFryIx (75 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
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

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