I am trying to align bullets to my different sprites guns (top down 2D for android). Right now Im using this method to draw my bullets:

public void draw(Canvas canvas){    
    canvas.rotate(angle, x + width / 2, y + height / 2);
    canvas.translate(translaterX, translaterY);
    canvas.drawBitmap(bitmap, x, y, null);

The problem here is the translater, Im always having to add or substract from it to make the bullets spawn from my gun when my sprites rotate and I cant figure it out, is there any standard value for this, like translaterX = bitmap.getwidth()/2? I dont really understand how the translater works and I feel like Ive tried everything, what am I missing? For the examples sake lets say that my gun is always in the middle of the bitmap.

Or is there even another way to do this that is simpler?

  • \$\begingroup\$ I it not obvious what you whant, or what your code should de. For example what are the variables: translaterX, translaterY, bitmap, x, y. Where do they come from, what do they store? \$\endgroup\$ – Arne Jan 19 '14 at 19:17

you'll need to calculate the rotation of the gun from the center of the player sprite

x = (float)Math.cos(playerAngle) * radius;
y = (float)Math.sin(playerAngle) * radius;

radius should be the distance that is from the player center to the position of the gun

| improve this answer | |
  • \$\begingroup\$ Will try when I get a chance and make sure to give u the bounty if it works :) thank you! does make sense \$\endgroup\$ – Green_qaue Jan 20 '14 at 6:03
  • \$\begingroup\$ by x, y you mean translaterX, translaterY correct? \$\endgroup\$ – Green_qaue Jan 21 '14 at 3:12
  • \$\begingroup\$ @Max yes, in your case is traslaterX and traslaterY \$\endgroup\$ – Joyal Jan 21 '14 at 22:17

You have different point locations for each gun, relative to the location of your sprite. Correct?

When you rotate your sprite, you need to rotate these points around the same center of rotation that you used for your sprite.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.