Skip to content

Commit

Permalink
Add particle lifetime. Fix shape disabled warning. (#69)
Browse files Browse the repository at this point in the history
- Fixes #68
- Fixes #67
  • Loading branch information
Ughuuu authored Apr 17, 2024
1 parent bff1f71 commit 20d14a2
Show file tree
Hide file tree
Showing 18 changed files with 358 additions and 84 deletions.
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@

A 2d [rapier](https://github.com/dimforge/rapier) physics server for [Godot Engine v4.2](https://github.com/godotengine/godot), implemented as a GDExtension. Also integrates with salva for 2d fluids.

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

# Features

- Parallel support (for non enhnanced determinism builds)
- SIMD (Single instruction, multiple data) build.
- Parallel support and SIMD build for better performance.
- Better physics stability.
- Fluids with surface tension, viscousity and elastic liquids.
- Fluids with surface tension, viscosity and elasticity.
- *DISABLED* 32 bits and 64 bits support.
- *DISABLED* Cross platform determinism.

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

<p align="center">
<img src="Fluid2d.gif"/>
Expand All @@ -39,6 +41,7 @@ A 2d [rapier](https://github.com/dimforge/rapier) physics server for [Godot Engi
- SeparationRay2D missing [issues/5](https://github.com/appsinacup/godot-rapier-2d/issues/5)
- Web exports not working [issues/23](https://github.com/appsinacup/godot-rapier-2d/issues/23)
- Cross platform determinism isn't working [issues/47](https://github.com/appsinacup/godot-rapier-2d/issues/47)
- Double build isn't working [issues/61](https://github.com/appsinacup/godot-rapier-2d/issues/61)

# Supported Platforms

Expand All @@ -47,7 +50,7 @@ A 2d [rapier](https://github.com/dimforge/rapier) physics server for [Godot Engi
- Linux (x86_64)
- Android (x86_64, arm64)
- iOS (arm64)
- Web (wasm32)
- *DISABLED* Web (wasm32)

# Installation

Expand All @@ -59,6 +62,10 @@ A 2d [rapier](https://github.com/dimforge/rapier) physics server for [Godot Engi

After installing, go to `Advanced Settings` -> `Physics` -> `2D`. Change `Physics Engine` to `Rapier2D`.

# Samples

After installing the addon, the samples are in the `samples/godot-rapier2d` folder. In order to run them, you have to enable the physics engine for some extra features to work (eg. fluids) as described above.

# Contribute

If you want to contribute, view [CONTRIBUTE.md](CONTRIBUTE.md) for more info.
Expand Down
11 changes: 10 additions & 1 deletion bin/addons/godot-rapier2d/Fluid2DRenderer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ class_name Fluid2DRenderer
extends MultiMeshInstance2D

@onready var fluid :Fluid2D = get_parent()
@export var color: Color

func _process(delta):
var index = 0
multimesh.instance_count = fluid.points.size()
for point in fluid.points:
var points = fluid.points
var create_times = fluid.get_create_times()
var current_time = Time.get_ticks_msec()
for i in points.size():
var point = points[i]
var created_at = create_times[i]
var life_remain = fluid.lifetime - (current_time - created_at) / 1000.0 + 0.5
var new_transform: Transform2D = Transform2D()
var new_color : Color = Color(color, life_remain)
new_transform.origin = point - position
multimesh.set_instance_transform_2d(index, new_transform)
multimesh.set_instance_color(index, new_color)
index += 1
1 change: 1 addition & 0 deletions bin/addons/godot-rapier2d/fluid_multi_mesh.tres
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ _surfaces = [{
}]

[resource]
use_colors = true
mesh = SubResource("ArrayMesh_gi4ki")
28 changes: 28 additions & 0 deletions bin/samples/godot-rapier2d/CharacterBody2D.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
extends CharacterBody2D


const SPEED = 600.0
const JUMP_VELOCITY = -800.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")


func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta

# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)

move_and_slide()
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class_name Faucet2D
extends Node2D

@export var fluid :Fluid2D
@export var fluid : Fluid2D

var points: PackedVector2Array
var velocities: PackedVector2Array
Expand All @@ -13,5 +14,4 @@ func _ready():
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))
fluid.add_points_and_velocities(points, velocities)
96 changes: 73 additions & 23 deletions bin/samples/godot-rapier2d/test_fluid.tscn
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[gd_scene load_steps=18 format=3 uid="uid://dudno1v5vtvh"]
[gd_scene load_steps=20 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://samples/godot-rapier2d/Faucet2D.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"]
[ext_resource type="Script" path="res://samples/godot-rapier2d/CharacterBody2D.gd" id="6_wpnii"]

[sub_resource type="FluidEffect2DViscosityXSPH" id="FluidEffect2DViscosityXSPH_y3smb"]
fluid_viscosity_coefficient = 0.3
Expand Down Expand Up @@ -36,10 +37,14 @@ size = Vector2(286, 678)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_vhhvt"]
size = Vector2(858, 333)

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

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

[sub_resource type="RectangleShape2D" id="RectangleShape2D_q8nq7"]
size = Vector2(44, 303)
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_re251"]
radius = 75.0
height = 200.0

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

Expand All @@ -49,15 +54,12 @@ 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
lifetime = 20.0
effects = Array[FluidEffect2D]([SubResource("FluidEffect2DViscosityXSPH_y3smb"), SubResource("FluidEffect2DSurfaceTensionAKINCI_amypy")])
debug_draw = false
modulate = Color(0.654902, 0.768627, 1, 1)
Expand All @@ -67,11 +69,11 @@ 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")
color = Color(0.552941, 0.701961, 1, 1)

[node name="StaticBody2D" type="StaticBody2D" parent="."]
position = Vector2(126, 282)
Expand All @@ -80,6 +82,25 @@ position = Vector2(126, 282)
position = Vector2(-3554, 1698)
shape = SubResource("RectangleShape2D_8y86f")

[node name="CollisionShape2D12" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(-4216, 860)
shape = SubResource("RectangleShape2D_8y86f")

[node name="CollisionShape2D13" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(-3409, 261)
rotation = 1.2564
shape = SubResource("RectangleShape2D_8y86f")

[node name="CollisionShape2D14" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(-2104, -1120)
rotation = -0.0459269
shape = SubResource("RectangleShape2D_8y86f")

[node name="CollisionShape2D15" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(3374, -699)
rotation = 0.981285
shape = SubResource("RectangleShape2D_8y86f")

[node name="CollisionShape2D2" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(-1170, 2352)
shape = SubResource("RectangleShape2D_8y86f")
Expand Down Expand Up @@ -122,8 +143,8 @@ position = Vector2(1405, 1225)
shape = SubResource("CircleShape2D_mdcyi")

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

[node name="StaticBody2D2" type="StaticBody2D" parent="."]
position = Vector2(1574, 643)
Expand All @@ -148,25 +169,54 @@ offset_bottom = 1042.0
theme_override_font_sizes/font_size = 80
text = "Rapier2D Fluid using Salva2D"

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

[node name="RigidBody2D" type="RigidBody2D" parent="PinJoint2D"]
position = Vector2(-32, -1)
mass = 400.0

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

[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D3"]
[node name="StaticBody2D3" type="StaticBody2D" parent="PinJoint2D"]
position = Vector2(-1384, -1875)

[node name="CollisionShape2D" type="CollisionShape2D" parent="PinJoint2D/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")
[node name="PinJoint2D2" type="PinJoint2D" parent="."]
position = Vector2(2858, 1869)
node_a = NodePath("StaticBody2D3")
node_b = NodePath("RigidBody2D")
motor_enabled = true
motor_target_velocity = 872.665
motor_target_velocity = 349.066

[node name="RigidBody2D" type="RigidBody2D" parent="."]
position = Vector2(1363, 1874)
mass = 200.0
[node name="RigidBody2D" type="RigidBody2D" parent="PinJoint2D2"]
position = Vector2(-21, -1)
mass = 400.0

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

[node name="StaticBody2D3" type="StaticBody2D" parent="PinJoint2D2"]
position = Vector2(-1384, -1875)

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

[node name="CharacterBody2D" type="CharacterBody2D" parent="."]
script = ExtResource("6_wpnii")

[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"]
shape = SubResource("CapsuleShape2D_re251")

[connection signal="timeout" from="Faucet/Timer" to="Faucet" method="_on_timer_timeout"]
14 changes: 11 additions & 3 deletions godot-rapier.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@
"string_view": "cpp",
"__config": "cpp",
"deque": "cpp",
"list": "cpp"
"list": "cpp",
"__hash_table": "cpp",
"__split_buffer": "cpp",
"__tree": "cpp",
"array": "cpp",
"initializer_list": "cpp",
"queue": "cpp",
"span": "cpp",
"stack": "cpp"
}
},
"launch": {
Expand Down Expand Up @@ -66,9 +74,9 @@
},
"args": [
"--path",
"${workspaceFolder:godot-rapier-2d}/samples/godot-rapier2d",
"${workspaceFolder:godot-rapier-2d}/bin",
"--debug-collisions",
"test_fluid.tscn"
"samples/godot-rapier2d/test_fluid.tscn"
]
}
]
Expand Down
2 changes: 0 additions & 2 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ 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 20d14a2

Please sign in to comment.