Fake 3d with only Java 2d

I’m trying to make an applet using only Java 2d
There’s this one tiny pseudo-element I want to implement
I want to be able to “stretch” a rectangle image in such a way that I can acheive an effect similar to this:

http://blog.muzy.com/wp-content/uploads/2010/05/tile-floor1.png

Now, the math of where the corners of the rectangle go is trivial. I know how to do that. But how can I acheive an effect like this with Java 2D, where I can manipulate each corner?
Is there even a way to do it? I couldn’t find one, but maybe I’m not searching for the right keywords.

Thanks

Couldn’t you just use lines to achieve the effect you are looking for, or is there a real reason it has to be rectangles?

Well, I have pretty floor tile images that I’d like to use

You could try this Graphics.drawPolygon(int[]. int[]. int)
Never mind I read “stretch a rectangle” instead of “stretch a rectangle image”. What about this instead drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)

That’s a useful function, but I’m not just trying to draw a polygon, I’m trying to draw a polygon with an image. Like the tiles on the floor in the SNES Mario Kart

The tiles on the ground are stretched images in the manner I’m trying to achieve

Ya, I misread your OP :persecutioncomplex: I edited my post when I saw that.

That method does allow me to stretch an image, but only as a rectangle with 90 degree angles, not a quadrilateral that can be used to obtain a 3d effect. It’s the difference between this

and this

What you’re looking for is a perspective transformation, and java2d is not going to give you one of those – it only has affine transforms. So you’re looking at doing it by hand, which I suspect is what they did for that SNES game there.

See this thread: http://www.java-gaming.org/index.php/topic,8803. It mentions JAI having perspective transforms, but JAI is nothing close to real time.

Ah well, I guess I’ll just stick with the solid color polygons. It’ll look good enough.

I haven’t used java2d alot, but would something like this be possible?

Create an Image object ‘ground’ and draw all the ground tiles on this Image.

Create a new Image object ‘groundOnScreen’ and individually map each pixel on ‘groundOnScreen’ to a pixel (or some value based on nearby pixels) on ‘ground’.

I made a quick illustration is paint:

http://www.oyvindbyhring.net/illustration.png

Just mindstorming here, I don’t know if you could actually do this with java2d :x

ah yes “mode 7”

would be really interested to see what you come up with

Oh cool … I also created exactly this many years ago (like 2006).

I pretty much gave it Nintendo GBA registers and a scanline “interrupt” so that you could quite easily put Mode 7 code into it with the same arithmetic as the GBA hardware, it’s tile mode too so if you’d like the code give me a shout and I’ll github it. here it is: https://github.com/keldon85-avasopht/stroll

Edit: Hmm, give me a minute, I’m just creating the repositories, etc. It comes with a demonstration of Tetris, which rotates the background mode 7 style.

p.s. it’s all software rendered.

By using the matrix described on this page
http://chrisjones.id.au/Ellipses/ellipse.html
you can project any image onto any rectangle in 3D space.
I used that technique once in a software renderer that I mentioned in this thread
http://www.java-gaming.org/topics/2d-rendering-but-with-a-3d-coordinate-system/26733/msg/237578/view.html#msg237578

I’d be easy just to do it in software. Each scanline is constant du & dv.

That’s exactly what my code above is. It’s a scanline renderer that imitates the GBA/SNES’s gfx hardware registers so that you can use mode7 code/calculations. The graphics API provides a HBL and VBL callback so that you can do all the types of tricks used for mode7 :slight_smile: Plus it comes with example code that demonstrates mode7 x-axis rotations.

Check it out. And if you want more info in the maths, check out this awesome Mode 7 tutorial.