Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (582) Games in Android Showcase (163) games submitted by our members Games in WIP (632) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Ways to make a good tower defense tower  (Read 1348 times) 0 Members and 1 Guest are viewing this topic.
Slyth2727
 « Posted 2013-01-17 02: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

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
HeroesGraveDev

JGO Kernel

Medals: 359
Projects: 11
Exp: 3 years

┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻

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

Yes, you can use java to rotate the image.

Agro
 « Reply #2 - Posted 2013-01-17 03: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.

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

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

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
HeroesGraveDev

JGO Kernel

Medals: 359
Projects: 11
Exp: 3 years

┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻

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

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

Jimmt
« League of Dukes »

JGO Kernel

Medals: 161
Projects: 5
Exp: 3 years

 « Reply #5 - Posted 2013-01-17 03: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
Slyth2727
 « Reply #6 - Posted 2013-01-17 03:18:47 »

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

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Slyth2727
 « Reply #7 - Posted 2013-01-17 03:19:34 »

Bingo. Exactly what I was searching for. Agro, I swear you are a super human genius your awesome!

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Agro
 « Reply #8 - Posted 2013-01-17 03: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

You cannot reply to this message, because it is very, very old.

 theagentd (28 views) 2015-05-27 22:21:17 theagentd (34 views) 2015-05-27 22:21:08 CopyableCougar4 (24 views) 2015-05-27 19:24:50 MrMapcom (24 views) 2015-05-23 20:26:16 MrMapcom (32 views) 2015-05-23 20:23:34 Waterwolf (37 views) 2015-05-20 15:01:45 chrislo27 (44 views) 2015-05-20 03:42:21 BurntPizza (79 views) 2015-05-10 15:53:18 FrozenShade (64 views) 2015-05-07 09:11:21 TheLopais (228 views) 2015-05-06 13:36:48
 Rayvolution 24x Spasi 21x Riven 16x theagentd 15x Drenius 15x BurntPizza 15x ra4king 13x opiop65 12x EgonOlsen 12x princec 12x DavidBVal 11x Husk 11x orangepascal 11x KevinWorkman 10x scanevaro 8x kevglass 8x
 List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org