Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Bullet shooting in mouse direction  (Read 3797 times)
0 Members and 1 Guest are viewing this topic.
Offline CogWheelz
« Posted 2014-08-05 19:21:22 »

I found other posts on this, but things were to complicated for me to understand. Knowing the pythag. theory, how would I make a little more sophisticated shooting?

This is a perfect example of what I'm trying to do.
Click to Play

(Wonder how those dots work  Huh)
Offline cylab

JGO Ninja


Medals: 55



« Reply #1 - Posted 2014-08-05 19:41:55 »

Please, people, learn vector math...
http://www.wildbunny.co.uk/blog/vector-maths-a-primer-for-games-programmers/

Mathias - I Know What [you] Did Last Summer!
Offline cylab

JGO Ninja


Medals: 55



« Reply #2 - Posted 2014-08-05 20:31:03 »

Some more info
o you need to understand 2d vectors
  -> they are variables with an x and an y component and can be used to do stuff like described in the link above
o you need to understand the difference between vectors and points
  -> technically they look the same (having x and y), but logically they are different (a point is a position, a vector represents direction and length)
o you need to understand game loops
  -> the loop that periodically calls your update and render methods
o you need to understand what a timestep is
  -> the time period between your update calls
o you need to understand fixed and variable timesteps
  -> fixed timesteps are locked. For example to 1/60s.
o fixed timestep is easier for animating your game
  -> but the gameloop needs to make sure of it by variable sleep times to wait before the next gameloop iteration
o you need to understand when to use floats vs. ints
  -> only use ints for final rendering. use floats (or doubles) for variables needed in computation or to store (varying) positions

regarding your question:
o you need a direction vector pointing from the turret to the mouse
  -> it's easily calculated by substracting the turret position from the mouse position (and normalize the result)
o you need a speed variable containing the pixels per timestep
  -> the amount of pixels the bullet should fly per frame
o you need a bullet position variable that adds up the direction*speed*timestep per update call
  -> this needs to have float components

pseudo code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
// assume this values
float timeStep=1f/60f;
float turretX=10f;
float turrety=10f;
float bulletSpeed=3f; // speed is 3 pixels per second

// if a bullet is fired it is initialized like this
float bulletX=turretX;
float bulletY=turretY;

// and you need to calculate the direction vector for it's movement
float dirX= getMouseXFromSomeWhere()-turretX;
float dirY= getMouseYFromSomeWhere()-turretY;
// you need to "normalize" the direction vector to be able to use the speed variable
float dirLength= Math.sqrt(dirX*dirX + dirY*dirY);
dirX=dirX/dirLength;
dirY=dirY/dirLength;

// now on every update, you can add up the direction * speed * timestep to the bullet
bulletX=bulletX+(dirX*bulletSpeed*timeStep);
bulletY=bulletY+(dirY*bulletSpeed*timeStep);

// on every render, you can render the bullet sprite at that position
g.drawImage(bulletImage, (int)bulletX, (int)bulletY, null); // rendering often uses ints for coordinates


as you can see, standalone x and y variables are just the components of vectors grouped by a naming convention, so you could also use a vector class for that:

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  
32  
33  
34  
35  
// some minimal vector class
class Vec
{
    public float x, y;
    public Vec(float x, float y)
    {
        this.x=x;
        this.y=y;
    }
    public normalize()
    {
        float l=Math.sqrt(x*x + y*y);
        x/=l;
        y/=l;
    }
}

// since vectors and points look technically similar, you can "abuse" a vector to store positions:
Vec turret= new Vec(10f,10f);

// creating the direction vector
Vec dir=new Vec(getMouseXFromSomewhere(),getMouseYFromSomewhere());
dir.x-=turret.x;
dir.y-=turret.y;
dir.normalize();

// initializing a new bullet
Vec bullet=new Vec(turret.x, turret.y);

// animate the bullet on update...
bullet.x+=speed*dir.x*timeStep;
bullet.y+=speed*dir.y*timeStep;

// render the bullet sprite
g.drawImage(bulletImage, (int)bullet.x, (int)bullet.y, null);


Of course you can add more methods to the Vec class to make the code using vectors look more concise...

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Rayexar
« Reply #3 - Posted 2014-08-07 12:53:20 »

Get the position of the mouse (mouseX, mouseY), and the position of the gun (gunX, gunY). The vector from the gun to the mouse is (mouseX-gunX, mouseY-gunY).

