Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
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  
  My bullet(missile,magic) doesn't want to go to mouse x,y!? [solved]  (Read 2303 times)
0 Members and 1 Guest are viewing this topic.
Offline GNecro1
« Posted 2014-07-19 09:07:13 »

I asked this question before but i had particles on my missile and i thougth that it was working, and it's not!   Emo

Well i get this:

before i changed static x,y to not static:



after:




GitHub for the code
Look in the src/Entity for the player(shooting from the player)
and missile, camera is in the control


I don't know why is it doing this? I want it to go at any angle! Why it doesn't ?

Java freak! Cheesy
Offline trollwarrior1
« Reply #1 - Posted 2014-07-19 09:41:05 »

Here is how you calculate rotation between player and mouse:
1  
float rot = Math.atan2(xmouse - xplayer, ymouse - yplayer);
Offline GNecro1
« Reply #2 - Posted 2014-07-19 09:45:52 »

And from that how can i calculate the velocity for x,y?

Java freak! Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tkausl
« Reply #3 - Posted 2014-07-19 09:49:10 »

Simple Pythagoras? Or what do you need?

My English isnt that great. Correct me, if you want, im still learning this Language Smiley
Offline NegativeZero

JGO Knight


Medals: 30
Projects: 2
Exp: 3 years


Zero but not.


« Reply #4 - Posted 2014-07-19 09:51:17 »

To calculate the x & y velocity you need to use this code:
1  
2  
3  
4  
5  
float x = xMouse - xPlayer;
float y = yMouse - yPlayer;
float length = Math.sqrt((x * x) + (y* y));
xVelocity = x / length * speed;
yVelocity = y / length * speed;


Sorry if it doesn't work I am very tired and I didn't test it.
Offline trollwarrior1
« Reply #5 - Posted 2014-07-19 10:16:41 »

Here are a couple of utility methods I wrote that work like a charm:

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  
   public static vec2 point(vec2 pivot, vec2 point, float rotation) {
     
      float rot = (float)(1f / 180 * rotation * Math.PI);
     
      float x = point.x - pivot.x;
      float y = point.y - pivot.y;
     
      float newx = (float)(x * Math.cos(rot) - y * Math.sin(rot));
      float newy = (float)(x * Math.sin(rot) + y * Math.cos(rot));
     
     
      newx += pivot.x;
      newy += pivot.y;
     
      return new vec2(newx, newy);
   }

   public static int angle(vec2 pivot, vec2 point) {

      float xdiff = pivot.x - point.x;
      float ydiff = pivot.y - point.y;
     
      float angle = (float) ((Math.atan2(xdiff, ydiff)) * 180 / Math.PI);
     
      return -(int)angle;
   }


Here is vec2 class.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public class vec2 {

   public float x, y;

   public vec2(float x, float y) {
      this.x = x;
      this.y = y;
   }

   public int getX() {
      return (int) x;
   }

   public int getY() {
      return (int) y;
   }

}


Now, to get velocities, you do something like this:
1  
2  
3  
float rot = angle(new vec2(player.x, player.y), new vec2(mouse.x, mouse.y));

vec2 velocity = point(new vec2(0, 0), new vec2(0, -speed), rot);


To get x and y velocities, do velocity.x for x velocity and velocity.y for y velocity.
Offline PandaMoniumHUN

JGO Coder


Medals: 32
Exp: 3 years


White-bearded OGL wizard


« Reply #6 - Posted 2014-07-19 10:18:55 »

So why exactly did you open this thread when you already have one for the exact same purpose?

