Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (522) Games in Android Showcase (127) games submitted by our members Games in WIP (590) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Show Posts Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: Snake movement (smooth, not a grid) on: 2012-06-26 18:51:21
I think I got it!

I had misunderstood one of the examples I just googled, this is the final one which seems to work so far.. hopefully it will be of use to someone fighting with the same problem in the future

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24 `   public void move(float delta) {         SnakePart snakeHead = parts.get(0);      int snakeLength = parts.size() - 1;            snakeHead.setPosX((float)(snakeHead.getPosX() + Math.sin(Math.toRadians(snakeHead.getRotDegree())) * snakeHead.getSpeed() * delta));      snakeHead.setPosY((float)(snakeHead.getPosY() + Math.cos(Math.toRadians(snakeHead.getRotDegree())) * snakeHead.getSpeed() * delta));            for (int i = 1; i <= snakeLength; i++) {               SnakePart partBefore = parts.get(i-1);         SnakePart thisPart = parts.get(i);                  float xChange = partBefore.getPosX() - thisPart.getPosX();         float yChange = partBefore.getPosY() - thisPart.getPosY();                  float angle = (float)Math.atan2(yChange, xChange);                  thisPart.setPosX(partBefore.getPosX() - (float)Math.cos(angle) * 10);         thisPart.setPosY(partBefore.getPosY() - (float)Math.sin(angle) * 10);                        }         }`
2  Game Development / Newbie & Debugging Questions / Re: Snake movement (smooth, not a grid) on: 2012-06-26 18:39:18
I did some more googling and as a result, this is my current move method (for trying to do a "free roaming" snake).. it doesn't quite work but seeing what it does makes me think I'm on the right track at least.. if nothing else this will hopefully teach me some maths too..

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24 `   public void move(float delta) {         SnakePart snakeHead = parts.get(0);      int snakeLength = parts.size() - 1;            snakeHead.setPosX((float)(snakeHead.getPosX() - Math.sin(Math.toRadians(snakeHead.getRotDegree())) * snakeHead.getSpeed() * delta));      snakeHead.setPosY((float)(snakeHead.getPosY() + Math.cos(Math.toRadians(snakeHead.getRotDegree())) * snakeHead.getSpeed() * delta));            for (int i = 1; i <= snakeLength; i++) {               SnakePart partBefore = parts.get(i-1);         SnakePart thisPart = parts.get(i);                  float xChange = partBefore.getPosX() - thisPart.getPosX();         float yChange = partBefore.getPosY() - thisPart.getPosY();                  float angle = (float)Math.atan2(yChange, xChange);                  thisPart.setPosX(partBefore.getPosX() - (float)Math.cos(angle) * parts.size() - 1);         thisPart.setPosY(partBefore.getPosY() - (float)Math.sin(angle) * parts.size() - 1);                        }         }`
 3 Game Development / Newbie & Debugging Questions / Re: Snake movement (smooth, not a grid) on: 2012-06-26 17:37:09 Quote from: ra4king on 2012-06-26 17:27:47Can't you still use a grid but interpolate in between for movement?Hm. I'll have to give that a try for the 90 degree turns based movement, should be better than what I have now, thanks for the idea.
 4 Game Development / Newbie & Debugging Questions / Re: Snake movement (smooth, not a grid) on: 2012-06-26 17:18:06 Thanks for the reply, that looks like what I'd use to move the head but then how to figure out the correct position for the body parts following the head? I'd need to somehow calculate the correct position for each part based on the location and the rotation of the part before it
 5 Game Development / Newbie & Debugging Questions / Snake movement (smooth, not a grid) on: 2012-06-26 17:04:15 Hi!I'm trying to make some sort of a snake game, but I'm having trouble with the movement. I refuse to go the "make the snake move along a grid, placing each part of the snake to the cell the previous part occupied the last update" route as I think it looks stupid, so I started making a smooth moving snake but still with 90 degree turns. This is working OKish already after the very first try by having each part move independently of each other. Every time the player turns the head, it saves the coordinates and the new direction where the turn happened and each part checks these coordinates of the previous part and does the same change once it hits the said coordinates, again saving the coordinates for the next part to check them etc. Once the part has made the turn, I then fix it's position relative to the part before it to make it line up perfectly as there are some rounding errors etc. But there are some problems still plus you can just feel that the snake isn't connected but it's rather just several blocks moving next to each other (and when it bugs out the snake snaps in two).I have two different options here.. either I keep going like this and try to perfect the snake movement so that it a) looks good and b) has no chance of crapping out or I take it one step further and make a freeform moving snake with the head rotating in small degree changes and the body flowing freely (yet the way you'd expect it to do) behind it.The latter is what I'd like to do, but as I'm not too good at maths I'm having trouble figuring out how to calculate the positions of the body parts.I came across this which seems interesting to try but I couldn't figure out how to fully translate that into Javanese to see how it works. Again, sucking at maths doesn't help as I can't figure out the corresponding Java tools to use (namely the Vector2.Transform and Matrix.CreateRotationZ in that code (can I use affine transform here somehow?). I'm doing this on libgdx if there are some tools in their built in vector classes and whatnot that I should use.Any input and help would be appreciated to get me going
 6 Games Center / Archived Projects / Re: TaivasHippa, my first finished Java game project on: 2012-05-06 14:41:32 I uploaded the final 1.0 version.. this thing is done (unless I come up with something fancy and quick to implement tonight still as I have a demo session with the game at the uni tomorrow).I tweaked the plane movement a bit, it's not as quick to accelerate and slow down now. At the same time I added a slight increase to these values to the speed up bonus item (previously it only affected your maximum speed and rotation speed).I also made it so that the explosion animation doesn't cut off prematurely any more when an enemy plane dies.Last, the high scores list is now actually populated with scores for the player to beat.Have fun, thanks for the comments and suggestions and I'll see you when version 2 implemented with libgdx is done
 7 Games Center / Archived Projects / Re: TaivasHippa, my first finished Java game project on: 2012-05-06 08:28:33 The problem with the explosion animation is that because of how I coded it, it doesn't get to play fully before the object gets nulled when it's an enemy plane dying.. you can see the full animation when the player plane gets destroyed. It's still plain though (it's from SpriteLib GPL like the rest of the graphics).
 8 Games Center / Archived Projects / Re: DigiLife - Digital 2D Sim on: 2012-05-05 06:34:12 Cool, the idea (2D sandbox GTA style thing) is what my dream future project is but I'm not anywhere near pulling something like that off myself.Looking forward to something playable to try out.
 9 Games Center / Archived Projects / Re: TaivasHippa, my first finished Java game project on: 2012-05-05 05:17:39 Yep I thought about doing that for collisions, but the game was already starting to feel sluggish at times plus the deadline to get it done complete with full design documents etc etc is approaching fast so I made the choice to go with what I had initially managed to do. Definitely something I'd like to look at though, another item on the to-do list for the next version. Unless libgdx has some better collision detection stuff built in, haven't really looked into it that much yet.TaivasHippa is Finnish for "sky tag".. took me all of 45 seconds to come up with that one
 10 Games Center / Archived Projects / Re: TaivasHippa, my first finished Java game project on: 2012-05-04 18:48:00 Thanks for the comments, I do have a timer there trying to limit the FPS but it was scraped together from example and tutorials and might not work correctly.. I'll have to take a look at that.Plane handling and other such tweaks are probably the only things I'll have time to go over any more as I'm about to turn the project over for evaluation and I do agree the plane slows down too fast.. I'll probably change that. It will make the game a tad harder as well as then you can't prevent collisions as easily at the last moment.Collision detection is a problem, I used to have a debug mode switch on the menu that turned on visual bounding boxes so you could see when you're gonna collide but it's disabled now. There's an ellipse on the planes and collision is checked using those, makes it very inaccurate on the backsides of the planes.It's all pure native Java at the moment as I wanted to learn as much as possible by doing everything myself.
 11 Games Center / Archived Projects / TaivasHippa, my first finished Java game project on: 2012-05-04 18:04:49 This is "TaivasHippa", a 2D top down plane shooter I made for a software development class in the university of Helsinki. I'm still a novice programmer but learning fast and I already shake my head at some of the code I've written for this game but there's really no time to go back a lot to revisit the code.I used SpriteLib GPL graphics in lack of better ones and sounds from freesound.org as credited in the game. The starting background is some random stock photo straight from Google image search and ran through some equally random Photoshop filters.I intend to keep developing the game after the class is done, but the game will get a different name (namely an English one), I will bump the version back to 0.5 and recode it all using libgdx and use it as my first Android project (as well as my first libgdx test).I know at least one annoying bug with the controls that I noticed on some (but not all!) Windows machines I've tested on but haven't really had the time to take a look at it and I will probably have to leave it to the next iteration of the game.Anyway, thought I'd let you guys take it for a spin as this is my first project that made it this far.Download a JAR file (used to have a web start link but I broke it and can't be bothered to fix it for now).
 12 Games Center / Featured Games / Re: Daedalus on: 2012-04-28 16:01:13 Quote from: deathpat on 2012-04-28 11:17:36Thanks for the feedback, you're actually the first one to test on Mac This error is quite strange, it means that LWJGL is not able to determine the refresh rate of the current display mode ... anyway, I made a fix so that it will use a default value in this case. I uploaded new jars so if you could test again it would be great ! ThanksYep, now it works. Looking good!
13  Games Center / Featured Games / Re: Daedalus on: 2012-04-28 06:12:50
Tried the Mac version, got a black full screen then it quits with this in console:

 1  2  3  4  5  6 `28/04/2012 09:10:16.700 [0x0-0x31031].com.apple.JarLauncher: java.lang.ArithmeticException: / by zero28/04/2012 09:10:16.700 [0x0-0x31031].com.apple.JarLauncher:    at com.deathpat.shoot.engine.time.TimeManager.init(TimeManager.java:138)28/04/2012 09:10:16.700 [0x0-0x31031].com.apple.JarLauncher:    at com.deathpat.shoot.engine.Engine.start(Engine.java:170)28/04/2012 09:10:16.700 [0x0-0x31031].com.apple.JarLauncher:    at com.deathpat.shoot.Daedalus.main(Daedalus.java:26)28/04/2012 09:10:17.064 [0x0-0x31031].com.apple.JarLauncher: SoundSystem shutting down...28/04/2012 09:10:17.297 [0x0-0x31031].com.apple.JarLauncher:     Author: Paul Lamb, www.paulscode.com`
 14 Game Development / Newbie & Debugging Questions / Re: "combining" a new BufferedImage (SOLVED with a better way) on: 2012-04-14 15:33:55 Hm so I see, I'll look into that. Thanks
15  Game Development / Newbie & Debugging Questions / Re: "combining" a new BufferedImage on: 2012-04-14 12:36:47
You only need two drawImage(..) calls.  Firstly draw your health bar background as you are doing, then use the scaling version of drawImage to draw your gradient on top.  You only need to have a single pixel wide gradient image too, and you can use the lowest quality scaling setting (nearest neighbour) as it won't make a difference to the output.

Ah nice, didn't even realize it can be used like that, thanks!

I reduced the gradient pic to 1px wide and this is working (getHpLeft() returns the HP percentage as an int, i.e. 80 for 80% and the bar is 200px)

 1  2 `g.drawImage(gameResources.getHpBarSprite(), 10, 10, null);         g.drawImage(gameResources.getHpPortionSprite(), 12, 12, playerPlane.getHpLeft() * 2, 10, null);`

16  Game Development / Newbie & Debugging Questions / "combining" a new BufferedImage (SOLVED with a better way) on: 2012-04-14 10:22:04
Hi,

I'm making a health bar for my little game and because I wanted something nicer looking, instead of simply using fillrect I have an empty framed health bar as a BufferedImage and I draw small pieces of gradiented bar (another BufferedImage) inside the empty bar frames. The amount of bar pieces needed to draw depends on how much HP the player has left, so the maximum amount of pieces to draw is 100.

So every game loop update, it checks the player's HP and then goes through a loop to draw as many tiny pieces of a health bar as is needed. I'm thinking this can't be efficient and is at least one of the reasons my screen update is getting jerky at times.

What I'd like to do instead, is go through that loop to build a health bar according to the actual hp, then save the result as a new BufferedImage and draw that one on the screen instead. But I can't figure out how to do this.

Instead of drawing the small pieces over and over again, I would then in the game loop only draw the previously generated BufferedImage and generate a new one only if the player's HP has actually changed since the last one was generated.

This make sense? How would I generate a new BufferedImage for a bar which is already filled out by the correct amount?

Here's the current loop to draw the health bar for reference. This can't be efficient. getHpBarSprite() and getHpPortionSprite() return BufferedImages that have been loaded from PNG's. getHpLeft() returns the percentage of HP left.

The free horizontal space inside the hp bar sprite is 200 pixels and the hp portion sprite is 2 pixels wide (not 100 pixels and 1 pixel because then the bar would've been too small to my taste..)

 1  2  3 `g.drawImage(gameResources.getHpBarSprite(), 10, 10, null);for (int x = 0; x < playerPlane.getHpLeft(); x++)    g.drawImage(gameResources.getHpPortionSprite(), 12+x*2, 12, null);`

This is what it looks like at 20% HP left. It does exactly what I want, but I want to do it in a better way so that I wouldn't have to go through that stressful drawing loop every single game loop iteration..

I'm just now starting to build the actual HUD starting with the HP bar so don't mind those messy ammo/lives etc texts visible in the pic..

17  Game Development / Newbie & Debugging Questions / Re: affine transformation, rotate, area for collision detection.. on: 2011-04-13 14:37:00
Right on, thanks. Seems the result of clone() needs to be cast as an Area (returned Object otherwise) but otherwise it works just like that, small modifications and it's another method for my car class:

 1  2  3  4  5  6  7 `   // collides with another car?   public boolean collidesWith(Car otherCar) {      Area check = (Area)this.getArea().clone();      Area otherArea = otherCar.getArea();      check.intersect(otherArea);      return(!check.isEmpty());   }`

 18 Game Development / Newbie & Debugging Questions / Re: affine transformation, rotate, area for collision detection.. on: 2011-04-13 08:05:34 Hrm ok so contrary to what I thought (without reading enough about it beforehand) it would appear you can't simply do "if(oneAreaObject.intersects(anotherAreaObject))" ...  back to the drawing board
 19 Game Development / Newbie & Debugging Questions / Re: affine transformation, rotate, area for collision detection.. on: 2011-04-13 05:40:57 Aw no, it was kind of meant as a reply to you - I already separated the two, as I want the area "detection" (for a lack of a better word) to be separate from the drawing anyway. I guess I could create the AT elsewhere and use it in both of the methods though, would at least save me one very frequently used "new AT" call bu I don't think efficiency will play too big of a role in a game like this (although I have big plans and a vivid imagination but I'm sure my programming skills will be a huge bottleneck in what I can actually do)
 20 Game Development / Newbie & Debugging Questions / Re: affine transformation, rotate, area for collision detection.. on: 2011-04-13 03:25:18 Yeah, that crossed my mind too when I realized I now have these hard coded values in two separate places in the code - I shrugged it off with "I'll just make two more integer variables representing the rotation point for the car and these methods can then use those variables", I'll just have to remember to actually do it (takes like 2 minutes but I didn't get around to it yet)
