In the game i am making i have run into a problem when it comes to using the mouse to fire bullets. The bullets go in a completely different direction then what i want them to do.You have probably had people with this problem several times, i have looked trough 3 of the posts on JG, but cant find an answer that helps me in there.

I am doing this in LibGDX.

These are the related methods;

Part of the InputProcessor in Player.java. (mouseMoved because i dont want to click at the moment, for testing.)

Why are you dividing the player position by 2 in mouseMoved? I assume you want the vector from the player to the mouse so it should just be "screenX - position.x". The reason the window height and width are divided in 2 in TheCherno's code is because he is taking the vector from the midpoint of the screen (width/2 , height/2) to the mouse.

The coordinate system should not matter as long as it is consistent between the mouse coords and the drawing coords.

opiop65:And how would i get on with doing that? I am not even a novice when it comes to this kind of math...

kramin42:I thought that would make the shots come from the characters position, you are right. And i believe i had removed that on an earlier try with this method.

*edit*So, after that brainfart i was able to make it half correct. It fires correctly on the X-axis, but is inverted on the Y-axis.I subtracted the width and height to the x and y to invert them from libgdx format.

*edit*So, this morning i had a fresh look at it, and since only the Y-axis was inverted i changed this method in Bullet.java.

It only works in the center of the map, if i move the player around it skews the direction. I tried putting the bullet and mouse positions to constants and it only happens when the player positions changes.

Can you describe your problem in more detail and/or perhaps post a little movie? For example: Is the problem getting worse the farther you are from the center of the screen? That should indicate that the center is part of the bullet equation somehow. If the shooting works when the player is standing still I´m wondering if something´s happening with the player update that messes up the bullets direction.How huge is the error? Is it almost correct or is it shooting wildly all around the screen when the player is moving? And is the error increasing when you move around or is it constant?

It also seems that you have changed a lot of code since the first post so perhaps posting a code update would be a good idea. In any case it would be nice to see the entire player class and bullet class.

The problem is that the bullet direction gets skewed when i move from the center of the map, the more from it i go, the more it differ, at the top of the map it fires down to the left corner if i try to fire straight up.

Player class, removed stuff like inventory and collision.The Entity class has collision and such.

In LibGdx the x and y starts at the lower left, and not top left, which the mouse and other java does. Thus i added that stuff to make them equal, i have noticed though that i dont need to add the width for the x since its 0 at that side anyways. But the problem is still there.

Disregard the mouseX / musX discrepancies, i write some stuff in swedish and i thought i had changed all the mus to mouse in my post so people wont get confused.

Anyways, i have checked the positions more and more, and it seems the problem lies somewhere in the way coordinates works for the maps loaded into LibGdx.Check this picture;

The dots are at (0,0), (240,160)(center) and (320,480) top right.I have also added a if player is outside those bounds, don't move there, and in the picture i am moving upwards, but cant get further then that.

But then the firing should be all right if i am within those bounds, but it isnt. :/

Woop!I fixed it.I found an answer online that converted the mouse position on the screen to the position ingame.With that it is working as intended. And since it gives the coordinates in the same fashion i do not need to invert the numbers.

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