Last active
July 25, 2024 21:46
-
-
Save jknightdoeswork/820e4a9333b15f999da5ac82045fd297 to your computer and use it in GitHub Desktop.
Example of using a multimesh renderer with the physics server api in godot.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends MultiMeshInstance2D | |
export var instances := 10000 | |
var physics_bodies := [] | |
func _ready() -> void: | |
multimesh.instance_count = instances | |
for i in range(multimesh.instance_count): | |
# Create multimesh renderer | |
var r := 600.0 | |
var s := 50.0 | |
var offset := Vector2(1000.0, 100.0) | |
var p := Vector2(rand_range(-r, r), rand_range(-s, s)) | |
var t := Transform2D(0.0, p + offset) | |
multimesh.set_instance_transform_2d(i, t) | |
# Create physics body | |
var physics_body := Physics2DServer.body_create() | |
Physics2DServer.body_set_mode(physics_body, Physics2DServer.BODY_MODE_RIGID) | |
Physics2DServer.body_set_state(physics_body, Physics2DServer.BODY_STATE_TRANSFORM, t) | |
Physics2DServer.body_set_space(physics_body, get_world_2d().space) | |
Physics2DServer.body_set_force_integration_callback(physics_body, self, "_body_moved", i) | |
physics_bodies.append(physics_body) | |
# Create shape | |
var circle_shape := Physics2DServer.circle_shape_create() | |
Physics2DServer.shape_set_data(circle_shape, 10.0) | |
Physics2DServer.body_add_shape(physics_body, circle_shape, Transform2D.IDENTITY) | |
func _body_moved(state, index): | |
multimesh.set_instance_transform_2d(index, state.transform) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment