Asset - Extension Water Physics - GM Studio 2 and 1

O

Oko Doko

Guest
HI,

just bought your great asset, but I am bit confused how to use it, I would need to use it with blocks (objects) created with room editor is this possible? If not what would be easiest way as I cannot open/understand your world example files.

Thanks in advance again, GREAT ASSET!!
 

Dragon47

Member
Thanks :)

You need to pass in collision data for the water as a DS grid. You can read about DS grids in the manual. If you're using objects as blocks, you can go through all your block instances and organize them into a DS grid. You can put for example 1 in the DS grid cells where there are blocks, and 0 where there are no blocks. The water will collide with grid cells that have a value that is not 0.

The world example files are DS grid strings (obtained with ds_grid_write). It's not supposed to be human readable, it's just a string version of a DS grid.
 
O

Oko Doko

Guest
Thanks got it running well. Is there a chance to make something like a ship swim on the surface or drawing wheels half in water to make water splashing like a watermill?
 

Dragon47

Member
Thanks got it running well. Is there a chance to make something like a ship swim on the surface or drawing wheels half in water to make water splashing like a watermill?
Yes. The ship would be similar to how I make the player in the example swim on the surface of water if you press W (you can use wp_rectangle_get_volume). Splashing can be done by adding velocity to a specific point in the water (wp_rectangle_set_velocity). This is also done in the example code, when the player falls and lands in water. Or you can create custom splash sprites where the impact happens if you want the look of the splash effect to be different.
 
B

Bulwarkene

Guest
This is a request, can you update text inputs for game-maker 2? The "#" key doesn't work, and I cant seem to fix it. I love it so much, please fix.
 

Dragon47

Member
This is a request, can you update text inputs for game-maker 2? The "#" key doesn't work, and I cant seem to fix it. I love it so much, please fix.
To fix the "#"-character issue yourself, you can do the following after importing the GM Studio 1 version into GM Studio 2:
1. Remove all string_hash_to_newline calls.
2. Remove all string_replace and string_replace_all calls that turn "#" into "\#".

Another bug you get from importing the GM Studio 1 version into GM Studio 2 is that linear interpolation filtering is disabled, making scaled and rotated text look strange. To fix this you can go to the draw GUI event of the text input object and put "var texfilter_previous = gpu_get_texfilter(); gpu_set_texfilter(true);" at the very top, and "gpu_set_texfilter(texfilter_previous);" at the very bottom.

I uploaded a GM Studio 2 version as a new marketplace asset with these fixes: https://marketplace.yoyogames.com/assets/5604/text-inputs-gm-studio-2
 
K

kogooma

Guest
Hello! I just bought water physics.Because I want to add it to my game.i read GUIDE.But it's too difficult for me.I do not speak English well, but most of it was understandable.In the rm_example, I want to delete out the background and blocks and gui. how i to do it?
 

Dragon47

Member
Hello! I just bought water physics.Because I want to add it to my game.i read GUIDE.But it's too difficult for me.I do not speak English well, but most of it was understandable.In the rm_example, I want to delete out the background and blocks and gui. how i to do it?
Hi, this is all part of the example implementation. You don't really need to use the example code, it's just there to show you how you can use the asset. But if you want to start with the example code and try to form it into your own game, I recommend starting in obj_example_main. That's where most of the control code is. Take a look at obj_example_main's draw event if you want to see how things are drawn and remove certain parts. The GUI objects draw themselves. If you want to remove these, you can do so directly from the room.
 
G

Guest User

Guest
So I bought this, but I have no idea of how to use it. I readed the guide and did what it said, but it doesn't work (I'm not using the example things). I'm probably doing everything wrong, so you'll have to help. : ) And I have never used ds grids before.
I have an object called control_water.
In the create event:
Code:
grid = ds_grid_create(room_width,room_height);
water_ = wp_rectangle_create(room_width,room_height,32,32,0,0,0,1,3,true,grid);
In the step event:
Code:
repeat(2) wp_rectangle_update_simulation(water_,grid);
wp_rectangle_set_volume(water_,mouse_x,mouse_y, 1, 1, 1, true);
In the draw event:
Code:
wp_rectangle_draw(water_,x,y,0,0,0,0,0,0,0,0);
But there is no water anywhere.
 

