So, here is my problem: I am working on a top down 2d shooter in Java and have a character, which can move and rotate/fire to the mouse direction. Shooting guns etc. works fine but now i am trying to implement a shell ejection system, which "ejects" the shells next to the player. The problem is, that i can't just draw the shells next to the player since the player has an x and y position which are rotating. I want the shells to have a random radius in which they will be ejected. But i don't know how to implement something like this. Any suggestions?

EDIT: That's my shell-ejection method, which doesn't work(the angle represents the player angle):

    public void shellEjection(){
    x=(float) x*((float) (Math.cos(angle)));
    y=(float) y*((float) (Math.sin(angle)));

    double length = Math.sqrt((mouseX - x)*(mouseX - x) + (mouseY - y)*(mouseY - y));

    double xVel = (5 - x) /length * 5*bulletVelocity;
    double yVel = (5 - y) /length * 5*bulletVelocity;

  • 1
    \$\begingroup\$ To which class is this method attached? How is it called? \$\endgroup\$
    – Vaillancourt
    Commented Dec 7, 2016 at 18:56
  • \$\begingroup\$ it's attached to the bullet class. \$\endgroup\$
    – Cringe
    Commented Dec 7, 2016 at 18:59
  • 1
    \$\begingroup\$ What does the code which calls this method look like? How is it used? \$\endgroup\$
    – Vaillancourt
    Commented Dec 7, 2016 at 19:01
  • \$\begingroup\$ edited my code. Hope it's more clear now. \$\endgroup\$
    – Cringe
    Commented Dec 7, 2016 at 19:16
  • 1
    \$\begingroup\$ Who calls that code? :P \$\endgroup\$
    – Vaillancourt
    Commented Dec 7, 2016 at 19:17

1 Answer 1


To give you a pseudo code solution:

  1. Get X and Y from the gun (gun position).
  2. Work out the relative rotation of the gun from the player (purple arrow), so that you can easily offset the shell fall direction later.
  3. Work out the direction and distance that the shell should fly, you will probably want a general direction that the shell will fall in since shells in real life don't tend to fall into exactly the same location.
  4. spawn the shell and start moving it in the direction that you determined in the last step.
  5. stop moving (and manipulating) the shell once it reaches the point you want it to land at.

enter image description here

I made a quick image of what I mean to better illustrate these steps above. That should be the rough steps of what you need to do. Keep in mind that you will want to use an object pool to stop yourself wasting resources and if it's available to you stop updating the shell object once it reaches it's final point so that you only render it (not wasting precious CPU cycles).

Hope this helped :)


From what I understand the problem that you have is that you don't know how to find the XY position of the gun since the gun is at an offset from the middle of the character. I'm guessing that the gun is always in the same position when being fired relative to the center XY (or at least you know about every position or can figure that out another way).

What you can try doing is figuring out what the distance is from the center to the gun, then use Sin and Cos (yay maths) to figure out where the gun is in space based on the rotation. There is a great site that visualises the relationship between sin and cos here.

So to give an example:

enter image description here

Use pythagoras theorem to work out the distance, again check that website i mentioned above to get a better writeup.

  • \$\begingroup\$ Yes, i had the same kind of idea in mind. The angle for the falling shell direction itself is not a problem since it's dependent on the shot-angle. The main problem i have, is the "ejection"-movement/direction itself. The ejection direction is dependent on the rotation which is updating the whole time and that's what i don't really understand how to achieve. \$\endgroup\$
    – Cringe
    Commented Dec 7, 2016 at 21:03
  • \$\begingroup\$ Yes and no, the ejection direction is dependent on the shooting angle only when the player shoots and a shell needs to be ejected. After that the shell only needs to follow its direction while the gun can move anywhere at any angle, after they spawn you should think of them as unrelated objects in your game that you need to move. \$\endgroup\$ Commented Dec 7, 2016 at 21:52
  • \$\begingroup\$ Yes and that's the problem. I don't know how to couple the rotation with the movement(the update of the x and y position to eject the bullet next to the playeror the weapon in this case). \$\endgroup\$
    – Cringe
    Commented Dec 7, 2016 at 22:20
  • \$\begingroup\$ ok i tried working with shapes now. I aligned a line to the player center and it rotates just like i want it to. I thought about maybe letting the bullets fly in the vector of a the bullets length but when i rotate the shape, the x and y positions of the line don't change. I am not that good with trigonometry, so a little help here would be much appreciated :( Maybe a code formula or something? \$\endgroup\$
    – Cringe
    Commented Dec 8, 2016 at 15:54
  • \$\begingroup\$ ok, no problem the edit should have you covered. \$\endgroup\$ Commented Dec 9, 2016 at 16:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .