# Android bitmap placing

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.save();
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);
canvas.restore();
}


    if (enemies.get(i).getHP() <= 0) {  //enemy death


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

• did you try with canvas.drawBitmap(bitmap, x + bitmap.getWidth/2, y + bitmap.getHeight/2, null)? – VinceFR Mar 28 '13 at 11:04
• 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. – petervaz Mar 28 '13 at 13:47
• 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. – Green_qaue Mar 28 '13 at 17:13
• @VinceFR I got the same idea earlier today, I will try it as soon as I have the time :) – Green_qaue Mar 28 '13 at 17:15

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);

• thanks! this should do it, cant try it our for a few days but it looks right. :) – Green_qaue Mar 29 '13 at 23:08