Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (844) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 SOLVED Breaking a bullet's velocity into components  (Read 2035 times) 0 Members and 1 Guest are viewing this topic.
CopyableCougar4
 « 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

CopyableCougar4

Either wandering the forum or programming. Most likely the latter

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

JGO Coder

Medals: 21

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

Are you sure that angle is in radians?
CopyableCougar4
 « Reply #2 - Posted 2014-06-21 23:36:55 »

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

Either wandering the forum or programming. Most likely the latter

Github: http://github.com/CopyableCougar4
BurntPizza

« JGO Bitwise Duke »

Medals: 485
Exp: 7 years

 « Reply #3 - Posted 2014-06-21 23:40:18 »

Is angle a double or float? What are x and y?
CopyableCougar4
 « 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

Github: http://github.com/CopyableCougar4
BurntPizza

« JGO Bitwise Duke »

Medals: 485
Exp: 7 years

 « 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.
CopyableCougar4
 « 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

CopyableCougar4

Either wandering the forum or programming. Most likely the latter

Github: http://github.com/CopyableCougar4
BurntPizza

« JGO Bitwise Duke »

Medals: 485
Exp: 7 years

 « 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.

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

Thanks 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

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

 EgonOlsen (76 views) 2018-06-10 19:43:48 EgonOlsen (56 views) 2018-06-10 19:43:44 EgonOlsen (76 views) 2018-06-10 19:43:20 DesertCoockie (258 views) 2018-05-13 18:23:11 nelsongames (156 views) 2018-04-24 18:15:36 nelsongames (155 views) 2018-04-24 18:14:32 ivj94 (896 views) 2018-03-24 14:47:39 ivj94 (160 views) 2018-03-24 14:46:31 ivj94 (809 views) 2018-03-24 14:43:53 Solater (173 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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