Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (754) Games in Android Showcase (229) games submitted by our members Games in WIP (842) 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
 Shooting algorithm slows down as you get closer  (Read 4213 times) 0 Members and 1 Guest are viewing this topic.
Fairy Tailz

Senior Newbie

 « Posted 2015-03-15 21:52:34 »

I'm using LWJGL an am trying to implement mechanics for shooting. So far I got it working correctly, but I have it slowing down as the mouse get's closer to the starting  position. This is correct, but not what im looking for. How can I make the bullet shoot at a constant speed no matter how close to the initial position?

CODE: http://pastebin.java-gaming.org/065f082432f14
BurntPizza

« JGO Bitwise Duke »

Medals: 485
Exp: 7 years

 « Reply #1 - Posted 2015-03-15 21:54:28 »

Normalize the (dirX, dirY) vector. It's a direction, it's length should always be 1. The magnitude is specified later as bulletSpeed.
Fairy Tailz

Senior Newbie

 « Reply #2 - Posted 2015-03-15 22:12:01 »

err, im not getting it.

 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  27  28  29  30  31 `class bul{    float timeStep=1f/60f;    float turretX=300f;    float turretY=10f;    float bulletSpeed=2f;    double bulletX;    double bulletY;    double dirX;    double dirY;    public bul( float startX, float startY)    {        this.bulletX = startX;        this.bulletY = startY;        dirX = Math.sqrt(dirX * Mouse.getX());//where do i apply the mouse?        dirY = Math.sqrt(dirY * Mouse.getY());    }    public void update()    {            bulletX = bulletX + (dirX * bulletSpeed * timeStep);            bulletY = bulletY + (dirY * bulletSpeed * timeStep);    }}`
 Games published by our own members! Check 'em out!
Husk

Senior Devvie

Medals: 20
Exp: 3 years

Want to learn everything.

 « Reply #3 - Posted 2015-03-15 23:40:37 »

In your code, you are setting bullet direction to be mouseX/Y - startX/Y, so as you move your mouse further away, the result will be a bigger number, as you move it closer, it will be a smaller number. You use these numbers directly in your bullet update function in multiplication, so they end moving further and shorter depending on your mouse.

What BurntPizza is saying is that you need to normalize these values, which means to set the distance of a vector to 1. You have a function that already does this in your Vec2. Turn dirX/Y in to a Vec2 and normalize it. You then use the values inside the Vec2 in your bullet update function instead.

Pages: [1]
 ignore  |  Print

 DesertCoockie (33 views) 2018-05-13 18:23:11 nelsongames (77 views) 2018-04-24 18:15:36 nelsongames (70 views) 2018-04-24 18:14:32 ivj94 (752 views) 2018-03-24 14:47:39 ivj94 (82 views) 2018-03-24 14:46:31 ivj94 (623 views) 2018-03-24 14:43:53 Solater (98 views) 2018-03-17 05:04:08 nelsongames (179 views) 2018-03-05 17:56:34 Gornova (405 views) 2018-03-02 22:15:33 buddyBro (1065 views) 2018-02-28 16:59:18
 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