Create a bullet at (gunX, gunY), and depending on how you handle movement: increment the position of the bullet by (mouseX-gunX, mouseY-gunY), multiplied by a constant depending on the speed of the bullet. The constant would be 1 if you want it to hit straight away, 0.5 if you want it to hit in two frames etc.
Offline CogWheelz
« Reply #4 - Posted 2014-08-07 16:45:11 »

I'm trying and so far I got some weird, slow jiggly movement. While I mess with it, here's my current screen class.

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  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
public class GameScreen implements Screen{
   
   MyGame mg;
   
   float delta = 1/60f;
   
   
   SpriteBatch batch;
   Sprite s;
   
   float dx = 5, dy = 5;
   
   float x = 10, y = 10;
   
   Vec turret= new Vec(10f,10f);
   Vec dir;
   
   public GameScreen(MyGame mg) {
      this.mg = mg;
     
   }

   @Override
   public void render(float delta) {
      Gdx.gl.glClearColor(1F, 1F, 1F, 1F);
       Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
      update(delta);
     
      batch.begin();
         batch.draw(s, x, y);
      batch.end();
     
   }
   
   public void update(float delta) {
     
      //x += dx;
      //y += dy;
     
     
     
     
      if(Gdx.input.isKeyPressed(Keys.SPACE)) {
         dir.x-=x;
         dir.y-=y;
         dir.normalize();
         
         
         
      }
     
      x+=dx*dir.x*delta;
      y+=dy*dir.y*delta;
     
     
   }

   @Override
   public void resize(int width, int height) {
      // TODO Auto-generated method stub
     
   }

   @Override
   public void show() {
      batch = new SpriteBatch();
       s = new Sprite(new Texture(Gdx.files.internal("HUDS/button_Back.png")));
       dir=new Vec(Gdx.input.getX(), Gdx.input.getY());
      dir.x-=x;
      dir.y-=y;
      dir.normalize();
   }

   @Override
   public void hide() {
      // TODO Auto-generated method stub
     
   }

   @Override
   public void pause() {
      // TODO Auto-generated method stub
     
   }

   @Override
   public void resume() {
      // TODO Auto-generated method stub
     
   }

   @Override
   public void dispose() {
      // TODO Auto-generated method stub
     
   }

}


The Vec class is the same as what cylab showed me.


EDIT: Forgot to add the second position *facepalm*
Offline CogWheelz
« Reply #5 - Posted 2014-08-07 21:07:58 »

(After some League XD) I got everything working, almost fine. The only problem is the image doesn't move in the oposite direction, and it isn't exactly where it should be shot.

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  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
public class GameScreen implements Screen{
     
      MyGame mg;
     
     
      SpriteBatch batch;
      Sprite s;
     
     
     
      float timeStep=1f/60f;
      float turretX=10f;
      float turretY=10f;
      float bulletSpeed=0.1f;
     
      float bulletX=turretX;
      float bulletY=turretY;
     
      float dirX= 100 - turretX;
      float dirY= 100 - turretY;
     
      public GameScreen(MyGame mg) {
         this.mg = mg;
         
         
         float dirLength= (float) Math.sqrt(dirX*dirX + dirY*dirY);
         dirX=dirX/dirLength;
         dirY=dirY/dirLength;
         
         
         
         
      }

      @Override
      public void render(float delta) {
         Gdx.gl.glClearColor(1F, 1F, 1F, 1F);
          Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
         update(delta);
         
         batch.begin();
            batch.draw(s, bulletX, bulletY);
         batch.end();
         
      }
     
      public void update(float delta) {
         if(Gdx.input.isKeyPressed(Keys.SPACE)) {
         dirX= Gdx.input.getX() - turretX;
         dirY= Gdx.input.getY() - turretY;
         }
         
         bulletX=bulletX+(dirX*bulletSpeed*timeStep);
         bulletY=bulletY+(dirY*bulletSpeed*timeStep);
         
         
         
      }

      @Override
      public void resize(int width, int height) {
         // TODO Auto-generated method stub
         
      }

      @Override
      public void show() {
         batch = new SpriteBatch();
          s = new Sprite(new Texture(Gdx.files.internal("HUDS/button_Back.png")));
         
      }

      @Override
      public void hide() {
         // TODO Auto-generated method stub
         
      }

      @Override
      public void pause() {
         // TODO Auto-generated method stub
         
      }

      @Override
      public void resume() {
         // TODO Auto-generated method stub
         
      }

      @Override
      public void dispose() {
         // TODO Auto-generated method stub
         
      }

   }


Click to Play
Offline PandaMoniumHUN

JGO Coder


Medals: 32
Exp: 3 years


White-bearded OGL wizard


« Reply #6 - Posted 2014-08-07 21:42:30 »

