Skip to content

Commit

Permalink
Add fluid sample (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu authored Apr 16, 2024
1 parent ac3743e commit bff1f71
Show file tree
Hide file tree
Showing 26 changed files with 444 additions and 23 deletions.
1 change: 1 addition & 0 deletions .github/actions/upload/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ runs:
name: godot-rapier-2d-${{ inputs.precision }}-${{ inputs.features }}
path: |
${{ github.workspace }}/bin/**
!${{ github.workspace }}/bin/project.godot
retention-days: 14
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Ignore everything in bin folder by default
bin/addons/godot-rapier2d/bin/*
bin/.godot/**
bin/samples/godot-rapier2d/addons/**

!bin/addons/godot-rapier2d/bin/libphysics_server_rapier2d.macos.template_debug.framework/Resources/Info.plist
!bin/addons/godot-rapier2d/bin/libphysics_server_rapier2d.macos.template_release.framework/Resources/Info.plist
Expand Down
Binary file added Fluid2d.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ A 2d [rapier](https://github.com/dimforge/rapier) physics server for [Godot Engi

- Parallel support (for non enhnanced determinism builds)
- SIMD (Single instruction, multiple data) build.
- Cross-platform determinism build.
- Fluid2D node: Fluids and squishy bodies made of particles
- Better physics stability.
- Fluids with surface tension, viscousity and elastic liquids.

<p align="center">
<img src="rapier-vid.gif"/>
</p>

<p align="center">
<img src="Fluid2d.gif"/>
</p>

# Limitations

- SeparationRay2D missing [issues/5](https://github.com/appsinacup/godot-rapier-2d/issues/5)
Expand Down
3 changes: 3 additions & 0 deletions bin/addons/godot-rapier2d/Fluid2D.svg.import
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

importer="texture"
type="CompressedTexture2D"
uid="uid://cv17s0qjrqj16"
path="res://.godot/imported/Fluid2D.svg-947567c51a8b586a0e695be7cba2d975.ctex"
metadata={
"has_editor_variant": true,
"vram_texture": false
Expand All @@ -10,6 +12,7 @@ metadata={
[deps]

source_file="res://addons/godot-rapier2d/Fluid2D.svg"
dest_files=["res://.godot/imported/Fluid2D.svg-947567c51a8b586a0e695be7cba2d975.ctex"]

[params]

Expand Down
13 changes: 13 additions & 0 deletions bin/addons/godot-rapier2d/Fluid2DRenderer.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class_name Fluid2DRenderer
extends MultiMeshInstance2D

@onready var fluid :Fluid2D = get_parent()

func _process(delta):
var index = 0
multimesh.instance_count = fluid.points.size()
for point in fluid.points:
var new_transform: Transform2D = Transform2D()
new_transform.origin = point - position
multimesh.set_instance_transform_2d(index, new_transform)
index += 1
1 change: 1 addition & 0 deletions bin/addons/godot-rapier2d/Node2D.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions bin/addons/godot-rapier2d/Node2D.svg.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://bjnvmmcdjgbu6"
path="res://.godot/imported/Node2D.svg-839688c6fcfc535aaba08e1eeebc9ec8.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://addons/godot-rapier2d/Node2D.svg"
dest_files=["res://.godot/imported/Node2D.svg-839688c6fcfc535aaba08e1eeebc9ec8.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
svg/scale=1.0
editor/scale_with_editor_scale=false
editor/convert_colors_with_editor_theme=false
18 changes: 18 additions & 0 deletions bin/addons/godot-rapier2d/fluid_multi_mesh.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[gd_resource type="MultiMesh" load_steps=2 format=3 uid="uid://d0bhettx43xt1"]

[sub_resource type="ArrayMesh" id="ArrayMesh_gi4ki"]
_surfaces = [{
"2d": true,
"aabb": AABB(-16, -16, 0, 32, 32, 0),
"attribute_data": PackedByteArray(0, 0, 128, 63, 154, 153, 129, 62, 0, 0, 128, 63, 0, 0, 112, 63, 102, 102, 118, 63, 0, 0, 112, 63, 205, 204, 76, 63, 0, 0, 128, 63, 0, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 0, 0, 0, 168, 62, 0, 0, 0, 61, 102, 102, 166, 61, 0, 0, 0, 61, 0, 0, 0, 0, 51, 51, 59, 63, 0, 0, 0, 0),
"format": 34393296913,
"index_count": 21,
"index_data": PackedByteArray(8, 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 4, 0, 5, 0, 6, 0, 6, 0, 7, 0, 8, 0, 8, 0, 1, 0, 2, 0, 2, 0, 4, 0, 6, 0, 6, 0, 8, 0, 2, 0),
"primitive": 3,
"uv_scale": Vector4(0, 0, 0, 0),
"vertex_count": 9,
"vertex_data": PackedByteArray(0, 0, 128, 65, 204, 204, 252, 192, 0, 0, 128, 65, 0, 0, 96, 65, 204, 204, 108, 65, 0, 0, 96, 65, 154, 153, 25, 65, 0, 0, 128, 65, 0, 0, 128, 193, 0, 0, 128, 65, 0, 0, 128, 193, 0, 0, 176, 192, 0, 0, 112, 193, 102, 102, 86, 193, 0, 0, 112, 193, 0, 0, 128, 193, 204, 204, 236, 64, 0, 0, 128, 193)
}]

[resource]
mesh = SubResource("ArrayMesh_gi4ki")
23 changes: 23 additions & 0 deletions bin/project.godot
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
; [section] ; section goes between []
; param=value ; assign values to parameters

config_version=5

[application]

config/name="Godot Physics Tests"
config/features=PackedStringArray("4.2", "Forward Plus")

[display]

window/size/viewport_width=1920
window/size/viewport_height=1080

[physics]

2d/physics_engine="Rapier2D"
2 changes: 2 additions & 0 deletions bin/samples/godot-rapier2d/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Normalize EOL for all files that Git considers text files.
* text=auto eol=lf
2 changes: 2 additions & 0 deletions bin/samples/godot-rapier2d/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Godot 4+ specific ignores
.godot/
17 changes: 17 additions & 0 deletions bin/samples/godot-rapier2d/Faucet.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
extends Node2D

@export var fluid :Fluid2D

var points: PackedVector2Array
var velocities: PackedVector2Array

func _ready():
points = fluid.points
velocities.resize(points.size())
velocities.fill(Vector2(0, 980))

func _on_timer_timeout():
if len(fluid.points) > 2000:
return
fluid.set_points_and_velocities(fluid.points+points, fluid.velocities+velocities)
print(len(fluid.points))
172 changes: 172 additions & 0 deletions bin/samples/godot-rapier2d/test_fluid.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
[gd_scene load_steps=18 format=3 uid="uid://dudno1v5vtvh"]

[ext_resource type="Script" path="res://samples/godot-rapier2d/Faucet.gd" id="1_omq0c"]
[ext_resource type="Script" path="res://addons/godot-rapier2d/Fluid2DRectangle.gd" id="2_x6a0h"]
[ext_resource type="MultiMesh" uid="uid://d0bhettx43xt1" path="res://addons/godot-rapier2d/fluid_multi_mesh.tres" id="3_880wm"]
[ext_resource type="Texture2D" uid="uid://bjnvmmcdjgbu6" path="res://addons/godot-rapier2d/Node2D.svg" id="4_xdgjh"]
[ext_resource type="Script" path="res://addons/godot-rapier2d/Fluid2DRenderer.gd" id="5_2rahx"]

[sub_resource type="FluidEffect2DViscosityXSPH" id="FluidEffect2DViscosityXSPH_y3smb"]
fluid_viscosity_coefficient = 0.3

[sub_resource type="FluidEffect2DSurfaceTensionAKINCI" id="FluidEffect2DSurfaceTensionAKINCI_amypy"]
fluid_tension_coefficient = 10.0

[sub_resource type="RectangleShape2D" id="RectangleShape2D_8y86f"]
size = Vector2(2676, 1041)

[sub_resource type="RectangleShape2D" id="RectangleShape2D_xuw7n"]
size = Vector2(1749, 1004)

[sub_resource type="RectangleShape2D" id="RectangleShape2D_c23gn"]
size = Vector2(1177, 182.5)

[sub_resource type="RectangleShape2D" id="RectangleShape2D_sd5wa"]
size = Vector2(521.5, 914.5)

[sub_resource type="RectangleShape2D" id="RectangleShape2D_os3ki"]
size = Vector2(1521, 2437.5)

[sub_resource type="CircleShape2D" id="CircleShape2D_mdcyi"]
radius = 72.06

[sub_resource type="RectangleShape2D" id="RectangleShape2D_4bdhm"]
size = Vector2(286, 678)

[sub_resource type="RectangleShape2D" id="RectangleShape2D_vhhvt"]
size = Vector2(858, 333)

[sub_resource type="CircleShape2D" id="CircleShape2D_k12t3"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_q8nq7"]
size = Vector2(44, 303)

[node name="Node2D" type="Node2D"]

[node name="Faucet" type="Node2D" parent="." node_paths=PackedStringArray("fluid")]
position = Vector2(1468, 993)
script = ExtResource("1_omq0c")
fluid = NodePath("../Fluid2D")

[node name="Timer" type="Timer" parent="Faucet"]
process_callback = 0
wait_time = 0.05
autostart = true

[node name="Fluid2D" type="Fluid2D" parent="."]
accelerations = PackedVector2Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
velocities = PackedVector2Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
points = PackedVector2Array(0, 0, 40, 0, 80, 0, 120, 0, 160, 0, 0, 40, 40, 40, 80, 40, 120, 40, 160, 40)
density = 200.0
effects = Array[FluidEffect2D]([SubResource("FluidEffect2DViscosityXSPH_y3smb"), SubResource("FluidEffect2DSurfaceTensionAKINCI_amypy")])
debug_draw = false
modulate = Color(0.654902, 0.768627, 1, 1)
position = Vector2(1480, 680)
script = ExtResource("2_x6a0h")
height = 2
width = 5

[node name="Fluid2DRenderer" type="MultiMeshInstance2D" parent="Fluid2D"]
modulate = Color(0.380392, 0.576471, 1, 1)
position = Vector2(-220, -59)
multimesh = ExtResource("3_880wm")
texture = ExtResource("4_xdgjh")
script = ExtResource("5_2rahx")

[node name="StaticBody2D" type="StaticBody2D" parent="."]
position = Vector2(126, 282)

[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(-3554, 1698)
shape = SubResource("RectangleShape2D_8y86f")

[node name="CollisionShape2D2" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(-1170, 2352)
shape = SubResource("RectangleShape2D_8y86f")

[node name="CollisionShape2D3" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(-245, 2430)
shape = SubResource("RectangleShape2D_xuw7n")

[node name="CollisionShape2D8" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(-1114, 1509)
shape = SubResource("RectangleShape2D_xuw7n")

[node name="CollisionShape2D9" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(282, 1097.75)
shape = SubResource("RectangleShape2D_c23gn")

[node name="CollisionShape2D10" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(282, 1411)
shape = SubResource("RectangleShape2D_c23gn")

[node name="CollisionShape2D11" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(869, 2149)
rotation = 0.563727
shape = SubResource("RectangleShape2D_c23gn")

[node name="CollisionShape2D4" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(1824, 2579)
shape = SubResource("RectangleShape2D_8y86f")

[node name="CollisionShape2D5" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(2171.25, 1708.25)
shape = SubResource("RectangleShape2D_sd5wa")

[node name="CollisionShape2D7" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(3748.5, 1752.75)
shape = SubResource("RectangleShape2D_os3ki")

[node name="CollisionShape2D6" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(1405, 1225)
shape = SubResource("CircleShape2D_mdcyi")

[node name="Camera2D" type="Camera2D" parent="."]
position = Vector2(1586, 1637)
zoom = Vector2(0.5, 0.5)

[node name="StaticBody2D2" type="StaticBody2D" parent="."]
position = Vector2(1574, 643)

[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D2"]
position = Vector2(-333, -38)
shape = SubResource("RectangleShape2D_4bdhm")

[node name="CollisionShape2D2" type="CollisionShape2D" parent="StaticBody2D2"]
position = Vector2(-45, -277)
shape = SubResource("RectangleShape2D_vhhvt")

[node name="CollisionShape2D3" type="CollisionShape2D" parent="StaticBody2D2"]
position = Vector2(225, -65)
shape = SubResource("RectangleShape2D_4bdhm")

[node name="Label" type="Label" parent="."]
offset_left = 1316.0
offset_top = 987.0
offset_right = 1903.0
offset_bottom = 1042.0
theme_override_font_sizes/font_size = 80
text = "Rapier2D Fluid using Salva2D"

[node name="StaticBody2D3" type="StaticBody2D" parent="."]

[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D3"]
position = Vector2(1380, 1879)
shape = SubResource("CircleShape2D_k12t3")

[node name="PinJoint2D" type="PinJoint2D" parent="."]
position = Vector2(1384, 1875)
node_a = NodePath("../StaticBody2D3")
node_b = NodePath("../RigidBody2D")
motor_enabled = true
motor_target_velocity = 872.665

[node name="RigidBody2D" type="RigidBody2D" parent="."]
position = Vector2(1363, 1874)
mass = 200.0

[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D"]
position = Vector2(12, 141.5)
shape = SubResource("RectangleShape2D_q8nq7")

[connection signal="timeout" from="Faucet/Timer" to="Faucet" method="_on_timer_timeout"]
20 changes: 19 additions & 1 deletion godot-rapier.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
"__locale": "cpp",
"string": "cpp",
"string_view": "cpp",
"__config": "cpp"
"__config": "cpp",
"deque": "cpp",
"list": "cpp"
}
},
"launch": {
Expand All @@ -52,6 +54,22 @@
"--debug-collisions",
"test.tscn"
]
},
{
"name": "Launch Fluid",
"program": "${workspaceFolder:godot}/bin/godot.macos.editor.arm64",
"type": "cppdbg",
"request": "launch",
"cwd": "${workspaceFolder:godot-rapier-2d}",
"osx": {
"MIMode": "lldb"
},
"args": [
"--path",
"${workspaceFolder:godot-rapier-2d}/samples/godot-rapier2d",
"--debug-collisions",
"test_fluid.tscn"
]
}
]
},
Expand Down
2 changes: 2 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ cd ../..
scons arch=arm64 target=template_debug debug_symbols=yes
rm -rf Godot-Physics-Tests/addons/godot-rapier2d
cp -rf bin/addons/godot-rapier2d Godot-Physics-Tests/addons/godot-rapier2d
rm -rf bin/samples/godot-rapier2d/addons/godot-rapier2d
cp -rf bin/addons/godot-rapier2d bin/samples/godot-rapier2d/addons/godot-rapier2d

Loading

0 comments on commit bff1f71

Please sign in to comment.