Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (736)
Games in Android Showcase (223)
games submitted by our members
Games in WIP (813)
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 3632 times)
0 Members and 1 Guest are viewing this topic.
Offline 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
Offline 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.
Offline 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!
Legends of Yore - The Casual Retro Roguelike
Offline 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  
 
 
You cannot reply to this message, because it is very, very old.

 
cybrmynd (120 views)
2017-08-02 12:28:51

cybrmynd (144 views)
2017-08-02 12:19:43

cybrmynd (138 views)
2017-08-02 12:18:09

Sralse (154 views)
2017-07-25 17:13:48

Archive (624 views)
2017-04-27 17:45:51

buddyBro (733 views)
2017-04-05 03:38:00

CopyableCougar4 (1260 views)
2017-03-24 15:39:42

theagentd (1239 views)
2017-03-24 15:32:08

Rule (1216 views)
2017-03-19 12:43:22

Rule (1268 views)
2017-03-19 12:42:17
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!