underwater effect

I am developing my game (2d tile based) and now I want to create a level that the player stays underwater (like one of the levels of the old Super Mario). But I am not sure how can I reach this effect using tiled based. Any good ideas?
[249 byte] By [luisoft] at [2007-9-30 4:54:54]
# 1
Using Java2d? not easily.
Abuse at 2007-7-1 15:27:21 > top of Java-index,Other Topics,Java Game Development...
# 2
yeah, using java2d. I could not find anything related in the web... but what should be to the solution without java2d? why it will be difficult? I just want to simulate the player walking and swimming underwater, I do not want to get into real physics of it
luisoft at 2007-7-1 15:27:21 > top of Java-index,Other Topics,Java Game Development...
# 3

oh, I thought u meant the wavey water affect console platformers tend to have.

If that is what you want, then your kinda screwed. I believe consoles did it in hardware, to achieve the same affect in java2d you would have to do it through per-pixel rendering, which would prevent you taking advantaged of hardware accelerated blitting.

Abuse at 2007-7-1 15:27:21 > top of Java-index,Other Topics,Java Game Development...
# 4

It's pretty simple to get the water to wobble without messing about with things on the per-pixel level, and you *can* get hardware acceleration.

It's not a perfect solution but the effect is cool. All's I done was chop a loaded image up into 1 pixel length widths (so you're left with long thin image strips) then just wobble them in order, I suppose you could use a sine wave to control the wobble.

KarateMarc at 2007-7-1 15:27:21 > top of Java-index,Other Topics,Java Game Development...
# 5

spose that'd work.... I was thinking of a more complex wavey effect ^_^

Though in a tile based platformer, from a performance perspective, that will increase the number of blits by a factor of TILE_WIDTH.

Unless you introduce an intermediary VolatileImage, and apply the wave affect after the frame has been rendered. That would only increase the number of blits by SCREEN_WIDTH+1.

Abuse at 2007-7-1 15:27:21 > top of Java-index,Other Topics,Java Game Development...
# 6

>I was thinking of a more complex wavey effect ^_^

I could have guessed :)

As far as performance goes I think you could get away with just creating a small 64*64 block strip and applying the wobble on that, then tile that image, but you'd have to make sure the wave was even, else it would look awful. It's not ideal but I don't think it would be that much of a problem, and of course, this only reduces the wobble calculations which aren't very complicated anyway.

I've still got two horrible examples of this lying around on my comp, although these examples wobble a 320/240 image, and the fps didn't budge, and they weren't hardware accelerated. The things you do when bored :)

KarateMarc at 2007-7-1 15:27:21 > top of Java-index,Other Topics,Java Game Development...
# 7
oh right, you are proposing just wobbling the background tiles?I was thinkin of wobbling all the tiles, foreground, background, game sprites etc etc.As if you were playing the game through a fish tank :D
Abuse at 2007-7-1 15:27:21 > top of Java-index,Other Topics,Java Game Development...
# 8

I was thinking more along the lines of just wobbling an image that you could place on your map that represents water, I didn't read luisoft post properly :) For example; draw the background then the water then the platforms, making sure that the platform draws over the bottom of the wobble/water image as this will also be wobbling :)

Wobbling the whole tile set would be a pain to say the least, but it would look a bit poo if it was only the background that was wobbling and the collision tiles where still. But you could experiment with making 64*64 wobbles and tiling them, but you are going to be drawing a lot of images.

I suppose you could just draw your level as normal and wobble a transparent image with white lines over the top of the scene but that would look a bit cheap, but if hardware translucent image where always supported this could work quite well.

KarateMarc at 2007-7-1 15:27:21 > top of Java-index,Other Topics,Java Game Development...
# 9

lmao, just for some fun, I implemented it into the racing game im working on atm.

I now know why cars don't drive underwater ;)

The code is pretty simple actually, and the effect is..... interesting :D

1st off, create the displacement map for each column of the screen

The columns can be any width, mine are 1 pixel.

int [] disMap = new int[SCREEN_WIDTH];

for(int i = 0;i < SCREEN_WIDTH;i++)

{

disMap[i] = (int)(Math.sin((i*Math.PI*4)/SCREEN_WIDTH)*10);

}

int disStart = 0;//the offset into the displacement map, increment each game loop

Then create your VolatileImage to use as the intermediary buffer

VolatileImage waterBuffer = createVolatileImage(SCREEN_WIDTH,SCREEN_HEIGHT);

Now, modify your game code so instead of blitting directly to the BufferStrategys back buffer, blit to the waterBuffer instead.

