Issue with snapping 2 parts together

Godot Version

Godot 4.3

Question

I’m making a game where the player is going through a procedurally generated facility, the code picks from a pool of rooms and snaps the entrance part of the current generating room to the exit of the last one.

The problem comes with the rotation, I can rotate the new room to match the rotation of the last rooms exit, but that rotates the new room along the center, offsetting it, and I want it to rotate along the entrance part so it stays in place.

Entrance is blue, exit is red

Without rotation:

With Rotation (i want it to not be offset and for the entrance to line up with the exit)

Here’s what the model looks like

And here is the code:

extends StaticBody3D

@export var room_count = 100

var room_pool : Dictionary
var rooms : Dictionary

func _ready() -> void:
	 # Define rooms in pool
	room_pool[0] = preload("res://Models/StupidPlatform.blend")
	room_pool[1] = preload("res://Models/ExtraStupidPlatform.blend")
	generate_rooms()

func generate_rooms():
	 # Decide which room will be what
	for i in room_count:
		rooms[i] = get_random_room()
	
	 # Generate Rooms
	var room_instance
	for i in rooms.size():
		var prev_room = room_instance
		room_instance = rooms[i].instantiate()
		if prev_room == null:
			 # If first room, stay in center
			room_instance.global_position = Vector3.ZERO
		else:
			var prev_exit = prev_room.get_node("Exit")
			var entrance = room_instance.get_node("Entrance")
			 # Find distance between center of model and where the entrance part is
			var entrance_offset = entrance.position - room_instance.position
			
			room_instance.position = prev_exit.global_position - entrance_offset
			room_instance.rotation = prev_exit.rotation
		
		add_child(room_instance)

func get_random_room():
	var random = randi_range(0, room_pool.size() - 1)
	var room = room_pool[random]
	
	return room

i think that’s because you are setting the rotation before the instance even exist

Having it happen after didn’t seem to change anything

are the instances unique? every instance has its unique things and isn’t linked with other instances

They’re still called entrance and exit remotely

no, i mean the instances, is every instance you make unique

did you enabled “local_to_scene” in the meshes inspector, if you dont do that, they will al od the same and not be unique

Ok I did that but it still looks the same

that’s weird cuz there is nothçing miss with your code

I think I need like a pivot point or something

1 Like

I have fixed the problem

After defining the entrance_offset:

var children = room_instance.get_children()
pivot_meshes(children, entrance_offset)

func pivot_meshes(meshes, offset):
	for mesh in meshes:
		if mesh is MeshInstance3D:
			mesh.position -= offset

This makes the center of the model be the center of the entrance mesh.

Then we just:

room_instance.position = prev_exit.global_position
room_instance.rotation = prev_exit.rotation
room_instance.rotation += prev_room.rotation

I correctly position and rotate the room