1 ! Copyright (C) 2010 Erik Charlebois
2 ! See http:// factorcode.org/license.txt for BSD license.
3 USING: accessors chipmunk classes.struct game.worlds kernel locals
4 math method-chains opengl.gl random sequences specialized-arrays
5 specialized-arrays.instances.alien.c-types.void* ui ui.gadgets.worlds
9 CONSTANT: image-width 188
10 CONSTANT: image-height 35
11 CONSTANT: image-row-length 24
13 CONSTANT: image-bitmap B{
14 15 -16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 -64 15 63 -32 -2 0 0 0 0 0 0 0
15 0 0 0 0 0 0 0 0 0 0 0 31 -64 15 127 -125 -1 -128 0 0 0 0 0 0 0 0 0 0 0 0 0 0
16 0 0 0 127 -64 15 127 15 -1 -64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 -64 15 -2
17 31 -1 -64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 -64 0 -4 63 -1 -32 0 0 0 0 0 0
18 0 0 0 0 0 0 0 0 0 0 1 -1 -64 15 -8 127 -1 -32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
19 1 -1 -64 0 -8 -15 -1 -32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -31 -1 -64 15 -8 -32
20 -1 -32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 -15 -1 -64 9 -15 -32 -1 -32 0 0 0 0 0
21 0 0 0 0 0 0 0 0 0 0 31 -15 -1 -64 0 -15 -32 -1 -32 0 0 0 0 0 0 0 0 0 0 0 0 0
22 0 0 63 -7 -1 -64 9 -29 -32 127 -61 -16 63 15 -61 -1 -8 31 -16 15 -8 126 7 -31
23 -8 31 -65 -7 -1 -64 9 -29 -32 0 7 -8 127 -97 -25 -1 -2 63 -8 31 -4 -1 15 -13
24 -4 63 -1 -3 -1 -64 9 -29 -32 0 7 -8 127 -97 -25 -1 -2 63 -8 31 -4 -1 15 -13
25 -2 63 -1 -3 -1 -64 9 -29 -32 0 7 -8 127 -97 -25 -1 -1 63 -4 63 -4 -1 15 -13
26 -2 63 -33 -1 -1 -32 9 -25 -32 0 7 -8 127 -97 -25 -1 -1 63 -4 63 -4 -1 15 -13
27 -1 63 -33 -1 -1 -16 9 -25 -32 0 7 -8 127 -97 -25 -1 -1 63 -4 63 -4 -1 15 -13
28 -1 63 -49 -1 -1 -8 9 -57 -32 0 7 -8 127 -97 -25 -8 -1 63 -2 127 -4 -1 15 -13
29 -1 -65 -49 -1 -1 -4 9 -57 -32 0 7 -8 127 -97 -25 -8 -1 63 -2 127 -4 -1 15 -13
30 -1 -65 -57 -1 -1 -2 9 -57 -32 0 7 -8 127 -97 -25 -8 -1 63 -2 127 -4 -1 15 -13
31 -1 -1 -57 -1 -1 -1 9 -57 -32 0 7 -1 -1 -97 -25 -8 -1 63 -1 -1 -4 -1 15 -13 -1
32 -1 -61 -1 -1 -1 -119 -57 -32 0 7 -1 -1 -97 -25 -8 -1 63 -1 -1 -4 -1 15 -13 -1
33 -1 -61 -1 -1 -1 -55 -49 -32 0 7 -1 -1 -97 -25 -8 -1 63 -1 -1 -4 -1 15 -13 -1
34 -1 -63 -1 -1 -1 -23 -49 -32 127 -57 -1 -1 -97 -25 -1 -1 63 -1 -1 -4 -1 15 -13
35 -1 -1 -63 -1 -1 -1 -16 -49 -32 -1 -25 -1 -1 -97 -25 -1 -1 63 -33 -5 -4 -1 15
36 -13 -1 -1 -64 -1 -9 -1 -7 -49 -32 -1 -25 -8 127 -97 -25 -1 -1 63 -33 -5 -4 -1
37 15 -13 -1 -1 -64 -1 -13 -1 -32 -49 -32 -1 -25 -8 127 -97 -25 -1 -2 63 -49 -13
38 -4 -1 15 -13 -1 -1 -64 127 -7 -1 -119 -17 -15 -1 -25 -8 127 -97 -25 -1 -2 63
39 -49 -13 -4 -1 15 -13 -3 -1 -64 127 -8 -2 15 -17 -1 -1 -25 -8 127 -97 -25 -1
40 -8 63 -49 -13 -4 -1 15 -13 -3 -1 -64 63 -4 120 0 -17 -1 -1 -25 -8 127 -97 -25
41 -8 0 63 -57 -29 -4 -1 15 -13 -4 -1 -64 63 -4 0 15 -17 -1 -1 -25 -8 127 -97
42 -25 -8 0 63 -57 -29 -4 -1 -1 -13 -4 -1 -64 31 -2 0 0 103 -1 -1 -57 -8 127 -97
43 -25 -8 0 63 -57 -29 -4 -1 -1 -13 -4 127 -64 31 -2 0 15 103 -1 -1 -57 -8 127
44 -97 -25 -8 0 63 -61 -61 -4 127 -1 -29 -4 127 -64 15 -8 0 0 55 -1 -1 -121 -8
45 127 -97 -25 -8 0 63 -61 -61 -4 127 -1 -29 -4 63 -64 15 -32 0 0 23 -1 -2 3 -16
46 63 15 -61 -16 0 31 -127 -127 -8 31 -1 -127 -8 31 -128 7 -128 0 0 }
48 :: get-pixel ( x y -- z )
49 x -3 shift y image-row-length * + image-bitmap nth
50 x bitnot 7 bitand neg shift 1 bitand 1 = ;
52 :: make-ball ( x y -- shape )
53 cpBodyAlloc 1.0 NAN: 0 cpBodyInit
55 cpCircleShapeAlloc body 0.95 0 0 cpv cpCircleShapeInit
56 [ shape>> 0 >>e ] [ shape>> 0 >>u ] bi drop ;
58 TUPLE: chipmunk-world < game-world
61 AFTER: chipmunk-world tick-game-world
62 space>> 1.0 60.0 / cpSpaceStep ;
64 SPECIALIZED-ARRAY: cpContact
65 M:: chipmunk-world draw-world* ( world -- )
67 GL_COLOR_BUFFER_BIT glClear
68 GL_PROJECTION glMatrixMode
70 -320 320 -240 240 -1 1 glOrtho
71 0.5 0.5 0 glTranslatef
72 GL_VERTEX_ARRAY glEnableClientState
74 world space>> :> space
80 [ num>> ] [ arr>> swap <direct-void*-array> ] bi [
81 cpBody memory>struct p>> [ x>> ] [ y>> ] bi glVertex2f
89 [ num>> ] [ arr>> swap <direct-void*-array> ] bi [
90 cpArbiter memory>struct
91 [ numContacts>> ] [ contacts>> swap <direct-cpContact-array> ] bi [
92 p>> [ x>> ] [ y>> ] bi glVertex2f
97 M:: chipmunk-world begin-game-world ( world -- )
100 cpSpaceAlloc cpSpaceInit :> space
102 world space >>space drop
103 space 2.0 10000 cpSpaceResizeActiveHash
104 space 1 >>iterations drop
106 image-height iota [| y |
107 image-width iota [| x |
109 x image-width 2 / - 0.05 0.0 1.0 uniform-random-float * + 2 *
110 image-height 2 / y - 0.05 0.0 1.0 uniform-random-float * + 2 *
112 space shape body>> cpSpaceAddBody drop
113 space shape cpSpaceAddShape drop
118 space cpBodyAlloc NAN: 0 dup cpBodyInit cpSpaceAddBody :> body
119 body -1000 -10 cpv >>p drop
120 body 400 0 cpv >>v drop
122 space cpCircleShapeAlloc body 8 0 0 cpv cpCircleShapeInit cpSpaceAddShape :> shape
124 [ shape>> 0 >>e drop ]
125 [ shape>> 0 >>u drop ] bi ;
127 M: chipmunk-world end-game-world
129 [ cpSpaceFreeChildren ]
132 : chipmunk-demo ( -- )
136 { world-class chipmunk-world }
137 { title "Chipmunk Physics Demo" }
138 { pixel-format-attributes
139 { windowed double-buffered }
141 { pref-dim { 640 480 } }
142 { tick-interval-micros 16666 }