21  Game Development / Newbie & Debugging Questions / Re: affine transformation, rotate, area for collision detection.. on: 2011-04-13 02:57:56
I separated it from the draw method totally so that I can just call getArea() when needed (i.e. when I check for collisions)

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19 `   // get the area   public Area getArea() {      // create a new area from the sprite      this.carArea = new Area(this.getBounds());            AffineTransform atArea = new AffineTransform();      // rotate the area      atArea.rotate(         Math.toRadians((int)this.getRotation()),         (int)this.getX() + (this.getWidth() / 2),         (int)this.getY() + (this.getHeight() - this.getHeight() / 4)      );            // apply the transformation to the area      this.carArea.transform(atArea);      return carArea;   }`

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24 `   // draw onto the Graphics2D object   public void draw(Graphics2D g) {                  // transformation, set the coords      AffineTransform atCar = new AffineTransform();      atCar.translate((int)this.getX(), (int)this.getY());            // rotate the sprite around the middle of the rear axel      atCar.rotate(         Math.toRadians((int)this.getRotation()),         this.getWidth() / 2,         this.getHeight() - (this.getHeight() / 4)      );            // draw the readily rotated sprite            g.drawImage(this.sprite, atCar, null);      // visualize the Area      if (debugging) {         g.setColor(Color.red);         g.draw(this.getArea());      }         }`

 22 Game Development / Newbie & Debugging Questions / Re: affine transformation, rotate, area for collision detection.. on: 2011-04-12 20:18:32 my very first "AI" cars re-enabled again, the area stuff does seem to work indeed http://www.youtube.com/watch?v=ElnEpBAsUAcnext up, use it for collision detection (hm I'm going to want to detect in what angle and what point the collisions happens as well), and when that's done my next bigger goal I think would be a scrolling game world. Having fun so far
