Skip to content

Instantly share code, notes, and snippets.

@thimenesup
Created April 26, 2019 14:28
Show Gist options
  • Save thimenesup/a808411599cc765283e23c0acf832ca9 to your computer and use it in GitHub Desktop.
Save thimenesup/a808411599cc765283e23c0acf832ca9 to your computer and use it in GitHub Desktop.
Adds static collision to all the instances in a multi mesh node
extends MultiMeshInstance
export(Shape) var shape = null
var _shape_rid = null
var _bodies = []
#Done in _enter_tree() and not in _init() because its when the exported shape and world space is set
func _enter_tree():
_shape_rid = RID(shape)
var i = 0
while i < multimesh.instance_count:
var body = PhysicsServer.body_create(PhysicsServer.BODY_MODE_STATIC)
PhysicsServer.body_set_space(body, get_world().space)
PhysicsServer.body_set_collision_layer(body, 1)
PhysicsServer.body_set_collision_mask(body, 1)
PhysicsServer.body_set_ray_pickable(body, false)
PhysicsServer.body_add_shape(body, _shape_rid)
_bodies.append(body)
var instance_transform = multimesh.get_instance_transform(i)
PhysicsServer.body_set_state(body, PhysicsServer.BODY_STATE_TRANSFORM, global_transform * instance_transform)
i +=1
func _notification(what):
if what == NOTIFICATION_PREDELETE:
for body in _bodies:
PhysicsServer.free_rid(body)
PhysicsServer.free_rid(_shape_rid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment