You’ll have much better luck if you work from an example that we can run. I’ve tried providing two examples to work from, or you could create your own. But as of now it’s pretty difficult to debug your code, since we can’t run it and it contains a bunch of extra logic that doesn’t really have anything to do with the problem.
Can I just straight up give you my Libgdx project? I don’t ever plan on releasing it to the public anyways.
Here’s the project (LibGDX) packed into a RAR for anyone that wants to help me out. In-Game you zoom in and out with the mouse wheel, the colored triangles in the world are the ships I’m testing out, you can zoom into that and see them. You can also select the middle one and right click another place to move it to test different angles. Thank You! ;D
http://www.mediafire.com/download/x5bxznz5nhbmi0s/Workspace.rar
Somebody might be willing to debug your entire project for you, but I think you’ll have much better luck if you work from a tiny project like the ones I posted that can be copy-pasted and run very easily without any setup or extra logic. It’s up to you though. Good luck.
To note, you have to Negate the result of the subtraction.
-originY - targetY
This is different from :
-(originY - targetY)
You shouldn’t have to negate anything.
Here is my code for shooting bullets with correct angle.
In the Bullet class there are two methods that I use. moveToTarget and rotate. moveToTarget gets my mouseposition when clicking. Remember to unproject the camera if you use mouse for shooting. Rotate method just rotates the bullet to point towards the mouse.
I still have some problems with the collision, but the rotation of bullet and the movement works perfectly. If you can’t get it work with this. I can check your workspace and try it myself.
Bullet class
//targetPos is mouse coordinates, startPosition is where the bullet will be created(in my case where the player is)
public void moveToTarget(Vector2 targetPos, OrthographicCamera camera, Vector2 startPosition, float delta){
Vector3 transformedPos = camera.unproject(new Vector3(targetPos, 0));
if(!isShot()){ //Rotating bullet and calculating direction towards mouse only once
rotate(transformedPos, startPosition); //takes the unprojected position and the spawn position.
//Calculating direction
this.direction = new Vector2(transformedPos.x - startPosition.x, transformedPos.y - startPosition.y);
this.direction.nor(); //normalizes the vector
this.direction.scl(this.getSpeed()); //scaling the vector with bullet speed
this.shot = true;
}
translate(direction.x, direction.y); //moving the bullet
circle.setPosition(this.getX(), this.getY()); // moving the collision circle
}
Also in Bullet class
private void rotate(Vector3 transformedPos, Vector2 startPosition){
float deltaX = transformedPos.x - startPosition.x;
float deltaY = transformedPos.y - startPosition.y;
float degrees = MathUtils.atan2(-deltaX, deltaY) * MathUtils.radiansToDegrees;
this.setRotation(degrees);
}
Good luck.
These are the equations that I came up with:
(Note: I just use Vector2 as a value to hold the x/y pairs that I come up with)
So…
bPos.x = bulletX
bPos.y = bulletY
and so on for the other variables.
//At the moment of bullet creation
Vector2 target = new Vector2(100,100); //target ship
Vector2 pos = new Vector2(0, 0); //mother ship (the ship that is shooting)
float gunDirection = (float) Math.atan2(target.y - pos.y, target.x - pos.x); //in radians
Vector2 bPos = new Vector2(); //bullet position
bPos.x = (float) Math.cos(gunDirection) + pos.x;
bPos.y = (float) Math.sin(gunDirection) + pos.y;
//Movement calculations
float speed = 100.0f;
bPos.x += (float) Math.cos(gunDirection) * speed;
bPos.y += (float) Math.sin(gunDirection) * speed;
After inputting different values for the ship positions, the results seem to work as they should.
Please let me know if I goofed up my math somewhere as I did this all on paper.
Big Bass, This is the exact same thing I’m doing.
And Zeta, That did not work either
Gah, why use angles at all outside of the controls? This is what happens.
What do you mean? I thought you needed to get the angles to figure out the direction of everything?
No: http://www.java-gaming.org/topics/bullet-shooting-in-mouse-direction/34029/msg/320951/view.html#msg320951
Up towards the top of that thread is more info.
In short, a direction vector is simply the position of one thing minus the position of the second thing, normalized.
Use that instead of finding angles between things.
This is what I said in reply number 4…
And yet they’re still trying to use angles…
There is one reason to use angles: if he wants the ships to “flow” by changing their heading. But without a smaller example, everybody is just guessing.
Come on down to the Guess-Fest! One guess for one dollar or 6 guesses for 5 dollars! That’s right, buy 5 guesses and get one guess free!
That’s why I predicated: “outside of the controls”
Everything else should just be straight (ha) vectors. Implicit conversion to and from vector and scalar cartesian and polar spaces everywhere gets you this guessing game complete with shotgun debugging.
So how exactly would I do this with Vectors only? ???
I think you have other problems that you need to fix first, I added a couple print statements (a very useful debugging tool, just print out values and check they are correct):
System.out.println("tx: " + TargetShip.getOriginX() + " ty: " + TargetShip.getOriginY());
System.out.println("px: " + OriginPositionX + " py: " + OriginPositionY);
And I get the following output every time, without change, even attempting to target other ships:
tx: 5120 ty: 5120
px: 6120 py: 5120
Any bullets of course will travel horizontally.
Also, you are storing bullet positions as integers which is problematic because the result of a trig function may not be enough to push it over to the next integer. This means the possible angles it can travel are limited, but it will be as close as it can be. Use floats or doubles. When changing the ship to target the mouse, this problem became clear, as you can visually see the relation between the target and the angle of the shots.