23  Game Development / Newbie & Debugging Questions / Re: affine transformation, rotate, area for collision detection.. on: 2011-04-12 20:03:56
I think I got it this seems to work:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36 `   // draw onto the Graphics2D object   public void draw(Graphics2D g) {            // create a new area from the sprite      carArea = new Area(this.getBounds());            // transformations, set the coords for the sprite ("car") AT      AffineTransform atCar = new AffineTransform();      AffineTransform atArea = new AffineTransform();      atCar.translate((int)this.getX(), (int)this.getY());            // rotate the sprite around the middle of the rear axel      atCar.rotate(         Math.toRadians((int)this.getRotation()),         this.getWidth() / 2,         this.getHeight() - (this.getHeight() / 4)      );      // rotate the area      atArea.rotate(         Math.toRadians((int)this.getRotation()),         (int)this.getX() + (this.getWidth() / 2),         (int)this.getY() + (this.getHeight() - this.getHeight() / 4)      );            // apply the transformation to the area      carArea.transform(atArea);            // draw the readily rotated sprite            g.drawImage(this.sprite, atCar, null);      // visualize the Area      g.setColor(Color.red);      g.draw(carArea);         }`

Now I'll have to figure out how to implement it wisely so it's easy to use it for collision detection etc, but now that it's at least working it will be easy.

