Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Ways to make a good tower defense tower  (Read 443 times)
0 Members and 1 Guest are viewing this topic.
Offline Slyth2727

Senior Member


Projects: 2



« Posted 2013-01-17 03:58:51 »

I have been developing a simple tower defense game in plain java for the past month. I want to make my game a little better so I am adding a cannon, which needs to be able to rotate according to the enemy's position on the screen. Thats my big problem... I have made 8 images of the tower in different positions and I am not sure whether or not I can actually use Java to rotate a single image, or do I have to have a sprite sheet of the tower in different directions? Also, how do I detect which way for it to face? I was thinking about comparing the slope of the x and y coordinates of the enemy and the tower, but I wasnt sure if that was efficient, so I came here. Help is appreciated
cMp
Offline HeroesGraveDev

JGO Wizard


Medals: 64
Projects: 8


Muahahahahahaha...


« Reply #1 - Posted 2013-01-17 04:02:22 »

Yes, you can use java to rotate the image.

Use Google.

Offline Agro
« Reply #2 - Posted 2013-01-17 04:03:54 »

Yeah, for this one you're going to have to use trigonometry. More specifically, the atan2 function.

So, what you need to do is get the closest enemy, and calculate the angle to it. To do this, do:

1  
double angle = Math.atan2(target.x - this.x, target.y, - this.y);


That will be in radians, of course, so put a Math.toDegrees(x) around that for degrees.

Now, it'll be easier to do in degrees, so you should convert to that. We have to divide 360 into 8 parts, which is 45. So what you're going to do is something like this

1  
2  
3  
4  
5  
if(angle >= 0 && angle < 45) {
    sprite is this
} else if(angle >= 45 && angle < 90) {
    sprite is that
}


But then again, it all matters on perspective, this way would be useful in basically all possible perspectives, but your game has a bird-eye view, it would be easier to rotate the image.

Games published by our own members! Check 'em out!
Try the Free Demo of Revenge of the Titans
Offline Slyth2727

Senior Member


Projects: 2



« Reply #3 - Posted 2013-01-17 04:06:54 »

yes, it has a birds eye view, so I am guessing that I would use the same concept but for rotation?
Offline HeroesGraveDev

JGO Wizard


Medals: 64
Projects: 8


Muahahahahahaha...


« Reply #4 - Posted 2013-01-17 04:09:28 »

You can use graphics.rotate() or glRotatef() (depending on your rendering) to rotate the image.

Offline Jimmt
« Reply #5 - Posted 2013-01-17 04:16:39 »

He said "plain java" which I assume means Java2D...in that case better to use AffineTransform, then you can rotate individual images as opposed to the entire graphics object
Offline Slyth2727

Senior Member


Projects: 2



« Reply #6 - Posted 2013-01-17 04:18:47 »

I will research both of the methods. Thank you very much for all of your help!
Offline Slyth2727

Senior Member


Projects: 2



« Reply #7 - Posted 2013-01-17 04:19:34 »

Bingo. Exactly what I was searching for. Agro, I swear you are a super human genius your awesome!
Offline Agro
« Reply #8 - Posted 2013-01-17 04:37:22 »

Nah, its just that just 2 days ago, we learned what sin, cos, and tan were, so I got a much better understanding of it than I had before. I also worked on an isometric tower defense game before this and I had your same problems, and I just give you the solutions I got, but a bit different since I had isometric.

Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (76 views)
2013-05-17 21:29:12

alaslipknot (89 views)
2013-05-16 21:24:48

gouessej (119 views)
2013-05-16 00:53:38

gouessej (113 views)
2013-05-16 00:17:58

theagentd (125 views)
2013-05-15 15:01:13

theagentd (112 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (178 views)
2013-05-12 17:10:36

kutucuk (178 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.136 seconds with 20 queries.