By the way NegativeZero's way of doing it is correct (the only mistake is that you have to cast Math.sqrt's result to be a float).
Basically you create a vector that points from your player's position to your mouse's position, normalize the vector (that's the part when you divide the components by the length of the vector) and then multiply it by the projectile speed. Ta da. Cheesy

@trollwarrior1: There's no need to involve angles in this, that just unnecessarily makes the code more complex.

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 trollwarrior1
« Reply #7 - Posted 2014-07-19 10:23:33 »

Its called abstractions, and while it adds more complexity to the code, the code is easier to read, if you don't need to understand what is happening behind the scenes. 2 lines of very simple code to get the velocities is very small simple to me at least. Besides, you will probably want to render the sprites rotated, in which case you will need to calculated rotation anyway, so whatever.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2014-07-19 10:36:28 »

Patronising much?

Anyway, for somebody with such attitude, you'd at least think the suggested code was flawless. Your atan2(x, y) must be atan2(y, x). Last but not least, I agree with NegativeZero: his code is faster, simpler, easier to grasp.

People calculating an angle using atan2, then passing that to sin and cos, should be brushing up on their vector math. The spurious casts to int are equally a red flag.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline trollwarrior1
« Reply #9 - Posted 2014-07-19 10:56:38 »

Guess I will go do that, because I can't argue with someone who has 10 times more experience in programming than me.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PandaMoniumHUN

JGO Coder


Medals: 32
Exp: 3 years


White-bearded OGL wizard


« Reply #10 - Posted 2014-07-19 11:15:39 »

Its called abstractions, and while it adds more complexity to the code, the code is easier to read, if you don't need to understand what is happening behind the scenes. 2 lines of very simple code to get the velocities is very small simple to me at least. Besides, you will probably want to render the sprites rotated, in which case you will need to calculated rotation anyway, so whatever.
Abstraction is not about adding levels of unnecessary code to the project. It's about providing a higher-level interface for easier development.
Even if you would like to render the sprites rotated, calculating the rotation (normally) has nothing to do with the mouse position.
The sad part is that you didn't even stop to think about my (or rather NegativeZero's) solution instead you continued to argue and only stopped because someone who has '10 times as much programming experience as you' proved you wrong. Sad

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 SauronWatchesYou
« Reply #11 - Posted 2014-07-19 11:22:32 »

I just want to ask something as this is relevant to the asked question persecutioncomplex

Panda, you helped me understand vector math with finding positions and normalizing them using the built in function that comes with LibGDX. As I understand, normalizing a vector gives it a unit length of 1 as we only care about the direction and not the length of the vector. However, i'm struggling to understand how the vector still knows which direction to go when it has a length of 1? If that makes sense. Sorry for the noob question, my math is poor Pointing
Offline trollwarrior1
« Reply #12 - Posted 2014-07-19 11:25:58 »

@panda

Sorry, but in my world, objects are not moving from point a to point b, they are moving in the direction they are rotated to.
Offline danieldean
« Reply #13 - Posted 2014-07-19 11:51:28 »

The vector still retains it's (i, j) for 2D or (i, j, k) if you're in 3D. You might be more familiar with (x, y) and (x, y, z). For example take a vector:

1  
(2, 2)


It's magnitude is found using Pythagoras Theorem:

1  
sqrt(2 * 2 + 2 * 2) = 2.828427125


Then to make it a unit vector multiply each component by the reciprocal of the magnitude:

1  
(1 / 2.828427125 * 2, 1 / 2.828427125 * 2)


Gets you:

1  
(0.707106781, 0.707106781)


Which if you find the magnitude of again:

1  
sqrt(0.707106781 * 0.707106781 + 0.707106781 * 0.707106781) = 1


So you can see it still retains the direction but has unit magnitude?
Offline GNecro1
« Reply #14 - Posted 2014-07-19 12:50:25 »

I use the code like NegativeZero said but i get the effect shown on the screen and i want it to go in a straight line, i tried with vec2 same effect,Is it possible or should I leave it as it is?
http://www.mediafire.com/download/sunoh7d6ctwalf1/game.jar
here you can see what is happening!

Java freak! Cheesy
Offline trollwarrior1
« Reply #15 - Posted 2014-07-19 12:52:05 »

My guess would be that you're using integer positions instead of float positions somewhere.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #16 - Posted 2014-07-19 13:10:18 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public class vec2 {

   public float x, y;

...

   public int getX() {
     return (int) x;
   }

   public int getY() {
     return (int) y;
   }

}


My guess would be that you're using integer positions instead of float positions somewhere.

It's a bit ironic... but... maybe there?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #17 - Posted 2014-07-19 13:20:57 »

The angles are snapped to multiples of 15 degrees. This is usually not what truncation to integers results into.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline GNecro1
« Reply #18 - Posted 2014-07-19 13:24:30 »

Then how can i make it not snapp to 15 degrees?

