Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (798)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (865)
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  
  [solved] Java2D Projectile System (Movement logic)  (Read 2341 times)
0 Members and 1 Guest are viewing this topic.
Offline TehJavaDev

JGO Knight


Medals: 17
Projects: 3
Exp: 7-9 months


Java is so dope they wanna >:D


« Posted 2014-09-06 00:45:05 »

Hey JGO, I'm trying to figure out how to go about firing a projectile from my players position to the coordinates that were tagged when the fire button is pressed.
(Game is static tiles for now, no movement)

I'm not sure about how I should do this so I'm coming here asking for help as I don't have that much experience with angles/vectors.

I think I'll be able to figure out the collision, I just need help with the actual logic for moving at an angle ^__^

When fire button is clicked:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public static void update() {

   // firing button is down
   if (leftClickDown){
      // TODO: Implement delay system.
      final int velocity = 5;
      bullets.add(new Bullet(playerX, playerY, mouseX, mouseY, velocity));
   }

   // update every bullet. (Move)
   for (Bullet b : bullets) {
      b.update();
   }
}


Bullet class:
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  
package org.game.core.player;

public class Bullet {

   // actual position of bullet.
   private int x, y;

   // the destination of the bullet.
   private int destinationX, destinationY;

   // handles x/y velocity
   private int velocity;

   public Bullet(int x, int y, int destX, int destY, int velocity) {
      this.velocity = velocity;
      this.x = x;
      this.y = y;
      destinationX = destX;
      destinationY = destY;
   }
   
   public void update() {
      // HERE IS WHAT I NEED TO IMPLEMENT.
   }

}

Thanks for any help/feedback ^__^

Checkout my new easy to use java networking library: GNetLib
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #1 - Posted 2014-09-06 01:03:24 »

Searched "bullet direction"

Some code, some math/programming lessons, good reads either way.

http://www.java-gaming.org/topics/moving-from-point-a-to-b-shooting-and-movement/33780/view.html
http://www.java-gaming.org/topics/my-bullet-missile-magic-doesn-t-want-to-go-to-mouse-x-y-solved/33905/view.html
http://www.java-gaming.org/topics/bullet-shooting-in-mouse-direction/34029/view.html
Offline TehJavaDev

JGO Knight


Medals: 17
Projects: 3
Exp: 7-9 months


Java is so dope they wanna >:D


« Reply #2 - Posted 2014-09-06 01:17:53 »


Thanks alot!
The first link helped me with everything I needed :3

First there was a bug where if the player moved the bullets would move but I made that integer final so that bugs gone.

I'm getting concurrent modification exceptions now lols, i'll try and figure it out.
Only adding bullets so far not removing so dunno why.

Thanks <3

Checkout my new easy to use java networking library: GNetLib
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CopyableCougar4
« Reply #3 - Posted 2014-09-06 01:20:18 »

Don't use enhanced for loops and that error should go away.

CopyableCougar4

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Offline TehJavaDev

JGO Knight


Medals: 17
Projects: 3
Exp: 7-9 months


Java is so dope they wanna >:D


« Reply #4 - Posted 2014-09-06 01:24:31 »

Don't use enhanced for loops and that error should go away.

CopyableCougar4

That fixed it, thanks ^__^

Checkout my new easy to use java networking library: GNetLib
Offline CopyableCougar4
« Reply #5 - Posted 2014-09-06 01:25:17 »

No problem Smiley

I posted a topic about concurrent modification exception a month or so ago.

CopyableCougar4

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Offline LostWarrior

Senior Devvie


Medals: 8
Exp: 5 years



« Reply #6 - Posted 2014-09-06 09:30:26 »

Although it's already solved, I'd like to share my 2 cents.

I'd use two vectors, one for the mouse position (in world space if you have a camera) and one for the starting position. Then you subtract the starting position from the mouse position vector, normalize it, and then maybe multiply it with the amount of speed per tick. Then you can add the vector to the current position each tick, and there you go, the bullet will move in the right direction.

Reading about vectors and some other math may help you with this kind of stuff.

Cows aren't sheep, because pigs already are.
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (28 views)
2019-09-04 15:33:17

hadezbladez (3957 views)
2018-11-16 13:46:03

hadezbladez (1434 views)
2018-11-16 13:41:33

hadezbladez (3954 views)
2018-11-16 13:35:35

hadezbladez (765 views)
2018-11-16 13:32:03

EgonOlsen (4079 views)
2018-06-10 19:43:48

EgonOlsen (4649 views)
2018-06-10 19:43:44

EgonOlsen (2748 views)
2018-06-10 19:43:20

DesertCoockie (3643 views)
2018-05-13 18:23:11

nelsongames (3834 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
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!