RapidGameFramework
Reusable Godot managers for data-driven small games
Loading...
Searching...
No Matches
motion_integrator.gd
Go to the documentation of this file.
1extends RefCounted
2
3
6
7
8
9func add_force(body:Dictionary, force:Vector2) -> Dictionary:
10 var next = body.duplicate(true)
11 next["force"] = next.get("force", Vector2.ZERO) + force
12 return next
13
14
15
16func apply_impulse(body:Dictionary, impulse:Vector2) -> Dictionary:
17 var next = body.duplicate(true)
18 if bool(next.get("static", false)):
19 return next
20 next["velocity"] = next.get("velocity", Vector2.ZERO) + impulse * float(next.get("inv_mass", 0.0))
21 return next
22
23
24
25func integrate(body:Dictionary, delta:float, gravity:Vector2 = Vector2.ZERO, damping:float = 0.0) -> Dictionary:
26 var next = body.duplicate(true)
27 if bool(next.get("static", false)):
28 next["force"] = Vector2.ZERO
29 next["acceleration"] = Vector2.ZERO
30 return next
31 var inv_mass = float(next.get("inv_mass", 1.0))
32 var acceleration = gravity + next.get("force", Vector2.ZERO) * inv_mass
33 var velocity = next.get("velocity", Vector2.ZERO) + acceleration * delta
34 if damping > 0.0:
35 velocity *= max(0.0, 1.0 - damping * delta)
36 next["previous_position"] = next.get("position", Vector2.ZERO)
37 next["position"] = next.get("position", Vector2.ZERO) + velocity * delta
38 next["velocity"] = velocity
39 next["acceleration"] = acceleration
40 next["force"] = Vector2.ZERO
41 return next