Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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 Breaking a bullet's velocity into components  (Read 349 times)
0 Members and 1 Guest are viewing this topic.
Offline CopyableCougar4

Senior Member


Medals: 7
Exp: 1 year



« Posted 2014-06-21 23:24:05 »

Hi

So I was starting to implement shooting into my game (for now just calculating movement, and not factoring gravity at all), and I was rendering the bullets by drawing a quad at the given location. Each
1  
update()
I would add the two stored components to the position of the bullet. Anyway, when I was trying out different angles, the bullet would only go at the nearest multiple of 45 degrees, which confused me because the input angles aren't in multiples of that.

Bullet vectory component calculation:
1  
2  
velocityX = (float)(Math.cos(angle) * velocity);
velocityY = (float)(Math.sin(angle) * velocity);


Any thoughts or ideas would be helpful Smiley

CopyableCougar4

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

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

Senior Member


Medals: 15



« Reply #1 - Posted 2014-06-21 23:33:48 »

Are you sure that angle is in radians?
Offline CopyableCougar4

Senior Member


Medals: 7
Exp: 1 year



« Reply #2 - Posted 2014-06-21 23:36:55 »

Yup! I use
1  
Math.atan2(y, x);
to get the angle Smiley

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

Github: http://github.com/CopyableCougar4
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza
« Reply #3 - Posted 2014-06-21 23:40:18 »

Is angle a double or float? What are x and y?
Offline CopyableCougar4

Senior Member


Medals: 7
Exp: 1 year



« Reply #4 - Posted 2014-06-21 23:44:49 »

x and y were just the placeholders I used to show what formula I used. In reality x is the difference between the x of the mouse and the x of the player, and y is the difference between the y of the mouse and the y of the player. All math functions return a double but the number is passed as a float.

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

Github: http://github.com/CopyableCougar4
Offline BurntPizza
« Reply #5 - Posted 2014-06-21 23:48:35 »

I meant what are the types. Just post the section of code containing this, can't really do anything but presume right now.
Offline CopyableCougar4

Senior Member


Medals: 7
Exp: 1 year



« Reply #6 - Posted 2014-06-21 23:52:32 »

This code constructs the bullet. The mouse rectangle just stores the mouse location and size.
1  
2  
3  
4  
5  
6  
7  
8  
Rectangle mouse = DisplayManager.getInstance().getRenderer().getMouse();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
float x = screenSize.width / 2.0f;
float y = screenSize.height / 2.0f;
int dx = (mouse.x - Math.round(x));
int dy = (mouse.y - Math.round(y));
float angle = (float)(Math.atan2(dy, dx));
bullets.add(new Bullet(x, y, width, height, angle, velocity, color));


This code is the constructor for bullet objects
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public Bullet(float x, float y, float width, float height, float angle, float velocity, Color color){
     
   // Construct the bounds
  bounds = new Rectangle(Math.round(x), Math.round(y), Math.round(width), Math.round(height));
   // Calculate the velocities
  velocityX = (float)(Math.cos(angle) * velocity);
   velocityY = (float)(Math.sin(angle) * velocity);
   // Set some variables
  this.color = color;
   done = false;
   life = 3000;
   this.angle = angle;
     
}


This code makes the bullet travel
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public void travel(){
     
   if(isAlive()){
      life--;
      bounds.x += Math.round(velocityX);
      bounds.y += Math.round(velocityY);
   }
   if(life == 0){
      dispose();
   }
     
}


bounds is a rectangle that stores the collision region for the bullet, for the future.

Let me know if you need more code Smiley

CopyableCougar4

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

Github: http://github.com/CopyableCougar4
Offline BurntPizza
« Reply #7 - Posted 2014-06-21 23:59:49 »

Quite possible it's that bounds.x, bounds.y are integers, at least try removing the Math.round()s from
bounds.x += Math.round(velocityX);
bounds.y += Math.round(velocityY);
or changing them to floor() or ceil().

Also might be that
velocity
is computed wrong, and rounds to 0 on non-45 degree divisible angles.

Offline CopyableCougar4

Senior Member


Medals: 7
Exp: 1 year



« Reply #8 - Posted 2014-06-22 00:07:02 »

Thanks Smiley I found my mistake. With the rounding it only allowed for the 45 degree multiples because the change was rounded.

CopyableCougar4

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

Github: http://github.com/CopyableCougar4
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (18 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (70 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32

BurntPizza (37 views)
2014-08-08 02:01:56
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

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

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!