I'm having some trouble with this. I want all the rigid bodies in the scene to have the same position, rotation, and scale.
How do I sync rigid body physics across a network?
If i remember correctly i read somewhere that when using RigiBodies with networking you should take care that only the server intstantiates a RigidBody. The Clients should instatiate a KinematicBody. This results in the physics only being processed on the server side. Then its quite straight forward to sync position, rotation and scale. The only thing you have to take care of is that the RigidBody on the server and the KinematicBody on the client have the same name and same position in the scenetree
That's what I trying to do right now. I'm doing this:
for body in get_tree().get_nodes_in_group("bodies"):
if body is RigidBody2D:
create_physics_puppet(body)
body.queue_free()
func create_physics_puppet(body):
var body_pos
var body_rot
var body_collision_scale
var body_collision_shape
var body_sprite_texture
body_pos = body.position
body_rot = body.rotation
for child in body.get_children():
if child is CollisionShape2D:
body_sprite_texture = child.get_child(0).texture
body_collision_scale = child.scale
body_collision_shape = child.shape
var new_body = KinematicBody2D.new()
var new_body_collision = CollisionShape2D.new()
var new_body_sprite = Sprite.new()
get_node("ShootingRange").add_child(new_body)
new_body.add_child(new_body_collision)
new_body_collision.add_child(new_body_sprite)
new_body.position = body_pos
new_body.rotation = body_rot
new_body_collision.scale = body_collision_scale
new_body_collision.shape = body_collision_shape
new_body_sprite.texture = body_sprite_texture
For some reason only two of these new kinematic bodies are showing up in-game. They all show up in the remote scene tree though. I turned on visible collisions just to see if the sprites were messed up, but that wasn't it. Browsing through the kinematic bodies in the remote scene tree, they all seemed to have reasonable transforms.
Are they showing up properly in the remote scene tree on the server or on the client side? I'm guessing client side?
Client side. I'm just testing the creation of kinematic bodies before I make it work over the network.
very strange....
What happens if you actually construct your kinematicBody as a scene, and add an initialize()
function. Then in your script above just load one of those scenes and initialize it with all necessary parameters.
The only reason i could think of is that maybe as you call queue_free()
inside of the loop that the loop somehow messes up the bodies index and therefore you somehow "skip"some of them. So another solution you could try is free your rigidbodies in a second run, after adding all kinematic bodies
@vinpogo said: very strange.... What happens if you actually construct your kinematicBody as a scene, and add an
initialize()
function. Then in your script above just load one of those scenes and initialize it with all necessary parameters.
That doesn't fix it unfortunately.
The only reason i could think of is that maybe as you call
queue_free()
inside of the loop that the loop somehow messes up the bodies index and therefore you somehow "skip"some of them. So another solution you could try is free your rigidbodies in a second run, after adding all kinematic bodies
I tried both getting rid of the queue_free()
and doing it on a second pass. Neither of those worked.
By the way, here's my source code: https://git.haddock.cc/ScrapjackStudios/slayer/src/branch/synced_physics