Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (467)
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  
  Rotation of translucent images (png)  (Read 2075 times)
0 Members and 1 Guest are viewing this topic.
Offline appel

JGO Wizard


Medals: 49
Projects: 4


I always win!


« Posted 2005-10-27 09:43:39 »

Ok, I have a few questions I'd like some comments on. I'm doing some things in my game I'm making that I don't think are very effecient, but I don't know of any other way (little experience), so any comments, hints and some help on how to do stuff RIGHT is appreciated Smiley

This is all Java2D, and my game is a 3rd-person-strategy-shooter, meaning that you pilot a small unit with your keyboard and mouse and blow up things Smiley


1) This tank you play, is composed of 2 PNG/TRANSLUCENT images. The "tank base" and the "tank tower". The tower *looks at* the position of the mouse-pointer. You can rotate the tank left/right using the A and D keys, while W moves you 3 up, S moves you 1 down (since it's slower to go in the back-gear).
QUESTION: Anyway, there is a lot of rotation here, rotating the tower and the base, according to how the tank is. Is this a very effecient way?


2) You can also shoot by clicking the mouse buttons. You shoot some sort of a missile or bullet, which is a PNG/TRANSLUCENT image also. Anyway, when you shoot the bullet needs to be rotated to match the direction. So, each time I fire I create a Sprite, and when that sprite draws the image it rotates it everytime. I notice that when I shoot alot of bullets, the game slows down a bit. But I need to do this because the bullet can be travelling toward any x,y point from the tank location, and the bullet needs to be rotated to match the direction.

QUESTION: I know this isn't very effecient, but I don't want some ugly bullets. I've tried scaling down the quality of the graphics, but any better way to do this?


3) In this game there is this Gun Tower, it shoots bullets (sprites) and it uses the same rotation for them as the tank does.
This gun tower composed of a TOWER and a BASE. The tower rotates on it's base, and that tower has a cannon that needs to *look at* the tank you're piloting. While this Gun Tower is idle (tank not in range) it rotates on it's base.




All this is related to the rotation of translucent png high quality png images.

My idea: I was thinking about having some sort of RotationSpriteCacher, to store the rotated images, so they don't need to be rotated again, but simply retrieved from the rotationSpriteCacher and then drawn.


Any thougs?

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline dranonymous

Junior Member




Hoping to become a Java Titan someday!


« Reply #1 - Posted 2005-10-27 17:23:07 »

Pre-rotating your images will certainly help.  Also, since you are using alpha you are going to take some slow down on the copying.

If you use transparent ie alpha 0 or 255, you will get some performance gain that way.  There used to be some sample code for a simple performance test of png with alpha on the forums here.  I didn't write it, but it was because of some similiar questions I had asked.  If I find it, I'll reply with a link to it.

HTH,
Dr. A>

PS - Do you have a sample app to demo?

PPS - Found a link
http://www.java-gaming.org/forums/index.php?topic=5407.0
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #2 - Posted 2005-11-01 02:46:58 »

http://www.gamelizard.com/game.php?game=robotank#game

Here's a game I made in the past which works like you're describing.  All of the images are translucent png's.  All of the images are drawn with the Graphics2D.drawImage(BufferedImage, AffineTransform) method.

Does this game run smoothly for you?  If so, perhaps there is something different in the code which makes things work a little nice.  I'd be happy to share with you the code for rendering a tank and its bullets so you can see how I did it.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline appel

JGO Wizard


Medals: 49
Projects: 4


I always win!


« Reply #3 - Posted 2005-11-01 05:25:27 »

http://www.gamelizard.com/game.php?game=robotank#game

Here's a game I made in the past which works like you're describing.  All of the images are translucent png's.  All of the images are drawn with the Graphics2D.drawImage(BufferedImage, AffineTransform) method.

Does this game run smoothly for you?  If so, perhaps there is something different in the code which makes things work a little nice.  I'd be happy to share with you the code for rendering a tank and its bullets so you can see how I did it.

Hey, thanks for that. Your game runs smoothly and it looks similar to mine, except that I have perhapse higher quality sprites/images and also stationary defensive structures.


There is one thing that I'm interested in though.

In my game I want to create maps that are maybe 2048x2048, or 4096x4096 in dimension.
My player (tank) should start at some given point on the map, let's say (2000,2000).
Then I have 2 enemy tanks and one enemy defense structure somewhere at position (400,400).

Appearently if I'm running the game in a 800x600 window, then my player (tank) should be positioned at point (400,300) that is in the middle. But then the enemy units and defense structure can't be located at point (400,400)  Huh

