Rotating a slew of moving enemies towards a player sprite

Hello :slight_smile:

I’m making a typical Zombie vs. Player Survival game. I have me an Enemy Class, and a Player class.

The Enemies are constantly adjusting their X and Y components of speed based on the Player’s location, to “home in” on the player.

I want the enemy (Zombie) To be facing the player, no matter where he moves, but I can’t seem to get both the Trig and the Programming down at the same time, and I’m getting incredibly spastic results.

So, does anyone have a way to rotate a bunch of zombies towards a moving player?

Well, depends on your zombie’s sprite size. If it’s quite big and there’re many of them then java2d may be out.

Have you tried to not using rotating method, but changing the sprite? should be faster but limited angle.

Hmmm… that was suggested to me by a friend… and really, the game looks very 8-bit, so limited rotation would fit in.

Would it be efficient?

Lol, this project is the most intensive on the computer I’ve ever done, so I really don’t know what my limits are.

Thanks :slight_smile:


rotation = Math.atan2(player.y - zombie.y, player.x - zombie.x);

Then draw it:


g.rotate(rotation,x + width/2, y + height/2);
//draw the image
g.setTransform(new AffineTransform()); // reset the transform

[quote]Would it be efficient?
[/quote]
if you seek or worry about performance, it’s a good price to pay :slight_smile: Further more on rotation you should watch out for origin’s coordinate and could be problem in simple collision check.