The only problem is the image doesn't move in the oposite direction, and it isn't exactly where it should be shot.
What do you mean by "image doesn't move in the opposite direction"? If you mean that the Y value is flipped that's because the way your camera is set up. In LibGDX the mouse position (0; 0)  means the top-left corner, not the bottom left. You can fix that by using this instead of your current bullet launching code:
1  
2  
3  
4  
if(Gdx.input.isKeyPressed(Keys.SPACE)){
   dirX= Gdx.input.getX() - turretX;
   dirY = Gdx.graphics.getHeight() - Gdx.input.getY() - turretY;
}


Looking through your code everything seems to be alright, expect for a few general coding problems.
Ditch your timestep variable, use delta instead. Start using LibGDX's Vector2 classes instead of storing X and Y parameters in separate floats. The vector2 class has lots of useful methods like nor, to normalize the vector, this way you can avoid the whole divide the components by the vector's length procedure in the constructor.

My Blog | Jumpbutton Studio - INOP Programmer
Can't stress enough: Don't start game development until you haven't got the basics of programming down! Pointing
Offline CogWheelz
« Reply #7 - Posted 2014-08-07 21:55:05 »

time step was delta, but I just changed the name for sake of the code. And I meant that  I wanted to press space bar and the image fly past the mouse's exact location.
Offline PandaMoniumHUN

JGO Coder


Medals: 32
Exp: 3 years


White-bearded OGL wizard


« Reply #8 - Posted 2014-08-07 22:29:04 »

time step was delta, but I just changed the name for sake of the code. And I meant that  I wanted to press space bar and the image fly past the mouse's exact location.
That happens because you only compute the direction once and then translate your object in that direction every frame.

You can't expect your projectile to just simply stop where you want it to. What you want to do is rather than computing the direction on the press of the spacebar you want to save the mouse's location and compute the direction against that every frame. However, this way your projectile will likely end up going back and forth around your point and that's because it "steps too big", so it goes over your point, and then tries to come back again, repeating the process from here. The way to fix this is to compute the distance from your projectile's location to the target location and if the distance is smaller or equal to the distance of your projectile's step vector's length then set the projectile's location to be at the target location and stop the movement.

I know this probably doesn't make too much sense the way I just explained it, but I'm too sleepy to explain it in more detail so I'll come back to the topic in the morning and try to elaborate. Tongue

My Blog | Jumpbutton Studio - INOP Programmer
Can't stress enough: Don't start game development until you haven't got the basics of programming down! Pointing
Offline CogWheelz
« Reply #9 - Posted 2014-08-07 22:42:00 »

time step was delta, but I just changed the name for sake of the code. And I meant that  I wanted to press space bar and the image fly past the mouse's exact location.
That happens because you only compute the direction once and then translate your object in that direction every frame.

You can't expect your projectile to just simply stop where you want it to. What you want to do is rather than computing the direction on the press of the spacebar you want to save the mouse's location and compute the direction against that every frame. However, this way your projectile will likely end up going back and forth around your point and that's because it "steps too big", so it goes over your point, and then tries to come back again, repeating the process from here. The way to fix this is to compute the distance from your projectile's location to the target location and if the distance is smaller or equal to the distance of your projectile's step vector's length then set the projectile's location to be at the target location and stop the movement.

I know this probably doesn't make too much sense the way I just explained it, but I'm too sleepy to explain it in more detail so I'll come back to the topic in the morning and try to elaborate. Tongue

Alright, If you're not too sleepy, I've been trying to make it slingshot based on where a mouse saves a vector point but I'm not so successful. Hopefuly I get it soon :/

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  
32  
33  
34  
35  
36  
37  
if(Gdx.input.isButtonPressed(0)) {
          dir=new Vec(Gdx.input.getX(), Gdx.input.getY());
         
         }
         
        // if(!Gdx.input.isButtonPressed(0)) {
         //    dir=new Vec(0, 0);
           
          //  }
         
         if(Gdx.input.isKeyPressed(Keys.SPACE)) {
            dirX= dir.x * turretX;
            dirY= dir.y * turretY;
         }
         
         if(Gdx.input.isKeyPressed(Keys.SPACE)) {
            dirX= dir.x ;
            dirY= dir.y ;
         }
         
         
         
         if(Gdx.input.getX() < bulletX){
         bulletX=bulletX-(dirX*bulletSpeed*timeStep);
         }
         if(Gdx.input.getX() > bulletX) {        
         bulletX=bulletX+(dirX*NegbulletSpeed*timeStep);
         }
         
         if(Gdx.input.getY() < bulletY){
            bulletY=bulletY+(dirY*bulletSpeed*timeStep);
            }
       
         
         if(Gdx.input.getY() > bulletY){            
         bulletY=bulletY-(dirY*NegbulletSpeed*timeStep);
         }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 301
