-
Notifications
You must be signed in to change notification settings - Fork 2
/
chipmunk_demo.rb
70 lines (46 loc) · 1.15 KB
/
chipmunk_demo.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require 'rubygems'
require 'chipmunk-ffi'
require 'simple'
require 'jruby'
class Sketch < Processing::App
include Visible
def setup
JRuby.objectspace=true
frame_rate 30
rect_mode RADIUS
ellipse_mode RADIUS
smooth
stroke 255
stroke_weight 2
no_fill
@demo = Simple.new
end
def draw
background 51
@demo.update
@demo.visible_shapes.each do|shape|
# Resetting all transformations possibly done by previous shapes' draw() method calls
reset_matrix
# Drawing our stuff around the center of Processing window, not upper-left corner
translate width/2, height/2
# In Chipmunk's demos Y axis is directed the other way , so we flip it
scale 1, -1
stroke shape == @shape_grabbed ? 200 : 255
shape.draw
end
end
def mouse_pressed
@shape_grabbed = @demo.grab(mouse_to_space)
end
def mouse_released
@demo.release
@shape_grabbed = nil
end
def mouse_dragged
@demo.drag mouse_to_space
end
def mouse_to_space
vec2(mouse_x - width/2, height/2 - mouse_y)
end
end
Sketch.new(:width => 600, :height => 600, :title => "Chipmunk Demos")