Java freak! Cheesy
Offline trollwarrior1
« Reply #19 - Posted 2014-07-19 13:29:48 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public class vec2 {

   public float x, y;

...

   public int getX() {
     return (int) x;
   }

   public int getY() {
     return (int) y;
   }

}


My guess would be that you're using integer positions instead of float positions somewhere.

It's a bit ironic... but... maybe there?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public class vec2 {

  public float x, y; // there is a reason these are public



   public int getX() {
      return (int) x;
   }

   public int getY() {
      return (int) y;
   }

}
Offline danieldean
« Reply #20 - Posted 2014-07-19 13:33:20 »

If you're using the x and y somewhere where they need to be integers cast them there not at root? In fact if that's you argument why not just access the variables directly and cast them and do away with the getters altogether? It's confusing to expose getters and setters that degrade the precision especially if you offer the code to others.
Offline SauronWatchesYou
« Reply #21 - Posted 2014-07-19 13:39:45 »


So you can see it still retains the direction but has unit magnitude?

Thank you for this Smiley I understand it better now
Offline GNecro1
« Reply #22 - Posted 2014-07-19 13:41:26 »

Ok weard stuff is going on xD
Click to Play

Java freak! Cheesy
Offline GNecro1
« Reply #23 - Posted 2014-07-19 14:05:38 »

Click to Play


Yea it works xD i Just made new x,y floats and and works sometimes i just don't understand computer/java thanks everybody:D

Java freak! Cheesy
Offline PandaMoniumHUN

JGO Coder


Medals: 32
Exp: 3 years


White-bearded OGL wizard


« Reply #24 - Posted 2014-07-19 15:03:58 »

Panda, you helped me understand vector math with finding positions and normalizing them using the built in function that comes with LibGDX. As I understand, normalizing a vector gives it a unit length of 1 as we only care about the direction and not the length of the vector. However, i'm struggling to understand how the vector still knows which direction to go when it has a length of 1? If that makes sense. Sorry for the noob question, my math is poor Pointing
Normalization is all about scaling a value to be between 0 and 1. This is useful for many things, but most game developers only use this to remove the magnitude of a vector and only keep it's direction. You can achieve normalization by dividing a number with it's maximum possible value. So if you want to normalize numbers that can go from 0 to 50 to the range 0 and 1 you have to divide your numbers by the max value, that is 50.

Same thing with vectors, although not as easy because vectors have multiple components. If a 2D vector only travels on the X axis for example, e.g. a vector that is [50; 0] than the vector's length is 50, so you have to divide all components with 50 resulting in a 2D vector [1; 0] which correctly represent the direction of the original vector. Things get a bit more complicated when the vector travels on both the X and Y axis, e.g. when you have a 2D vector [50; 18]. In this case you have to calculate the vector's length which is ~53.1413 in this case and divide the components with that number, resulting in ~ [0.9409; 0.3387] which again correctly represent the direction of the original vector. Hope things are a bit clearer now.

@panda

Sorry, but in my world, objects are not moving from point a to point b, they are moving in the direction they are rotated to.
That's cool, in my world people usually represent directions like forward and right using 2D vectors (or with a single 3x3 or 4x4 matrix, for that matter) and not with angles because it's more efficient to do it this way instead of computing the direction every single time you want to use it. If we want to rotate something we just use rotation matrices. Smiley
No need to be arrogant or sarcastic, I was never doing that to you either. Pointing

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 SauronWatchesYou
« Reply #25 - Posted 2014-07-21 18:02:57 »

So am I right in thinking this:

I have a vector which is 4, 4 and find the magnitude by doing 4x4 + 4x4 = 32 and the square root is 5.6 which is the magnitude of the vector. I then do 4 / 5.6, 4 / 5.6 which is 0.714285, 0.714285. This keeps the vector on the same angle as before but now has a magnitude of 1 if we do:

0.714285 x  0.714285,  0.714285 x 0.714285 = 1.0204.

Then when I want something like bullets to travel towards the player I do: playerXPos += direction.x * speed. This makes the bullet travel on the angle towards the player at the desired speed.

Am I right in thinking that the direction.x knows it has a magnitude of 1 and the angle it is going? I'm a little confused about what the direction.x represents. For example, when I multiply the speed onto the direction, is it using the magnitude so the direction.x might equal 4 (if the speed was 4)? This is the only bit I am finding confusing right now and if someone could clear me up on what information the direction.x is holding I would appreciate it Cheesy
Offline trollwarrior1
« Reply #26 - Posted 2014-07-21 18:07:00 »