Thanks for nudging me to the correct direction
 24 Game Development / Newbie & Debugging Questions / Re: affine transformation, rotate, area for collision detection.. on: 2011-04-12 19:49:18 Hm yes it's something to that effect - commenting out the translate line, the area is initially drawn in the correct place (the sprite however is not) and it moves correctly for as long as I don't rotate, but rotating makes it go haywire.So, I guess easiest/best would be to separate the sprite and area transformations and figure out the correct translation / rotation for the area so that it behaves correctly..? Then to figure out what's going wrong with the rotation. The getbounds as far as I can tell returns a rectangle with the x and y coordinates (on the whole "play area") of the sprite's top left corner pixel, and it's width and height in pixels.
 25 Game Development / Newbie & Debugging Questions / Re: affine transformation, rotate, area for collision detection.. on: 2011-04-12 18:51:40 the coordinates are all out of whack.. not the best quality video but take a look: http://www.youtube.com/watch?v=OsYdl_57Q4k
26  Game Development / Newbie & Debugging Questions / affine transformation, rotate, area for collision detection.. on: 2011-04-12 17:39:05
Hi!

I'm making a a top down car game (how original huh?) to teach myself some Java and game programming.. it's going ok so far (in fact, I've already made a lot more I ever thought I could pull off) but I'm now facing my biggest problem so far: collision detection.

I'm trying to use an Area made from the bounds of my car sprite, rotate it the same way as my sprite and move them together and then I would use the Area to check for collisions. My problem I guess is that I don't really understand affine transformations, I originally just rotated the Image holding my sprite but as I started to realize I might have to go for Areas to get the collision detection working, I switched into using an AffineTransform to rotate my car.

I got it rotating the sprite correctly into the correct coordinates by trial and error but for some reason it's not working for the Area - the Area rotates but the coordinates are no where near the car itself.

Here's the code for my car's draw method, I put the area code there as I'm already messing around with affinetransformations. If you forget about all the Area stuff, it works the way it should (i.e. it rotates the car correctly and draws it to the correct location). But the area gets drawn in funny places.. the fact that I don't understand my affine transformation code aside, I further don't understand why it works for the sprite but not for the area..

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28 `   // draw onto the Graphics2D object   public void draw(Graphics2D g) {            // create a new area from the sprite      carArea = new Area(getBounds());            // transformation, get the coords      AffineTransform atCar = new AffineTransform();      atCar.translate((int)this.getX(), (int)this.getY());            // rotate the sprite around the middle of the rear axel      atCar.rotate(         Math.toRadians((int)this.getRotation()),         this.getWidth() / 2,         this.getHeight() - (this.getHeight() / 4)      );      // apply the transformation to the area      carArea.transform(atCar);            // draw the readily rotated sprite            g.drawImage(this.sprite, atCar, null);      // visualize the Area      g.setColor(Color.red);      g.draw(carArea);         }`

I don't understand the translate part, but without that the sprite would be in funny places as well..

my getBounds
 1  2  3  4 `   // get the car bounds   public Rectangle getBounds() {      return new Rectangle((int)getX(), (int)getY(), getWidth(), getHeight());   }`

anything else from my crappy code you need to see, ask

 Pages: [1]
 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 trollwarrior1 (29 views) 2014-11-22 12:13:56 xFryIx (71 views) 2014-11-13 12:34:49 digdugdiggy (50 views) 2014-11-12 21:11:50 digdugdiggy (44 views) 2014-11-12 21:10:15 digdugdiggy (38 views) 2014-11-12 21:09:33 kovacsa (62 views) 2014-11-07 19:57:14 TehJavaDev (67 views) 2014-11-03 22:04:50 BurntPizza (64 views) 2014-11-03 18:54:52 moogie (80 views) 2014-11-03 06:22:04 CopyableCougar4 (80 views) 2014-11-01 23:36:41
 theagentd 28x basil_ 27x HeroesGraveDev 23x BurntPizza 21x Riven 19x Spasi 18x princec 17x KevinWorkman 13x kpars 11x SHC 11x gouessej 10x Grunnt 10x Nate 9x Longarmx 9x LiquidNitrogen 8x CopyableCougar4 8x
 Understanding 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:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06
 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