some 2d scaling and mapping to relative co-ords

Im writing a chess game that is sort of 2d 3d… anyway, what’s the best way to determine where the piece is (in which grid square) as they aren’t really squares? im thinking of a work-around that uses the closest match, but there must be a better way?

also does anyone know of a better way to scale the images that won’t look so bad as these, yet still be full speed?

here’s a picture of the board:

http://www.users.bigpond.com/mattandlaura/JMChessPrev.jpg

and a jar you can use to see the scaling working. as you can see it’s not too good, particularly round the edges.

http://www.users.bigpond.com/mattandlaura/JMChess.jar

source:

http://www.users.bigpond.com/mattandlaura/ChessSrc.zip

I assume you are currently scaling the images with Java 2D. Have you tried playing around with the rendering hints before scaling?

You’ll also have to tell us what you mean by “full speed.” I assume you are only moving a single piece at a time, so you can probably get away with scaling on the fly to smoothly slide a piece up or down a column. That way you won’t need to pre-render a ton of images for all the possible sizes.

Really depends if you are using smooth sliding or if you just need to scale piece once at destination. If you are doing the later I would load in all pieces and save them as reference images and apply scaling to image after you move it and save the scaled version as a new image. That way you are only producing one extra image for each piece and won’t notice any speed hits.

For smooth sliding you can probably do it on the fly and save the new image after you arrive at your desitnation and since its only the one piece that you need to scale it shouldn’t hurt performace much.

For your polygon grids… you just need to create a chessboard class that holds all the positions and set it up so you know the center point for each grid (can be precomputed based on the plane/slant of your board). Then just create vectors for movement for a piece between the two board points and draw your piece movement along this vector.

Rendering hints? is this something you do with 3ds max? i haven’t really got much experience with 3ds.

i think i’ll use the scaling i have now and then once the user lets go of the piece i’ll have it do a decent looking scale using some other method though i dont know which, have to look up the api.

i really do not want to include more than one image of each piece. i always want my games to be no more than a meg (in jar form)

the chess board class is a good idea. so i’ll make it so that you pass in the co-ordinates and it returns the grid square in int[][] array or something or even better, the board class keeps track of which piece is on which square. take some of the complexity out of writing the rules…

but i have a friend who i want to play this game with online, the main reason im writing it actually, cause i showed him this chess board i had made by following a 3ds tutorial and we have been playing it sort of like email chess. a bit slow, so im going to make this multiplayer with no rules first… im planning on having it so that when the player clicks, it takes the location of the piece every 1/4 a second, and sends that info to the other player whose client moves the piece smoothly to match.

There is no reason why you have to have more then one image of each piece included with your game.

Simply create a new image after the player releases the piece based off a scaled version of your original piece. I’d keep 2 images of each piece in memory though so you are using the original piece as you create a new scaled image or your image will start looking pretty crappy after a few moves.

something like:
Image scaledPiece[piece] = graphicsConfig.createCompatableImage(scaledwidth, scaledHeight, Transparancy.OPAQUE); //or use translucent if you are have dirtect draw features enabled for alpha acceleration
Graphics2D g = (Graphics2D) scaledPiece[piece].getGraphics();

//Something like this, been awhile or scale with other drawing primitives
g.drawImage(originalPiece[piece], x,y, scaledWidth, scaledHeight, 0,0,originalwidth, originalHeight);

g.dispose();

your scaledPiece’s are what you always draw to the screen buffer and originalPieces are only used for scaling

[quote]Rendering hints? is this something you do with 3ds max? i haven’t really got much experience with 3ds.
[/quote]
Huh? Who mentioned anything about 3ds???

Check the JavaDocs. Graphics2D setRenderingHint

In particular RenderingHints.KEY_INTERPOLATION set to RenderingHints.VALUE_INTERPOLATION_BICUBIC

and RenderingHints.KEY_RENDERING set to RenderingHints.VALUE_RENDER_QUALITY

Excellent. i made a bufferedimage that i draw the board and pieces in high quality and if there’s a piece selected and the mouse is down, it draws it in ordinary quality straight onto the bufferstrategy.

it added a few new nice features that i was thinking of putting into the game anyway… the piece comes to the front if you hold your mouse down on it for a second (it would be faster for faster computers i’ve no doubt) so it’s like being able to check what piece is hidden behind another… now… gotta have it leave a ghost of the original piece behind when i move it… that would be really useful.

thanks for the rendering hints tip. that works excellent!

I was hoping to maintain as close a quality that 3ds would render, this does it.

anyway here’s a demo of the improved graphics. let me know what you think and if you have any tips on making the time it takes for the piece to be ‘movable’ after you press the mouse down, let me know.

the old file has been updated, executable JAR. just unzip it for the source as well.

http://www.users.bigpond.com/mattandlaura/JMChess.jar

edit: Just discovered a really bad bug, which is caused by me having the MouseListener call most of the render methods. clicking repeatedly on a piece will cause it to hang until it has re-rendered the board as many times as you clicked…