How do I change 2 RigidBody2Ds into one unique RigidBody2D when they're colliding with eachother?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Sidoku

I want to make a chemical reaction with water and lava particles (RigidBody2D) so that it becomes obsidian. And I want them to change their sprite and collision to another shape entirely. From a circle to a cube for example. I couldn’t find any other explanation of merching 2 RigidBodys together into one. When I put the particles in my scene, I duplicate it to over 100 pieces, so my idea is that when they merch it becomes less each time.

This is my script so far with the Water Rigid Body:

extends RigidBody2D

export(String, FILE, "*.tscn") var WaterParticleCollisionShape2D = ""
export(String, FILE, "*.tscn") var WaterAreaCollisionShape2D = ""

var static_mode_switch_legth = 2
func _integrate_forces(state):
	if state.linear_velocity.length() < static_mode_switch_legth:
		sleeping = false

func _on_WaterParticle_body_shape_entered(_body_rid, _body, body_shape_index, _local_shape_index):
	if $WaterParticleCollisionShape2D.collided_with(body_shape_index): visible = false
	if $WaterParticleCollisionShape2D.empty(): queue_free()
	if $WaterAreaCollisionShape2D.collided_with(body_shape_index): visible = false
	if $WaterAreaCollisionShape2D.empty(): queue_free()
	pass # Replace with function body.

The body shape entered function haven’t solved the problem, but maybe I haven’t writen the correct input.

This is the script for the lava rigid body:

extends RigidBody2D

export(String, FILE, "*.tscn") var game1_target_level_path = ""

var static_mode_switch_legth = 2
func _integrate_forces(state):
	if state.linear_velocity.length() < static_mode_switch_legth:
		sleeping = false


func _on_Area2D_body_entered(body):
	if not body is Player: return
	if game1_target_level_path.empty(): return
	get_tree().change_scene(game1_target_level_path)
	pass # Replace with function body.

I don’t know if it even is possible. If it is possible, please let me know. Thanks.

:bust_in_silhouette: Reply From: aidave

This is possible but not directly by adding two RigidBody together. Instead you can move the collision shapes from one body to the other.

So you can remove_child the collision shape on first RigidBody, then add_child to the second RigidBody (maybe also set_parent), then queue_free the first RigidBody. You will have to account for position, so convert global to local coordinates.

Hey, sorry for not answering right away. I’m really new to Godot and don’t know where to put remove child, add child, set parent and queue free to my script. And how do I convert global to local coordinates?

Sidoku | 2023-04-25 06:18

You’re going to need to learn more about gdscript to answer those kinds of questions

Try some tutorials and do a search for those topics. Examples:

Nodes and scene instances — Godot Engine (stable) documentation in English
https://forum.godotengine.org/40058/what-is-the-difference-between-global-and-local-coordinates

aidave | 2023-04-25 14:19