Dragon47

Member
So I bought this, but I have no idea of how to use it. I readed the guide and did what it said, but it doesn't work (I'm not using the example things). I'm probably doing everything wrong, so you'll have to help. : ) And I have never used ds grids before.
I have an object called control_water.
In the create event:
Code:
grid = ds_grid_create(room_width,room_height);
water_ = wp_rectangle_create(room_width,room_height,32,32,0,0,0,1,3,true,grid);
In the step event:
Code:
repeat(2) wp_rectangle_update_simulation(water_,grid);
wp_rectangle_set_volume(water_,mouse_x,mouse_y, 1, 1, 1, true);
In the draw event:
Code:
wp_rectangle_draw(water_,x,y,0,0,0,0,0,0,0,0);
But there is no water anywhere.
You should take a look at how the example does it and learn how DS grids work. I can see several things here that are wrong, such as using 0 and 0 for binding capacities in wp_rectangle_create, and using room_width and room_height as size for the DS grid. If you still have problems afterward, feel free to continue asking.
 
G

Guest User

Guest
I tryied to modify it and do it like in the example. I don't know if this is any better.
In the create event:
Code:
water_block_width = 32;
water_block_height = 32;
//Walls and grids
sf_walls[0] = -1;
sf_walls[1] = -1;
wall_width = 64;
wall_height = 64;
wall_grid_width = ceil(room_width/wall_width);
wall_grid_height = ceil(room_width/wall_height);
wall_grid = ds_grid_create(wall_grid_width,wall_grid_height);


water_ = wp_rectangle_create(ceil(room_width/water_block_width),ceil(room_height/water_block_height),water_block_width,water_block_height,0,0.7,0.7,1,3,false,wall_grid);
In the step event:
Code:
//Add water when pressing LMB
if mouse_check_button(mb_left) {
wp_rectangle_set_volume(water_,floor(mouse_x/water_block_width),floor(mouse_y/water_block_height),1,1,1,true);
}

// Update the physics simulation
repeat(2) wp_rectangle_update_simulation(water_,wall_grid);
In the draw event:
Code:
wp_rectangle_draw(water_,x,y,0,0,0,0,0,0,0,0);

