Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (563) Games in Android Showcase (151) games submitted by our members Games in WIP (605) 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 498 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
BurntPizza

« JGO Bitwise Duke »

Medals: 356
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: 356
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: 356
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.

 wxwsk8er (49 views) 2015-03-20 15:39:46 Fairy Tailz (42 views) 2015-03-15 21:52:20 Olo (26 views) 2015-03-13 17:51:59 Olo (29 views) 2015-03-13 17:50:51 Olo (33 views) 2015-03-13 17:50:16 Olo (40 views) 2015-03-13 17:47:07 ClaasJG (29 views) 2015-03-10 11:36:42 ClaasJG (37 views) 2015-03-10 11:33:01 Pippogeek (45 views) 2015-03-05 14:36:23 Pippogeek (37 views) 2015-03-05 13:56:12
 LiquidNitrogen 23x KevinWorkman 22x BurntPizza 21x basil_ 21x theagentd 18x Riven 18x EgonOlsen 17x Roquen 14x princec 12x SHC 9x Varkas 9x Slyth2727 9x NegativeZero 9x teletubo 7x nsigma 7x wessles 7x
 How 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:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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