[java] Java Star Map Help

Started by
13 comments, last by RobAU78 20 years, 5 months ago
Hey Tortoise,

Thanks again for your reply. Let''s say that I want to implement a system whereby the star image gets smaller as it moves away from the viewer (in terms of the virtual 3D space). Would it be faster to implement a transform or similar method on the BufferedImage than it would be to load multiple images at different times?

Also, I''ve been thinking about the best (i.e. fastest) way to make the stars respond to mouse clicks. Would loading the images onto JPanels be best, or is there a better alternative class?

Thanks,
Rob
Advertisement
Since you will most likely have many stars at the same "depth" it will proabaly be a good idea to have a static number of pre-rendered images of stars at various depths. A concept of a "star" can just be a point in 3D space, when it comes time to render that 3D point, you can use the Z-Axis as a lookup to a static array (or vector) of images that contains the prerendered image for that size. Instead of one star image that gets referenced and trasnformed all the time by each star, you have 100 or so images of stars of various sizes that get passed to a paint function all the time. Cuts out a transform step. You can use your own program to-transform and render the images in all the various depths when your game starts.

Transparent JPanles are probably a bad way to just grab clicks. You can use the canvas you are painting on the grab the clicks, then use the current X,Y position of the player (camera) to find out if the x,y position of the mouse click instersects a "star". If you have a star at grid position 5,5 and the player or camera is still at 0,0, then a mouse click on the canvas at 5,5 means it hit a star. A basic loop through your stars sorted via the Z-Axis should determine if a click hit a star.

The only problem with that is foreshortening with 3D. As your mouse click travels into the canvas through 3D "space" it will gradully travel inwards until it hits the exact center of the canvas. Does that make sense? If your game is not going to deal with foreshortening (i.e. you can only click on starts that are above a certain z-axis) then this should matter too much. Then again, maybe you don''t have to do this sort of manipulation on your mouse clicks. I''d have to see a concept screen shot of something...
Hey, Jim,

Have you ever seen/played the game Ascendancy? Its star map is pretty similar to the one I''d like to implement. Here''s a screen shot: http://www.logicfactory.com/games_ascendancy_screenshots.htm#.
Click on the picture at the bottom right.

- Rob
I think you''re going about this the wrong way. Forget about JPanels and all of that stuff.

Have one image buffer that you write to, and handle mouse clicks on your own. It will make your code faster, and more portable if you do it this way.

To handle the foreground-background star issues, keep two buffers. Your regular image buffer, and a Z-buffer. Whenever you draw a planet / star to the video buffer, update the z-buffer by storing the depth (z coordinate) for each pixel in your sprite.

As far as scaling/zooming your stars, you can write a fast function to do this for you. you know how large your sprite is for a star/planet, so you can calculate a step-rate for drawing the scaled version.

ie:
float stepRate = spritWidth / drawWidth;

float spriteX = 0;
for ( i = 0 to drawWidth)
{
color = sprite.getpixel ( (int)spriteX, (int)spriteY);
screen.setPixel (x,y, color);
spriteX += steprate;
}

There are other tricks you could use to speed this up even more.

To handle mouse clicks of your stars / spaceships/ planets, and whatnot, you have many options.

You could calculate bounding boxes for each star, and check which box the mouse is in, OR, you could create a third buffer (like your Z buffer) where you will store and object ID instead of a depth or a color.

Whenever the user clicks on your image, check your object Buffer at the mouse X,Y to see what object ID is underneath of it. This is probably the most accurate way of handling clicking, although it does suck up a lot of memory.

If I were you I would avoid using as many Java classes as possible-- write as much as you can on your own. The end result will most likely be much cleaner and way more flexible. Remember that every time you instantiate a new object you''re throwing away speed.

Hope that this has been helpful,
Will

------------------http://www.nentari.com
Hey Will,

Thanks for your reply. So how would I make these image buffers? Or would they just be BufferedImages? Should I make a Sprite class?

- Rob

This topic is closed to new replies.

Advertisement