11func create_body(id:String, position:Vector2, options:Dictionary = {}) -> Dictionary:
15 "previous_position": position,
16 "velocity": options.get(
"velocity", Vector2.ZERO),
17 "acceleration": Vector2.ZERO,
18 "force": Vector2.ZERO,
19 "mass": max(0.0001, float(options.get(
"mass", 1.0))),
21 "static": bool(options.get(
"static", false)),
22 "restitution": clamp(float(options.get(
"restitution", 0.1)), 0.0, 1.0),
23 "friction": clamp(float(options.get(
"friction", 0.2)), 0.0, 1.0),
24 "shape": options.get(
"shape", {
"type":
"aabb",
"size": Vector2(16, 16)}).duplicate(true),
25 "layer": int(options.get(
"layer", 1)),
26 "mask": int(options.get(
"mask", 1)),
27 "metadata": options.get(
"metadata", {}).duplicate(true)
29 body[
"inv_mass"] = 0.0
if bool(body[
"static"])
else 1.0 / float(body[
"mass"])
31 return body.duplicate(true)
35func get_body(id:String) -> Dictionary:
36 return bodies.get(id, {}).duplicate(true)
40func set_body(body:Dictionary) -> void:
41 var id = str(body.get(
"id",
""))
44 bodies[id] = body.duplicate(true)
48func remove_body(id:String) -> void:
53func get_body_ids() -> Array:
58func get_bodies() -> Array:
60 for id
in bodies.keys():
61 results.append(bodies[id].duplicate(true))
66func can_collide(a:Dictionary, b:Dictionary) -> bool:
67 if bool(a.get(
"static", false))
and bool(b.get(
"static", false)):
69 var a_layer = int(a.get(
"layer", 1))
70 var a_mask = int(a.get(
"mask", 1))
71 var b_layer = int(b.get(
"layer", 1))
72 var b_mask = int(b.get(
"mask", 1))
73 return (a_mask & b_layer) != 0
and (b_mask & a_layer) != 0
77func get_state() -> Dictionary:
78 return {
"bodies": bodies.duplicate(true)}
82func apply_state(state:Dictionary) -> void:
83 bodies = state.get(
"bodies", {}).duplicate(true)