I've recently added an explosion in my game, for when enemies get killed, and it works fine and all, but the explosion wont "spawn" on the enemies x and y. The problem is that I'm not drawing the bitmap from the center, but from its 0,0 position, so the explosion spawns at my enemies 0,0. It's not like the explosion spawns at random places, it spawns around the enemies bitmap, but just not on the center, and it is really annoying to look at.

But if I try to change it so I add them at the center, It creates a really weird effect when rotating them that is hard to explain.

I cant use this: canvas.drawBitmap(bitmap, x - bitmap.getWidth/2, y - bitmap.getHeight/2, null);, or I'm just doing it wrong and missing something.

How I draw my enemies:

    public void draw(Canvas canvas) {
    canvas.rotate((float) rotater, x + (width / 2), y + (height/2));
    int srcX = currentFrame * width;
    int srcY = 0 * height;
    Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
    Rect dst = new Rect(x, y, x + width, y + height);
    canvas.drawBitmap(bitmap, src, dst, null);

How I add my explosions:

    if (enemies.get(i).getHP() <= 0) {  //enemy death
        addExplosion(enemies.get(i).getX(), enemies.get(i).getY());

If you need any more info to be able to help, let me know. Thank you!

  • \$\begingroup\$ did you try with canvas.drawBitmap(bitmap, x + bitmap.getWidth/2, y + bitmap.getHeight/2, null)? \$\endgroup\$
    – VinceFR
    Mar 28, 2013 at 11:04
  • \$\begingroup\$ Is an enemy the same size of the explosion? if so you should be able to use the top-left of your explosion as the top-left of the explosion. If not, you have to determine the center of the explosion and set it at the center of the enemy. If you are using rotation, its origin should be centered too. \$\endgroup\$
    – petervaz
    Mar 28, 2013 at 13:47
  • \$\begingroup\$ No its not the same size, when it is the same size, it works perfectly. @petervaz I'm aware of this, I think its even in the question, but the problem is I cant get it to add at the center for some reason. \$\endgroup\$
    – Green_qaue
    Mar 28, 2013 at 17:13
  • \$\begingroup\$ @VinceFR I got the same idea earlier today, I will try it as soon as I have the time :) \$\endgroup\$
    – Green_qaue
    Mar 28, 2013 at 17:15

2 Answers 2


Try something like this (this is pseudocode, I don't know your objects actual fields)

1 - determine the enemy's center.

centerX = enemy.getX() + (enemy.getWidth() /2 );
centerY = enemy.getY() + (enemy.getHeight() / 2);

2 - assume the center of the explosion is the same center of the enemy and determine that explosion top-left from its size

explosion.setX(centerX - (explosion.getWidth() / 2);
explosion.setY(centerY - (explosion.getHeight() / 2);
  • \$\begingroup\$ thanks! this should do it, cant try it our for a few days but it looks right. :) \$\endgroup\$
    – Green_qaue
    Mar 29, 2013 at 23:08

When you add an explosion, your enemy object should return its center like this: addExplosion(enemies.get(i).getCenterX(), enemies.get(i).getCenterY());

The drawing of an explosion should happen after canvas.restore(); so that is not affected by the enemie's rotation. Every object drawn on Canvas can have its own canvas.save()/cavas.transform/canvas.restore() especially when the rotational pivotal point is not the same for all objects.

Do not create new objects in onDraw, such as new Rect. You can use the same ones with src.set(0,0,0,0);

For each group of objects create a draw method, and in onDraw just call them one by one and pass the canvas. i.e. drawEnemyShips(canvas); drawExplostions(canvas);

  • \$\begingroup\$ Well, the first two lines in this answer is pretty helpful I guess, I can try that out for sure. The other stuff I already know since its pretty basic, I do draw and add objects like u stated. And I dont rotate the canvas at all in my MainView's onDraw(). The problem is adding the enemy at its center, since my rotate-function makes the rotation flip out, adding the explosion at the enemies center is not very hard, but it has to be the explosions center drawn at the enemies center. Anyway, thanks for the answer! \$\endgroup\$
    – Green_qaue
    Mar 28, 2013 at 17:38

You must log in to answer this question.

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