-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.fs
More file actions
104 lines (70 loc) · 1.99 KB
/
test.fs
File metadata and controls
104 lines (70 loc) · 1.99 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
module test
open System.Drawing
open System.Windows.Forms
open fsphys
open fsphys.geom
let ri,rd =
let r = System.Random()
r.Next, r.NextDouble
let box w h =
let w = w/2.
let h = h/2.
phys.def.poly [| vec(w,h); vec(w,-h); vec(-w,-h); vec(-w,h) |]
let randshape () =
let n = float (4 + ri 4)
phys.def.poly
[| for i in 1. .. n ->
(30.+rd()*20.) .* vpolar(-i/n*6.28)
|]
let circle () =
phys.def.circle 20.
let fps = 40.
let init () =
let p = phys.init <| phys.default_cfg fps
List.iter (p.add_body>>ignore)
[ for _ in 0 .. 30 do
let pos = vec(rd()*700., rd()*200.)
yield (pos,0.), phys.def.body 1. [randshape ()]
yield
(vec(350., 450.),0.), phys.def.body_static [box 700. 50.]
]
p
let p = ref (init ())
let update () = (!p).update()
let draw (g:Graphics) =
let d = (!p).dump
let line = new Pen(Brushes.Black)
let green = Brushes.Green
let pointf (v:vec) = PointF(float32 v.x, float32 v.y)
g.Clear(Color.White)
for b in d.bodies do
for s in b.shapes do
match s with
| shape.Poly p ->
g.DrawPolygon(line,
[| for v in p.verts -> pointf v |] )
| shape.Circle c ->
let r = vec(c.radius,c.radius)
g.DrawEllipse(line,
RectangleF(pointf (c.center -| r), SizeF(pointf (2. .* r))) )
for ct in d.contacts.Values do
let r = let s = 5. in vec(s,s)
g.FillEllipse(green,
RectangleF(pointf (ct.p-|0.5.*r), SizeF(pointf r)) )
type Main =
inherit Form
new() as w = {} then
w.Width <- 700
w.Height <- 500
w.DoubleBuffered <- true
let l = new Label()
l.Text <- "Click to reset"
w.Controls.Add l
let t = new Timer()
t.Interval <- 1000/int fps
t.Start()
w.Closed.Add (fun _ -> t.Stop())
t.Tick.Add (fun _ -> update(); w.Invalidate())
w.Paint.Add (fun e -> try draw e.Graphics with _ -> ())
w.Click.Add (fun _ -> p := init ())
do (new Main()).ShowDialog() |> ignore