Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (604) Games in Android Showcase (171) games submitted by our members Games in WIP (654) 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 655 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

Senior Devvie

Medals: 17

 « 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
 Games published by our own members! Check 'em out!
BurntPizza

« JGO Bitwise Duke »

Medals: 420
Exp: 6 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: 420
Exp: 6 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: 420
Exp: 6 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

You cannot reply to this message, because it is very, very old.

 SHC (28 views) 2015-08-01 03:58:20 Jesse (20 views) 2015-07-29 04:35:27 Riven (40 views) 2015-07-27 16:38:00 Riven (22 views) 2015-07-27 15:35:20 Riven (25 views) 2015-07-27 12:26:13 Riven (15 views) 2015-07-27 12:23:39 BurntPizza (36 views) 2015-07-25 00:14:37 BurntPizza (46 views) 2015-07-24 22:06:39 BurntPizza (31 views) 2015-07-24 06:06:53 NoxInc (37 views) 2015-07-22 22:16:53
 theagentd 51x wessles 49x basil_ 35x KaiHH 26x orangepascal 25x Riven 20x ags1 20x mooman219 17x bornander 15x KudoDEV 13x princec 11x CelestialCreator 11x klaus 10x pquiring 10x Jesse 10x Spasi 8x
 List of Learning Resourcesby gouessej2015-07-09 11:29:36How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21
 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