Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

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!

share|improve this question
    
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. –  Max 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 :) –  Max Mar 28 '13 at 17:15

2 Answers 2

up vote 2 down vote accepted

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);
share|improve this answer
    
thanks! this should do it, cant try it our for a few days but it looks right. :) –  Max Mar 29 '13 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);

share|improve this answer
    
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! –  Max Mar 28 '13 at 17:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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