This is what's troubling me. How do you deal with this sort of problem? Do you have some sort of worldYOffset and worldXOffset ?


Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #4 - Posted 2005-11-01 06:24:19 »

appel, yeah, you'll have to translate everything that you render by the coordinates of the thing your camera is following. Btw, I'm presently writing a game in which the map size is several screen width on each side. I wrote a Viewport class and an interface, ViewportListener, in order to keep track of camera positions and have other panels correctly respond to camera location changes (or rather, not worry about them when it's not necessary). Of course your camera might be moving continuously, in that case it would make sense just to ask about the camera position every frame.
Offline MasterOfDisaster

Junior Member




O_o


« Reply #5 - Posted 2005-11-01 08:50:02 »

http://www.gamelizard.com/game.php?game=robotank#game

Here's a game I made in the past which works like you're describing.  All of the images are translucent png's.  All of the images are drawn with the Graphics2D.drawImage(BufferedImage, AffineTransform) method.

Does this game run smoothly for you?  If so, perhaps there is something different in the code which makes things work a little nice.  I'd be happy to share with you the code for rendering a tank and its bullets so you can see how I did it.

man, this runs perfectly smooth ... how do you do that?
when i use translucent images in my games the performance goes down dramatically. only transparent images work for me. did you pre-render the rotated tank  images?

Offline appel

JGO Wizard


Medals: 49
Projects: 4


I always win!


« Reply #6 - Posted 2005-11-01 09:25:13 »

appel, yeah, you'll have to translate everything that you render by the coordinates of the thing your camera is following. Btw, I'm presently writing a game in which the map size is several screen width on each side. I wrote a Viewport class and an interface, ViewportListener, in order to keep track of camera positions and have other panels correctly respond to camera location changes (or rather, not worry about them when it's not necessary). Of course your camera might be moving continuously, in that case it would make sense just to ask about the camera position every frame.

Interesting. You care to share it with me? Smiley I'd like to see what other people are doing, because this is currently what I've been working on. I've actually solved my problem, but personally I feel that my code is a bit of a hack.


Also, how do you solve the problem with the scrolling background image? For example, when you move your unit in any direction, then the background image should scroll in the opposite direction, making it appear that the unit is moving, but still it's stationary and everything else is moving Smiley

My solution was to create a background image that had extra external padding. So if my texture as 256x256, then the extra external padding was 256 pixels on all sides. So when I would move my unit upward, this external padding would be displayed...until it had reached the boundary, then the camera would be reset to it's default position on Y-axis (or X-axis if you're moving to left/right) and if the tank is still moving upward then this process would simply repeat itself, making it appear that the background is scrolling continiously.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #7 - Posted 2005-11-02 04:07:47 »

appel, I'll share the code at some time not too far in the future, but there are a few adjustments and inline commenting before we have our "prealpha 0.0001" in a state which doesn't contain too many half-implemented things  Smiley. I'd hate to share code containing known but un-documented errors so others make mistakes.
Offline m00wley

Senior Newbie





« Reply #8 - Posted 2005-11-02 13:01:58 »

Quote
Here's a game I made in the past which works like you're describing.  All of the images are translucent png's.  All of the images are drawn with the Graphics2D.drawImage(BufferedImage, AffineTransform) method.

Does this game run smoothly for you?  If so, perhaps there is something different in the code which makes things work a little nice.  I'd be happy to share with you the code for rendering a tank and its bullets so you can see how I did it.

That game was totally awesome. I can imagine how it will look in V1.0  Smiley

I also noticed how smooth your Loader worked. Is there any hints that you can give, of how to make a loader like that. I've fiddled with a loader but it's nothing like the one you developed.


// ----------------------------------------------------------------------<br />// 1 c0de, there for( I am...)<br />// ----------------------------------------------------------------------
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #9 - Posted 2005-11-02 16:14:02 »

loader loading images:
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  
/*****   Load Images   *****/      
int status = 0;
bottomBar.setTotal(totalImageSize);
String string = null;

for (int i = 0; i < imageStrings.length; i++) {
   string = imageStrings[i];

   topBar.setStatus(currentProgress);
   bottomBar.setStatus(status);
   bottomBar.setText(new String[] {"Loading Image: ", string.substring(string.lastIndexOf("/")+1)});
   
   refreshLoader();
     
   BufferedImage image = null;
   try {
       image = GLUIUtil.loadImage(getResourceAsStream(codeBase+string));
   }
   catch (Exception e) {
       throw new Exception("image: " + string + "\n" + e.getMessage());
   }
   
   loadedImages.put(string, image);
   
   status += imageSizes[i].intValue();
   currentProgress += imageSizes[i].intValue();
}
/*****   Done Loading Images   *****/


Move the screen (camera code)
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  
public void moveScreen() {
   double centerX    = screenX+SCREEN_WIDTH/2;
   double centerY    = screenY+SCREEN_HEIGHT/2;
   double heroX    = heroTank.xCoord();
   double heroY    = heroTank.yCoord();
   
   heroX -= heroTank.getDx()*8 - (double)SCREEN_WIDTH/4*Math.cos(heroTank.getBaseTheta());
   heroY -= heroTank.getDy()*8 - (double)SCREEN_HEIGHT/4*Math.sin(heroTank.getBaseTheta());
   
     
   if       (heroX > centerX)            screenX+=(heroX-centerX)*.25;
   else if (heroX < centerX)            screenX-=(centerX-heroX)*.25;
   if       (heroY > centerY)            screenY+=(heroY-centerY)*.25;
   else if (heroY < centerY)            screenY-=(centerY-heroY)*.25;
   
   
   if (Math.abs(heroX-centerX) < 1)      screenX = heroX-SCREEN_WIDTH/2;
   if (Math.abs(heroY-centerY) < 1)      screenY = heroY-SCREEN_HEIGHT/2;
     
   if       (screenX > mapWidth-SCREEN_WIDTH)   screenX = mapWidth-SCREEN_WIDTH;
   else if (screenX < 0)                  screenX = 0;
   if       (screenY > mapHeight-SCREEN_HEIGHT)   screenY = mapHeight-SCREEN_HEIGHT;
   else if (screenY < 0)                  screenY = 0;
   
   grassStartX = -(int)(screenX)%grassWidth;
   grassStartY = -(int)(screenY)%grassHeight;
}


important stuff in my render method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
moveScreen();
     
//Draw Grass
for (int i=grassStartX-grassWidth; i<SCREEN_WIDTH; i+=grassWidth)
   for (int j=grassStartY-grassHeight; j<SCREEN_WIDTH; j+=grassHeight)
      g2d.drawImage(grass, i, j, null);      

//Draw all tanks that are on the screen
heroTank.draw(g2d, screenX, screenY);
for (int i=0; i<esOnScreen.size(); i++)
   ((Tank)esOnScreen.get(i)).draw(g2d, screenX, screenY);


And the Tank render method:
1  
2  
3  
4  
5  
6  
7  
resetTrans(screenX, screenY);

for (int i=0; i<projectile.length; i++)
   if (projectile[i].isAlive())
      projectile[i].draw(g2d, screenX, screenY);
g2d.drawImage(RoboTank.getTankImage(color), trans, null);
g2d.drawImage(RoboTank.getTankImage(Constants.IMAGE_TURRET), tTrans, null);


resetTrans method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public void resetTrans(double screenX, double screenY) {
   
   if (hasCapability(Constants.TURRET_HOVER))
      turretTheta = (Math.atan2(y-screenY-mouseY, x-screenX-mouseX))+Math.PI;
   else
      turretTheta = baseTheta;
   
   trans.setTransform(scaleX, 0, 0, scaleY, x-screenX-desiredWidth/2, y-screenY-desiredHeight/2);
   tTrans.setTransform(scaleX, 0, 0, scaleY, x-screenX-desiredWidth/2, y-screenY-desiredHeight/2);
   
   trans.rotate(baseTheta, imageWidth/2, imageHeight/2);
   tTrans.rotate(turretTheta, imageWidth/2, imageHeight/2);
}


And the Projectile draw method is basically the same.  I hope that helps! Smiley

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline MasterOfDisaster

Junior Member




O_o


« Reply #10 - Posted 2005-11-02 21:10:21 »

i wonder why you can turn on antialiasing and use translucent images (had a look at your code after a bit of decompiling) without getting any performance problems.
when i do this in my games the framerate goes down dramatically. i noticed you set some system properties (sun.java2d.transaccel/opengl = true). is this the magic bit? i remember i tried these once but with no success...

Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #11 - Posted 2005-11-03 19:19:21 »

Perhaps it's a balance of other performance issues.  Maybe if you profiled your app you'd find other bottlenecks that you could work on and end up with performance like mine.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (81 views)
2014-04-15 18:08:23

BurntPizza (73 views)
2014-04-15 03:46:01

UprightPath (84 views)
2014-04-14 17:39:50

UprightPath (67 views)
2014-04-14 17:35:47

Porlus (84 views)
2014-04-14 15:48:38

tom_mai78101 (107 views)
2014-04-10 04:04:31

BurntPizza (167 views)
2014-04-08 23:06:04

tom_mai78101 (263 views)
2014-04-05 13:34:39

trollwarrior1 (214 views)
2014-04-04 12:06:45

CJLetsGame (223 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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
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!