sqrt(32) = 5.656

EDIT-
If you use this, you will get that magnitude = 0.999, which is 1 technically.
Offline danieldean
« Reply #27 - Posted 2014-07-21 19:00:17 »

So am I right in thinking this:

I have a vector which is 4, 4 and find the magnitude by doing 4x4 + 4x4 = 32 and the square root is 5.6 which is the magnitude of the vector. I then do 4 / 5.6, 4 / 5.6 which is 0.714285, 0.714285. This keeps the vector on the same angle as before but now has a magnitude of 1 if we do:

0.714285 x  0.714285,  0.714285 x 0.714285 = 1.0204.

Then when I want something like bullets to travel towards the player I do: playerXPos += direction.x * speed. This makes the bullet travel on the angle towards the player at the desired speed.

Am I right in thinking that the direction.x knows it has a magnitude of 1 and the angle it is going? I'm a little confused about what the direction.x represents. For example, when I multiply the speed onto the direction, is it using the magnitude so the direction.x might equal 4 (if the speed was 4)? This is the only bit I am finding confusing right now and if someone could clear me up on what information the direction.x is holding I would appreciate it Cheesy

The square root of 32 is 5.656854249... and more, it's irrational, that is it can't be represented by a finite number of digits, so the maths isn't going to be perfect and you will likely not get back to exactly 1.0 as you expect. Finding your magnitude again you haven't show that you square root but you should. As trollwarrior1 has said 0.9... is arbitrarily close to 1.0 and for all intent and purpose can be considered to be 1.0, it's not perfect but things like this rarely are.

The only way you will get this to be perfect is by evaluating it symbolically, i.e:

1  
sqrt(32) = sqrt(2 * 16) = sqrt(2 * 2 * 8) = sqrt(2 * 2 * 2 * 4) = sqrt(2 * 2 * 2 * 2 * 2) = 4 * sqrt(2)


Then use this as you can go no further. Clearly you can't use this in your game so it's of no help.

You might consider looking at this http://www.mathsisfun.com/algebra/vectors.html it's basic but I think will help as it has diagrams to explain.
Offline SauronWatchesYou
« Reply #28 - Posted 2014-07-21 22:59:48 »

I think I understand it all and thanks for the link, it helped a lot due to the diagrams. I'm still confused about what part of the vector is actually added during this code:

1  
playerPosX += velocity.x * speed


Is it the magnitude(1 unit) * speed? Or is it the x value from normalizing the vector? I'm slightly confused on what info is actually being passed to the player position. I have a feeling that the magnitude is * by the speed and the vector then just goes it the direction?
Offline PandaMoniumHUN

JGO Coder


Medals: 32
Exp: 3 years


White-bearded OGL wizard


« Reply #29 - Posted 2014-07-22 08:26:33 »

1  
playerPosX += velocity.x * speed


Is it the magnitude(1 unit) * speed? Or is it the x value from normalizing the vector? I'm slightly confused on what info is actually being passed to the player position. I have a feeling that the magnitude is * by the speed and the vector then just goes it the direction?
The variable naming there is confusing. The 'velocity' shouldn't be called velocity but something like 'direction' instead, and it's supposed to be a normalized vector.
When you multiply a normalized vector with a value then essentially you're manipulating it's magnitude.
If we would use my previous example here [50; 18] which got normalized to ~ [0.9409; 0.3387] and let's say that speed is 20 then we would get the following:
1  
2  
playerPos.x = 0.9409f * 20f;
playerPos.y = 0.3387f * 20f;

If we would calculate playerPos's length now the result would be 20, or at least very close to it.
This is because floating point rounding errors, and you have to watch out for this because it can be a pain at times.
Last time when I was developing the AI for our game I've spent 2 days debugging only to find out that I have one of these nasty errors. 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
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.

TehJavaDev (32 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (22 views)
2014-10-26 10:17:50

Luminem (27 views)
2014-10-26 10:14:04

theagentd (33 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (58 views)
2014-10-16 15:22:06

Norakomi (47 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
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

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!