0
\$\begingroup\$

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){    
    update();
    canvas.save();
    canvas.rotate(angle, x + width / 2, y + height / 2);
    canvas.translate(translaterX, translaterY);
    canvas.drawBitmap(bitmap, x, y, null);
    canvas.restore();
}

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?

\$\endgroup\$
1
  • \$\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, 2014 at 19:17

2 Answers 2

1
+50
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\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, 2014 at 6:03
  • \$\begingroup\$ by x, y you mean translaterX, translaterY correct? \$\endgroup\$
    – Green_qaue
    Jan 21, 2014 at 3:12
  • \$\begingroup\$ @Max yes, in your case is traslaterX and traslaterY \$\endgroup\$
    – Joyal
    Jan 21, 2014 at 22:17
0
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

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