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  
  Rotating bullet bullet shot[Solved]  (Read 1451 times)
0 Members and 1 Guest are viewing this topic.
Offline GlennBrann
« Posted 2013-05-04 23:12:03 »

Hey Everyone,

So i'm having this problem that i can't seem to figure out and i can't find it on here. Maybe i'm just wording it wrong. basically what i want to do is where ever i press my mouse on the game i want a bullet from the player to shoot towards where ever i press. So my game is a top down game and i just can't seem to figure it out. I know how to rotate the bullet manually but i don't know how to do it with the mouse. I appreciate any help. thank you.
Offline Axeman

Senior Devvie


Medals: 7



« Reply #1 - Posted 2013-05-04 23:39:45 »

Something like this should do the trick

bulletDirection = mousePosition - gunPosition
normalize the vector bulletDirection
multiply bulletDirection with the desired speed
Offline Agro
« Reply #2 - Posted 2013-05-04 23:44:32 »

^ is the best way you can find. You don't want to get into the angle stuff, but if you want to rotate the bullet sprite so it shoots in the correct direction, you'll have to.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GlennBrann
« Reply #3 - Posted 2013-05-04 23:45:32 »

Yeah thats what i want to do.
Offline alaslipknot
« Reply #4 - Posted 2013-05-05 00:06:21 »

this article i wrote may help you more on understanding moving/rotation
but in general i think you should add this method into your player class

this method will create a bullet in the current player coordinates (x,y and angle)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
public void fire(int load, int number, int spread,int life) {

      if (tmpLoad == 0) {
         for (int i = 0; i < number; i++) {
           
            Board.bullet.setX(x + w);
            Board.bullet.setY(y + h / 2);
            Board.bullet.setA(a + ((spread * (i - 1)) / 2));
            Board.bullet.setW(5);
            Board.bullet.setH(5);

            bullets.add(new Bullet(Board.bullet.getX(),
                  Board.bullet.getY(), Board.bullet.getA(), Board.bullet
                        .getW(), Board.bullet.getH()));
         }
         
         tmpLoad = load;
      }else{
         tmpLoad -= 1;
         life--;
      }

   }

and based on this cooridnates, you make your bullet move forward

1  
2  
3  
4  
public void moveForward(int speed) {
      x += Math.cos(a)*speed;
      y += Math.sin(a)*speed;
   }


now all you have to do is painting  your bullet array in your paint method and updating in your game loop

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline HeroesGraveDev

JGO Kernel


Medals: 309
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #5 - Posted 2013-05-05 00:18:20 »

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;

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 139
Projects: 4
Exp: 3 years



« Reply #6 - Posted 2013-05-05 00:39:00 »

this article i wrote may help you more on understanding moving/rotation
but in general i think you should add this method into your player class

this method will create a bullet in the current player coordinates (x,y and angle)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
public void fire(int load, int number, int spread,int life) {

      if (tmpLoad == 0) {
         for (int i = 0; i < number; i++) {
           
            Board.bullet.setX(x + w);
            Board.bullet.setY(y + h / 2);
            Board.bullet.setA(a + ((spread * (i - 1)) / 2));
            Board.bullet.setW(5);
            Board.bullet.setH(5);

            bullets.add(new Bullet(Board.bullet.getX(),
                  Board.bullet.getY(), Board.bullet.getA(), Board.bullet
                        .getW(), Board.bullet.getH()));
         }
         
         tmpLoad = load;
      }else{
         tmpLoad -= 1;
         life--;
      }

   }

and based on this cooridnates, you make your bullet move forward
I don't understand your code...you're creating new bullets by using static values of the bullet class? We don't know what the board class/bullet class does either (if you're going to copy+paste code from your project here, try to generalize it or put it into pseudocode).
Offline alaslipknot
« Reply #7 - Posted 2013-05-05 01:04:48 »

@Jimmt
okay  Roll Eyes

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 139
Projects: 4
Exp: 3 years



« Reply #8 - Posted 2013-05-05 01:10:33 »

My point was not to use magic numbers or non-standard classes in example code unless you explain what they are and what they do. It's far more helpful to give pseudocode/a forumula or one-file example code than to have them read an article that doesn't teach them anything new. 
Offline alaslipknot
« Reply #9 - Posted 2013-05-05 01:21:22 »

i got that, it will not happened again  Grin
and i was going to write the article tomorrow morning (it's 2:20AM here :3 ) but because what you said was right (my codes are un-readable Tongue here) i decided to make the article now so the one who post this topic will find his answer, he want to know how to shoot bullets and that's the main goal of my article (i already made one on movement) i know those are very simple and basic but people still asking about how they do it because they didn't found an article or a tutorial about it on our forum, i asked the same thing 2 weeks ago so why not creating an article about all the basics i know, it can be useful for someone else, and as you know...
sharing is caring  Grin

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GlennBrann
« Reply #10 - Posted 2013-05-05 14:51:51 »

Ok guys i figured it out. Thanks for the help though. if you are wondering how i did it. this is how :


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
//Constructor of my bullet class. with the angle, x position and y position
public Bullet(double angle,int x, int y){
      this.x = x;
      this.y = y;
     
             // this is what makes the bullet move towards the mouse cursor. and getX() and getY() are the positions of the bullet.
      rad = Math.atan2(getY() - Game.mouseY, getX() - Game.mouseX) - Math.PI;
             // this is how i change the angles
      dx = Math.cos(rad) * speed;
      dy = Math.sin(rad) * speed;
}


When i shoot the bullet i the angle is the "rad" variable. I hope this helps.
-GlennBrann
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 (29 views)
2014-12-15 09:26:44

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

BurntPizza (40 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 (50 views)
2014-12-03 16:27:13

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

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

toopeicgaming1999 (102 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!