//Create block surfaces
for (var layer = 0; layer < 2; ++layer) {
    if (!surface_exists(sf_walls[layer])) {
        sf_walls[layer] = surface_create(room_width,room_height);
        surface_set_target(sf_walls[layer]);
            draw_enable_alphablend(false);
                draw_clear_alpha(c_black, 0);
                var t_x, t_y, color, grid = wall_grid[layer];
                for (t_y = 0; t_y < wall_grid_height; ++t_y) {
                    for (t_x = 0; t_x < wall_grid_width; ++t_x) {
                        if (grid[# t_x, t_y] != 0) {
                            draw_sprite(sprite_wall,0,t_x * wall_width, t_y * wall_height);
                        }
                    }
                }
            draw_enable_alphablend(true);
        surface_reset_target();
    }
}

And it showed this error:
Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object control_water:

trying to index a variable which is not an array
 at gml_Object_control_water_DrawEvent_1 (line 10) -                 var t_x, t_y, color, grid = wall_grid[layer];
############################################################################################
What should I do?
 

Dragon47

Member
I tryied to modify it and do it like in the example. I don't know if this is any better.
In the create event:
Code:
water_block_width = 32;
water_block_height = 32;
//Walls and grids
sf_walls[0] = -1;
sf_walls[1] = -1;
wall_width = 64;
wall_height = 64;
wall_grid_width = ceil(room_width/wall_width);
wall_grid_height = ceil(room_width/wall_height);
wall_grid = ds_grid_create(wall_grid_width,wall_grid_height);


water_ = wp_rectangle_create(ceil(room_width/water_block_width),ceil(room_height/water_block_height),water_block_width,water_block_height,0,0.7,0.7,1,3,false,wall_grid);
In the step event:
Code:
//Add water when pressing LMB
if mouse_check_button(mb_left) {
wp_rectangle_set_volume(water_,floor(mouse_x/water_block_width),floor(mouse_y/water_block_height),1,1,1,true);
}

// Update the physics simulation
repeat(2) wp_rectangle_update_simulation(water_,wall_grid);
In the draw event:
Code:
wp_rectangle_draw(water_,x,y,0,0,0,0,0,0,0,0);

//Create block surfaces
for (var layer = 0; layer < 2; ++layer) {
    if (!surface_exists(sf_walls[layer])) {
        sf_walls[layer] = surface_create(room_width,room_height);
        surface_set_target(sf_walls[layer]);
            draw_enable_alphablend(false);
                draw_clear_alpha(c_black, 0);
                var t_x, t_y, color, grid = wall_grid[layer];
                for (t_y = 0; t_y < wall_grid_height; ++t_y) {
                    for (t_x = 0; t_x < wall_grid_width; ++t_x) {
                        if (grid[# t_x, t_y] != 0) {
                            draw_sprite(sprite_wall,0,t_x * wall_width, t_y * wall_height);
                        }
                    }
                }
            draw_enable_alphablend(true);
        surface_reset_target();
    }
}

And it showed this error:
Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object control_water:

trying to index a variable which is not an array
 at gml_Object_control_water_DrawEvent_1 (line 10) -                 var t_x, t_y, color, grid = wall_grid[layer];
############################################################################################
What should I do?
Remove the code below "//Create block surfaces". That's just for visualization of dirt and stone blocks etc.
 
G

Guest User

Guest
Remove the code below "//Create block surfaces".
I did that. There is no more error messages. But there isn't water. How to add the water to the room?
 

Dragon47

Member
I did that. There is no more error messages. But there isn't water. How to add the water to the room?
It seems like the water is just not being drawn, because of all those zeros in wp_rectangle_draw. Set the arguments to undefined, or omit them like this: "wp_rectangle_draw(water_, x, y);"
If it still doesn't work, try setting x and y to 0.
 
G

Guest User

Guest
I did something and now there is a lot of water, but it seems that it doesn't collide with the walls. So, how to do that collision?
water escape.png
 

Dragon47

Member
I did something and now there is a lot of water, but it seems that it doesn't collide with the walls. So, how to do that collision?
View attachment 14950
You have to enter values for the walls into the DS grid so the water knows what it should collide with. That's the purpose of the DS grid.
The top left cell in the DS grid corresponds to the top left wall in your world. The cell to the right of that one corresponds to the wall to the right, etc. A grid is all zeros by default. Zeros means no collision. If a cell has a value greater than 0, e.g. 1, the water will collide with it. So you have to go through all the walls and enter values greater than 0 for them into the DS grid. This can be done by doing something like:
Code:
with (obj_wall) ds_grid_set(other.wall_grid, floor(x / other.wall_width), floor(y / other.wall_height), 1);
 
G

Guest User

Guest
I added this:
Code:
with(wall) {
ds_grid_set(control_water.wall_grid,floor(x/control_water.wall_width),floor(y/control_water.wall_height),1);
}
Now there is some kind of colision, but it is in the wrong place, as you can see in the image:water escape2.png
 

Dragon47

Member
I added this:
Code:
with(wall) {
ds_grid_set(control_water.wall_grid,floor(x/control_water.wall_width),floor(y/control_water.wall_height),1);
}
Now there is some kind of colision, but it is in the wrong place, as you can see in the image:View attachment 14976
Try this instead:
Code:
with(wall) {
ds_grid_set(control_water.wall_grid,floor((x - control_water.x)/control_water.wall_width),floor((y - control_water.y)/control_water.wall_height),1);
}
It subtracts control_water's position so the cells get aligned to it.
 
G

Guest User

Guest
Try this instead:
Code:
with(wall) {
ds_grid_set(control_water.wall_grid,floor((x - control_water.x)/control_water.wall_width),floor((y - control_water.y)/control_water.wall_height),1);
}
It subtracts control_water's position so the cells get aligned to it.
Now the collisions are relative or something. One is perfect and all above are too up and all below are too down.

water escape3.png
 

Dragon47

Member
What that means?
It means that your blocks are aligned according to the room editor grid. 64x64 means the width and height of each cell in the grid is 64 pixels. None of them should be one pixel off compared to the others, they should all be a multiple of 64 pixels away from each other.

If you can't figure out what is the problem, could you add this to my game?
I'm sorry, I don't have time for that. The asset's example still works, so you can study it to find your mistake.
 
G

Guest User

Guest
It means that your blocks are aligned according to the room editor grid. 64x64 means the width and height of each cell in the grid is 64 pixels. None of them should be one pixel off compared to the others, they should all be a multiple of 64 pixels away from each other.
Ok, so how do I know how they are?
 

Dragon47

Member
Ok, so how do I know how they are?
If the room editor grid is set to 64x64 when you place down your object instances, they should automatically be aligned. To check if they're aligned, you can go through the instances' positions and check if x and y is a multiple of 64, but noone does this. You can read about the room editor grid in the manual if you don't know what it is.
 
G

Guest User

Guest
@Flyinglegend_Studio:
I believe the problem is in this line of code in your create event:
Which should be:
Code:
wall_grid_height = ceil(room_height/wall_height);
I changed that and then changed this:

with(wall) {
ds_grid_set(control_water.wall_grid,floor(x/control_water.wall_width),floor(y/control_water.wall_height),1);
}

back to this:

with(wall) {
ds_grid_set(control_water.wall_grid,floor(x/control_water.wall_width),floor(y/control_water.wall_height),1);
}

And now it works perfectly. Thanks to both @kagamma and @Dragon47 for the help!
 
G

Guest User

Guest
Hi. I'm here again. Sorry to bother you again. I have some problems using wp_rectangle_update_blocks. Is it possible to update everything instead of just one point? Because in my game sometimes many things in different places change. Thanks in advance.
 

Dragon47

Member
Hi. I'm here again. Sorry to bother you again. I have some problems using wp_rectangle_update_blocks. Is it possible to update everything instead of just one point? Because in my game sometimes many things in different places change. Thanks in advance.
Yes, it's possible to specify a rectangular region. All of the scripts are documented, so open the script and you should be able to figure it out.
 
G

Guest User

Guest
Yes, it's possible to specify a rectangular region. All of the scripts are documented, so open the script and you should be able to figure it out.
I tried to do this when a wall gets destroyed:
Code:
wp_rectangle_update_blocks(water_,wall_grid,0,0,wall_grid_width,wall_grid_height);
But it didn't work.
 

Dragon47

Member
I tried to do this when a wall gets destroyed:
Code:
wp_rectangle_update_blocks(water_,wall_grid,0,0,wall_grid_width,wall_grid_height);
But it didn't work.
It looks correct. That will update all blocks in the grid though, so it might be a bit slow. Did you remember to set the cell in the grid back to 0 before calling the script?
 
G

Guest User

Guest
Did you remember to set the cell in the grid back to 0 before calling the script?
Oh. I didn't know I have to do something like that... : ) But I added now this to the walls destroy event:
Code:
ds_grid_set(control_water.wall_grid,x,y,0);
Is this right? Because it didn't work. But there are a lot of messages in the compile form. Like this:
Grid 0, index out of bounds writing [1472,2944] - size is [32,47]
 

kagamma

Member
Oh. I didn't know I have to do something like that... : ) But I added now this to the walls destroy event:
Code:
ds_grid_set(control_water.wall_grid,x,y,0);
Is this right? Because it didn't work. But there are a lot of messages in the compile form. Like this:
Grid 0, index out of bounds writing [1472,2944] - size is [32,47]
Code:
ds_grid_set(control_water.wall_grid, floor(x/control_water.wall_width), floor(y/control_water.wall_height), 0);
:)
 
G

Guest User

Guest
Hi again. When the walls destroy, all the water disappears. How to prevent that from happening?
 

Dragon47

Member
Hi again. When the walls destroy, all the water disappears. How to prevent that from happening?
That's strange. Seems like the surfaces get deleted for some reason. Do you have an old graphics card? Do you know how much VRAM is on it? Although not a completely satisfactory solution, you can try to call wp_rectangle_surface_to_buffer once or twice a second. This sets a backup which will be used whenever the water disappears from surface deletion.
 
G

Guest User

Guest
Do you have an old graphics card? Do you know how much VRAM is on it?
I don't know how much VRAM is on it, but it should be quite new and good.
I call this code every second, but it doesn't help, for some reason.
Code:
wp_rectangle_surface_to_buffer(water_);
 
G

Guest User

Guest
Wait. Now I removed that code and the water doesn't disappear anymore... ???????
EDIT: It's maybe because I did something with the scripts wp_rectangle_save_volume and wp_rectangle_load_volume.
EDIT 2: Still big part of the water disappears...
EDIT 3: Apparently, now the water that disappears appears in to the point where the wall is destroyed... o_O I don't know what I have done... The only thing that is different now is that there is a wp_rectangle_load_volume. But I don't even save the volume...:cool:
 
Last edited by a moderator:
G

Guest User

Guest
Wait again. I got it to work. What I had to do, is alarm event with wp_rectangle_save_volume, and when a wall gets destroyed, wp_rectangle_load_volume.
EDIT: When a wall gets destroyed, water appears there without any reason. Why?
 
Last edited by a moderator:

Dragon47

Member
Wait again. I got it to work. What I had to do, is alarm event with wp_rectangle_save_volume, and when a wall gets destroyed, wp_rectangle_load_volume.
EDIT: When a wall gets destroyed, water appears there without any reason. Why?
Glad you got it to work. However, that's kind of a slow solution as it accesses external memory. You should be able to make it work with wp_rectangle_surface_to_buffer.
In addition to updating blocks, wp_rectangle_update_blocks also adds inactive water behind blocks in order to make the outside water bind to the blocks. Otherwise you would see an unnatural curve next to the blocks. You can set the volume to 0 with wp_rectangle_set_volume after you destroy the wall.
 
G

Guest User

Guest
Glad you got it to work. However, that's kind of a slow solution as it accesses external memory. You should be able to make it work with wp_rectangle_surface_to_buffer.
In addition to updating blocks, wp_rectangle_update_blocks also adds inactive water behind blocks in order to make the outside water bind to the blocks. Otherwise you would see an unnatural curve next to the blocks. You can set the volume to 0 with wp_rectangle_set_volume after you destroy the wall.
Ok. Thank you for the help.
 

RendCycle

Member
Do you have something similar for an isometric type of map wherein a portion of the body of water can be affected by external factors like a Weather System? For example, when a storm cloud is nearby, those water tiles in a specified area are more wavy and more turbulent, etc. Another example is, let's say there is a Tornado, the target water tile forms a whirlpool under it.
 

Dragon47

Member
Do you have something similar for an isometric type of map wherein a portion of the body of water can be affected by external factors like a Weather System? For example, when a storm cloud is nearby, those water tiles in a specified area are more wavy and more turbulent, etc. Another example is, let's say there is a Tornado, the target water tile forms a whirlpool under it.
Sorry, but no, I haven't worked on an isometric system.
 

Cromagan

Member
Hello, I am interresetet in your Asset.
I tryed your Block Lightning 2d assest, but it crashes. So i am not sure, if the Water Physics will work.
The Problem ist, I will make a very very large world, with a 64x64px tileset. (Similar to the game Nom Nom Galaxy)
I get an error: "Atempting to create a texture of size blabla x blahbla in SetupD3DTexture, this is bigger than the maximum allowable dimension (16384)".

So my question: Will Water Physics work? (And also Fluid Dynamics)
 

Dragon47

Member
Hello, I am interresetet in your Asset.
I tryed your Block Lightning 2d assest, but it crashes. So i am not sure, if the Water Physics will work.
The Problem ist, I will make a very very large world, with a 64x64px tileset. (Similar to the game Nom Nom Galaxy)
I get an error: "Atempting to create a texture of size blabla x blahbla in SetupD3DTexture, this is bigger than the maximum allowable dimension (16384)".

So my question: Will Water Physics work? (And also Fluid Dynamics)
The error says you have a texture, likely the texture of a surface, with a dimension (width or height) greater than 16384 pixels. The problem is most likely because you have a very large world. The first thing you can do is to turn off surface optimization in the block lighting asset (look for the variable with a similar name to "surface optimization"). This will allow for 64x larger worlds as it no longer records a screenshot of the whole world, but only 1 pixel per block. It's still possible to use the assets with worlds exceeding this limit (even infinite worlds), but this requires more programming experience to implement, so it's easier to stay below the limit.
 
Compré el activo ayer, he estado intentando implementarlo en mi juego durante varias horas con la ayuda de la guía, pero ni siquiera puedo ver el agua, ¿no tienes ningún video sobre cómo implementarlo en cualquier juego? ¿Es solo la guía?
 
Top