-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimulation.rb
42 lines (36 loc) · 971 Bytes
/
simulation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Simulation
attr_reader :visible_shapes
def initialize
@visible_shapes = []
@space = CP::Space.new
@mouse_body = CP::Body.new CP::INFINITY, CP::INFINITY
end
def update(ticks = 0)
@space.step 1.0/60.0
end
def grab(point)
release
@last_mouse_point = point
shape = @space.point_query_first(point, CP::ALL_ONES, 0)
if (shape)
@mouse_joint = CP::PivotJoint.new @mouse_body, shape.body, CP::ZERO_VEC_2, shape.body.world2local(point)
@mouse_joint.max_force = 50000.0
@mouse_joint.bias_coef = 0.15
@space.add_constraint @mouse_joint
end
end
def release
if @mouse_joint
@space.remove_constraint @mouse_joint
@mouse_joint = nil
end
end
def drag(point)
if @mouse_joint
new_point = @last_mouse_point.lerp(point, 0.25)
@mouse_body.p = new_point
@mouse_body.v = (new_point - @last_mouse_point) * 60
@last_mouse_point = new_point
end
end
end