Exp: 6 years



« Reply #10 - Posted 2014-08-07 22:51:32 »

Why the conditionals? Also if you're using libGDX it has a nice Vector2 class.

1  
2  
3  
4  
5  
6  
// remember to flip y axis
Vector2 mouse = new Vector2(Gdx.input.getX(), Gdx.graphics.getHeight() - 1 - Gdx.input.getY());

Vector2 dir = mouse.sub(turret).nor().mul(bulletSpeed * timeStep);

bullet.add(dir); // move in direction 'dir' at 'bulletSpeed' for 'timeStep'
Offline CogWheelz
« Reply #11 - Posted 2014-08-07 23:49:43 »

I used the conditions to atempt to get it done. In this line of your code:
1  
         Vector2 dir = mouse.sub(turret).nor().mul(bulletSpeed * timeStep);


It throws a syntax error:
1  
The method mul(Matrix3) in the type Vector2 is not applicable for the arguments (float)


And when I run it..... the exception... is..:
1  
2  
3  
4  
5  
6  
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.Error: Unresolved compilation problem: 
   The method mul(Matrix3) in the type Vector2 is not applicable for the arguments (float)

   at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Caused by: java.lang.Error: Unresolved compilation problem:
   The method mul(Matrix3) in the type Vector2 is not applicable for the arguments (float)


Nearly the same.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 301
Exp: 6 years



« Reply #12 - Posted 2014-08-07 23:52:53 »

My bad, it's
.scl(bulletSpeed * timeStep);


Offline CogWheelz
« Reply #13 - Posted 2014-08-08 00:00:46 »

I'm getting a nullpointer from the bullet Vector. I'm not sure why, but is it too much to ask for a small code example? Mainly the logic to making an image in the bottom left corner of your screen slide to your mouse wherever it is? In my case I need it to fly past the mouse.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 301
Exp: 6 years



« Reply #14 - Posted 2014-08-08 00:15:00 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
class Bullet {
   
   public static final float BULLET_SPEED = 10; // modify to your liking

   Vector2 velocity = new Vector2();
   boolean velocityHasBeenSet = false;
   Vector2 position = new Vector2(); // alternatively set this in constructor

   public void update(float delta) {
      if(Gdx.input.isButtonPressed(0) && !velocityHasBeenSet) {
         Vector2 dir = new Vector2(Gdx.input.getX(), Gdx.graphics.getHeight() - 1 - Gdx.input.getY());
         dir.sub(position).nor().scl(BULLET_SPEED);
         velocity.set(dir);
         velocityHasBeenSet = true;
      }

      position.add(velocity.scl(delta));
   }

   public void render(SpriteBatch batch) {
      // render your image or whatever at position
   }
}


So you'd probably init the bullet at the position of your turret, and call update() and render() on all bullet entities each frame.
Do what you like in regards to velocityHaBeenSet, you said you wanted it to fly past the mouse, so the simplest solution is to only set the velocity the first time the mouse is pressed. The bullet will fly off in that direction forever after.
Offline Slyth2727
« Reply #15 - Posted 2014-08-08 00:42:56 »

...

