next up previous
Next: 3.5 シミュレーションの例(2) Up: 3 Euslispクラスシステム Previous: 3.3 インスタンスの生成

3.4 シミュレーションの例(1)

例として宇宙空間において浮遊している物と ロケットの運動をシミュレートするものを紹介します.

(defvar *gravity* '(0 4))
(defvar *initial-height* 25)
(defvar *asteroid-width* 25)

(defmacro pname-part (particle-info)
         `(elt ,particle-info 0))
(defmacro x-pos (particle-info)
         `(elt ,particle-info 1))
(defmacro y-pos (particle-info)
         `(elt ,particle-info 2))
(defmacro x-speed-part (particle-info)
         `(elt ,particle-info 3))
(defmacro y-speed-part (particle-info)
         `(elt ,particle-info 4))

(defun make-something (typ particle-info)
  (let ((x (instance typ :init)))
    (send x 'set-pname (pname-part particle-info))
    (send x 'set-x-position (x-pos particle-info))
    (send x 'set-y-position (y-pos particle-info))
    (send x 'set-x-speed (x-speed-part particle-info))
    (send x 'set-y-speed (y-speed-part particle-info))
    x))

(defun create-universe1 (&optional (cnt 5))
  (let ((p1 (make-something particle '(p1 4 3 2 5)))
        (p2 (make-something particle '(p2 0 0 0 1)))
        (p3 (make-something particle '(p3 1 1 9 4)))
        (r1 (make-something rocket '(r1 2 3 4 3)))
        (r2 (make-something rocket '(r2 7 5 2 1)))
        (r3 (make-something rocket '(r3 2 4 4 2))))
    (send r1 'set-motor '(2 1))
    (send r2 'set-motor '(7 6))
    (send r3 'set-motor '(2 3))
    (dotimes (i cnt)
             (format t "~%~%cnt=~s" i)
             (dolist (obj (list p1 p2 p3 r1 r2 r3))
                     (send obj 'status)
                     (send obj 'new-position)
                     (send obj 'new-velocity)))))
ここでは,3つの浮遊物(particle)と3つのロケット(rocket)があります. rocketはparticleの属性をもつために,particleのメソッドを利用する ことができます. ここでのシミュレーションというのは,create-universe1の引数として, 実行単位時間数を与えることで,その時間だけたつことをシミュレーションし ます. その単位時間がたつごとに,3つのパーティクルと3つのロケットの状態を 表示することを行ないます.パーティクルの状態変数としては,場所(Pos),速度 (Vel),加速度(Acc)です.ロケットの状態は,そのパーティクルと同じ状態変 数に加えて推進力(Motor)があります. この状態変数を表示する手続きは,particleとrocketのそれぞれのクラスに statusという名のメソッドとして定義されています. ロケット(r1,r2,r3)にstatusメソッドが送られた時には,パーティクルの statusメソッドではなく,ロケットのstatusメソッドが実行されます.

cnt=0
P1's Pos:(4 3) Vel:(2 5) Acc:(0 4)
P2's Pos:(0 0) Vel:(0 1) Acc:(0 4)
P3's Pos:(1 1) Vel:(9 4) Acc:(0 4)
R1's Pos:(2 3) Vel:(4 3) Acc:(2 5) Motor:(2 1)
R2's Pos:(7 5) Vel:(2 1) Acc:(7 10) Motor:(7 6)
R3's Pos:(2 4) Vel:(4 2) Acc:(2 7) Motor:(2 3)

cnt=1
P1's Pos:(6 8) Vel:(2 9) Acc:(0 4)
P2's Pos:(0 1) Vel:(0 5) Acc:(0 4)
P3's Pos:(10 5) Vel:(9 8) Acc:(0 4)
R1's Pos:(6 6) Vel:(6 8) Acc:(2 5) Motor:(2 1)
R2's Pos:(9 6) Vel:(9 11) Acc:(7 10) Motor:(7 6)
R3's Pos:(6 6) Vel:(6 9) Acc:(2 7) Motor:(2 3)

cnt=2
P1's Pos:(8 17) Vel:(2 13) Acc:(0 4)
P2's Pos:(0 6) Vel:(0 9) Acc:(0 4)
P3's Pos:(19 13) Vel:(9 12) Acc:(0 4)
R1's Pos:(12 14) Vel:(8 13) Acc:(2 5) Motor:(2 1)
R2's Pos:(18 17) Vel:(16 21) Acc:(7 10) Motor:(7 6)
R3's Pos:(12 15) Vel:(8 16) Acc:(2 7) Motor:(2 3)

cnt=3
P1's Pos:(10 30) Vel:(2 17) Acc:(0 4)
P2's Pos:(0 15) Vel:(0 13) Acc:(0 4)
P3's Pos:(28 25) Vel:(9 16) Acc:(0 4)
R1's Pos:(20 27) Vel:(10 18) Acc:(2 5) Motor:(2 1)
R2's Pos:(34 38) Vel:(23 31) Acc:(7 10) Motor:(7 6)
R3's Pos:(20 31) Vel:(10 23) Acc:(2 7) Motor:(2 3)

cnt=4
P1's Pos:(12 47) Vel:(2 21) Acc:(0 4)
P2's Pos:(0 28) Vel:(0 17) Acc:(0 4)
P3's Pos:(37 41) Vel:(9 20) Acc:(0 4)
R1's Pos:(30 45) Vel:(12 23) Acc:(2 5) Motor:(2 1)
R2's Pos:(57 69) Vel:(30 41) Acc:(7 10) Motor:(7 6)
R3's Pos:(30 54) Vel:(12 30) Acc:(2 7) Motor:(2 3)
NIL
ロケットの推進力は,変えていません.加速度はすべてのパーティクル,ロケッ トで一定です.加速度の大きさに応じて速度と位置が変化しているのがわかり ます.

generated through LaTeX2HTML. M.Inaba 平成18年5月6日