#### Archived

This topic is now archived and is closed to further replies.

# Angle of an enemy image depending on player position

This topic is 5414 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi I'm programming a tow-down shooter game (DirectX, 2D) and I want to add a new enemy, a stationary gun that fires missiles in the player's direction. Since bitmap rotation is too CPU intensive, I decided to 'simulate' rotation using 12 frames (0=facing east,1=facing southeast,2=facing a bit further southeast,3=facing south). Now my problem is, how do I get the right frame depending on the position of the player. I tried using trigonometry, finding the angle of the player to the cannon in an imagined triangle but that neither seemed very robust nor fast, I later got the following angles, depending on the players position: 90° 45° - 45° 180° (GUN) 0° 145° - 45° 90° (I tried it with ASCII art but that doesn't really work here, so try to imagine the situation ) So I'm not even sure if my formula was right. Can anybody help an oldschool 2D programmer solving this tricky problem? [edited by - Phillemann on January 18, 2004 1:51:41 PM]

##### Share on other sites
One has to be amazed at such statements... Demo's have had full screen rotation and scaling in software since the days of the 10 Mhz 386, and yet on a 2Ghz machine it's suddenly too cpu intensive. Makes you wonder...

Oh well.

There are numerous resources for bitmap rotation on Programmers Heaven and This probably being one of the best and easiest to implement.

If you decide to use the method you mentioned in your post, then it's simply a matter of finding out the angle of the turret by simple trigonometry. Then dividing the number of degrees with the number of separate images, to find wich image to display.

Hope that gets you going.

(note: if you feel like delving into software image rotation, keep the image you wish to rotate in system memory, because reading from video memory is incredibly slow.)

[edited by - Bad Maniac on January 19, 2004 8:50:23 AM]

##### Share on other sites
The *best* thing to do would have been to use 3D with an orthographic matrix, making it look 2D but allowing you to use full 3D hardware for transforms (i.e. rotations). Oh well.

What you''ve done is divided the gun''s attack zone into ''sectors.'' The first image faces east (call it 0 degrees) and the second faces ENE (30 degrees), so the first ''sector'' - angle that the first frame covers - is from +15 degrees to -15 degrees.

You need the distance along the x axis from the gun to the target, and the distance along the y axis. Both should keep their signs - i.e. 5 pixels above the gun is +5 Y, while 5 pixels below is -5 Y.

Then, tan(angle) = y/x. So, the angle you want is atan(y/x). You then determine which sector that angle is in, and use the associated frame.

Richard "Superpig" Fine
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
Enginuity1 | Enginuity2 | Enginuity3 | Enginuity4 | Enginuity5
ry. .ibu cy. .y''ybu. .abu ry. dy. "sy. .ubu py. .ebu ry. py. .ibu gy." fy. .ibu ny. .ebu
Preserve wildlife: pickle a squirrel today!

##### Share on other sites
quote:
One has to be amazed at such statements... Demo''s have had full screen rotation and scaling in software since the days of the 10 Mhz 386, and yet on a 2Ghz machine it''s suddenly too cpu intensive. Makes you wonder...

[edited by - Bad Maniac on January 19, 2004 8:50:23 AM]

This again shows my lack of experience in game programming
Yet thanks for the links, using bitmap rotation is way more attractive and easier to implement

@superpig: I decided to dvelve into 3D at a later point of time. 2D is a bit easier and I can see how a simple game is structured, basically so the step to 3D won''t be that large

##### Share on other sites
quote:
Original post by Phillemann
@superpig: I decided to dvelve into 3D at a later point of time. 2D is a bit easier and I can see how a simple game is structured, basically so the step to 3D won''t be that large

Eh, I still maintain that 3D is easier than most people make it out to be. Just go through the motions of setting up matrices and then rendering quads, ignoring the z-coordinate... heck, in OpenGL you don''t even need to specify it.

##### Share on other sites
Very good point, the only argument against doing this in OpenGL (which I have taken a look at some time ago) is that I''ve done pretty much of the game without 3D, I''ll finish this 2D game first and then move on to 2D stuff in a 3D world (maybe I''ll even port the game )...

##### Share on other sites
Actually you've got a good point superpig, 3D for this would be easier AND faster. But I didn't think of that when I posted, at least now Phillemann has the choice

[edited by - Bad Maniac on January 19, 2004 3:19:32 PM]

##### Share on other sites
Sure thing, it''s good practice anyway. Useful stuff to have done if you ever get into something like GBA coding.

1. 1
Rutin
31
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633313
• Total Posts
3011320
• ### Who's Online (See full list)

There are no registered users currently online

×