Just to add on to his excellent answer, there is such a thing as a position vector. It really is the exact same thing as a point, however it does exist. (You may have said this later in the post, however I saw you saying that there're only directional/length vectors.) Incredible explanation other than that!

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline lcass
« Reply #16 - Posted 2014-08-08 00:47:26 »

for doing this on one of my older game I used this to get the increments of the bullet and angle , just put it in a method then use a for loop to get the bullet to move.
1  
2  
3  
4  
delta_x = (mh.x / 2) - (play.getx() - xoffset) - 16;
      delta_y = (mh.y / 2) - (play.gety() - yoffset) - 16;
      //anglea = Math.toDegrees(Math.atan2(delta_y, delta_x));
      angle = Math.atan2(delta_x, delta_y);
Offline CogWheelz
« Reply #17 - Posted 2014-08-08 01:07:13 »

If I was supposed to render like so
1  
batch.draw(s, position.x, position.y);
I think something is wrong with that neat code. The image is static at 0,0.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 301
Exp: 6 years



« Reply #18 - Posted 2014-08-08 01:31:08 »

Derp:

1  
2  
-position.add(velocity.scl(delta));
+position.add(velocity.cpy().scl(delta));


Other than that just make sure you call update() on your Bullets.
Offline CogWheelz
« Reply #19 - Posted 2014-08-08 01:47:33 »

Derp:

1  
2  
-position.add(velocity.scl(delta));
+position.add(velocity.cpy().scl(delta));


Other than that just make sure you call update() on your Bullets.

Is this a way of saying add and remove that?

Still no use, I'll link a pastebin.
http://pastebin.com/mRCUBufY
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 301
Exp: 6 years



« Reply #20 - Posted 2014-08-08 02:02:08 »

Why would you de-encapsulate? I went straight to an "entity list" of bullets just to show why you shouldn't.

http://pastebin.java-gaming.org/bd41013570b1b

EDIT: Crap, copied the un-fixed position.add(), just change it to use the .cpy()
Offline CogWheelz
« Reply #21 - Posted 2014-08-08 02:21:51 »

Why would you de-encapsulate? I went straight to an "entity list" of bullets just to show why you shouldn't.

http://pastebin.java-gaming.org/bd41013570b1b

EDIT: Crap, copied the un-fixed position.add(), just change it to use the .cpy()

Must have chose the wrong option in pastebin. The code is does exactly what mine did, except yours is worded differently. I just pasted it into eclipse and imported everything, still no movement.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 301
Exp: 6 years



« Reply #22 - Posted 2014-08-08 02:25:46 »

Try removing the hasBeenSet check: (Also it's probably a good idea to not use hardcoded values for buttons)

1  
2  
-if(Gdx.input.isButtonPressed(0) && !velocityHasBeenSet) {
+if(Gdx.input.isButtonPressed(Buttons.LEFT)) {


Otherwise I really don't know why this wouldn't work other than you haven't made this the active screen.
Offline CogWheelz
« Reply #23 - Posted 2014-08-08 02:36:51 »

Wow, removing the Boolean made it work, the speed is just really slow for now. Thanks for helping me out  Grin
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 301
Exp: 6 years



« Reply #24 - Posted 2014-08-08 02:38:05 »

Increase BULLET_SPEED to make bullets faster. That's what it's there for.
Offline CogWheelz
« Reply #25 - Posted 2014-08-08 02:40:57 »

Increase BULLET_SPEED to make bullets faster. That's what it's there for.

I had to bring it up to 10k. Without removing that one part, it actually did work, but my button mechanics aren't too good, and the speed was low, so It wasn't noticeable.
Offline PandaMoniumHUN

JGO Coder


Medals: 32
Exp: 3 years


White-bearded OGL wizard


« Reply #26 - Posted 2014-08-08 12:13:39 »

Oh, the struggle when you don't understand what your code really does. I would strongly suggest you to go over some online linear algebra tutorials about vectors, it will help a lot.
BurntPizza's code is the good way of doing what you want but using that code the bullet still won't stop at the cursor position (not sure why you need this feature though). If that's what you want do what I suggested: Check the bullet's velocity's length and the distance between the bullet and it's destination and if the distance is smaller than the velocity's length set the bullet to be at the destination, stop the movement (maybe apply some condition to stop moving).

A small graph that I put together, hope it's helps to understand the problem:

(click for full res)

My Blog | Jumpbutton Studio - INOP Programmer
Can't stress enough: Don't start game development until you haven't got the basics of programming down! Pointing
Offline CogWheelz
« Reply #27 - Posted 2014-08-08 15:45:52 »

Okay, I'll go read about vectors and linear algebra. Based off of your graph, I'm in need of the strength. Why I need it, I'm simply trying to make a bullet shoot towards the mouse like a shooting game.  Stare
Offline CogWheelz
« Reply #28 - Posted 2014-08-09 19:53:23 »

Besides learning about Vectors, I think this was a pretty useless attempt. I was trying to achieve this,
Click to Play


and I got a bullet teleporting half way at a speed of 29999. I don't really see the movement so I might as well do something like this


Like the first gif, It was suposed to move towards the target at a steady  enough pace to look a bit realistic.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 301
Exp: 6 years



« Reply #29 - Posted 2014-08-09 21:13:58 »

This is simple use of vectors to simulate basic physics, specifically numeric integration of the equations of motion.
See more here: http://gafferongames.com/game-physics/integration-basics/

http://pastebin.java-gaming.org/d4103275b0b17

Click to Play
Pages: [1] 2
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (14 views)
2014-12-28 02:10:29

BurntPizza (17 views)
2014-12-27 22:38:51

Mr.CodeIt (13 views)
2014-12-27 04:03:04

TheDudeFromCI (17 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (86 views)
2014-12-05 12:39:02
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
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!