//Graphics2D bg = (Graphics2D)bs.getDrawGraphics();

Graphics2D bg = waterBuffer.createGraphics();

Now, after all your render code, and before you call bufferStrategy.show(), add the code to blit the waterBuffer to the bufferStrategys back buffer. (applying the water affect)

Graphics g = bs.getDrawGraphics();

for(int i = 0;i < SCREEN_WIDTH;i++)

{

g.setClip(i,0,1,SCREEN_HEIGHT);

g.drawImage(waterBuffer,0,disMap[(i+disStart)%SCREEN_WIDTH],null);

}

disStart++;

bs.show();

Has no affect on fps at all either :)

Though im sure in a more taxing game it could cause problems (all that extra blitting is using alot of extra memory bandwidth on the graphics card)

And if it were running on a system that didn't implement VolatileImages in vram (think Mac :P), all the extra mem copies would absolutely kill the framerate.

Abuse at 2007-7-1 15:27:21 > top of Java-index,Other Topics,Java Game Development...
# 10

hmm, I just rethought that code a little, and dthe isplacement map doesn't need to be as wide as the screen. You just need to precompute a sufficiently large range to get a nice sine wave, and then index into the array using the columns x offset [modding it into range].

but hey, that code works, and it was just an experiment ;)

Abuse at 2007-7-1 15:27:21 > top of Java-index,Other Topics,Java Game Development...
# 11
Having said that, the displacment map size should be a factor of the screenwidth, otherwise your sine curve won't meet properly. So, a good displacement map size would be SCREEN_WIDTH/2.You would then get 2 full waves on the screen.
Abuse at 2007-7-1 15:27:21 > top of Java-index,Other Topics,Java Game Development...
# 12
Oh, and you will have to make your intermediary waterBuffer a little taller for it to not lose a portion of the screen when u distort it.If you make your wave have an amplitude of 10 pixels, you will need to make the waterBuffer SCREEN_HEIGHT+10*2 pixels high.
Abuse at 2007-7-1 15:27:21 > top of Java-index,Other Topics,Java Game Development...
# 13
hmmmm, a 4K water based shooter..... :D
Abuse at 2007-7-1 15:27:21 > top of Java-index,Other Topics,Java Game Development...
# 14

Hehe, nice one Abuse, the way as I was doing it was *very* simple, rather than use a sine wave I was just using a bit character jumping code to control the wave :), ugh, hey it was a 2minute bodge :)

A 4k water shooter sounds interesting, if you can pile in some translucency effects behind it as well, you could probably melt some heads :D

KarateMarc at 2007-7-1 15:27:21 > top of Java-index,Other Topics,Java Game Development...
# 15
thank you very much for the answers!I am playing now with a wobble algorithm, but I am having some problems... I am at work now... and I cant full test it... so when I get home tonight I will try your code Abuse, it seems very simple!
luisoft at 2007-7-1 15:27:23 > top of Java-index,Other Topics,Java Game Development...
# 16
Hey Abuse! From where the hell do you have such good ideas like that one?!?!! I could not resist and implement it one second ago... and it works!!!! and the effect is really cool!!!!! It was exactly the effect that I was planning to reach!!!!! Thank you man!!!!
luisoft at 2007-7-1 15:27:23 > top of Java-index,Other Topics,Java Game Development...
# 17

Mwhuhewowwhaa, I'm can't spell evil laughter.

Just thought, you could make this effect more interesting...

At them moment the wobble effect is only happening in the y-axis, so it's only fair that you should wobble it on the x-axis as well, create another buffer that wobbles on the x and blit your y-wobble to that.

Wobble-tastic :)

KarateMarc at 2007-7-1 15:27:23 > top of Java-index,Other Topics,Java Game Development...
# 18
I'm should read message before I'm post as well :D
KarateMarc at 2007-7-1 15:27:23 > top of Java-index,Other Topics,Java Game Development...
# 19
yeah, I thought about that too ;)Your post prompted me to try it out...and im happy to report it makes a realy good 'drunk' effect :)
Abuse at 2007-7-1 15:27:23 > top of Java-index,Other Topics,Java Game Development...
# 20

A really good drunk effect without the preceding head throb sounds useful :)

I had no idea the createGraphics method was that fast, I've only ever used that for pre-calculated stuff, I somehow got the idea that's all it's good for :/ lots of possibilities there though :)

If only the image methods for get pixel colour and set pixel colour where that nippy :D

KarateMarc at 2007-7-1 15:27:23 > top of Java-index,Other Topics,Java Game Development...