Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

most of physics for moving

  • Loading branch information...
commit 5ec1bdf8baed4175a63e038b2a3144bead41916f 1 parent 32fa1cd
Dan Ballard authored
30 engine.lisp
@@ -3,7 +3,8 @@
3 3 (defclass engine-object (game-object)
4 4 ((start-time :initarg :start-time :accessor start-time :initform 0)
5 5 ;; time till fully active
6   - (activation-time :initarg :activation-time :accessor activation-time :initform 0)))
  6 + (activation-time :initarg :activation-time :accessor activation-time :initform 0)
  7 + (force :initarg :force :accessor force :initform (make-instance 'force))))
7 8
8 9
9 10 (defmethod activate ((object engine-object) start-time)
@@ -37,9 +38,9 @@
37 38 (0.0 0.0 (0 1 2))))
38 39
39 40 (defparameter *thruster-colors*
40   - '(((16 64 2) (0 132 2) (32 164 2))
41   - ((16 64 2) (0 132 2) (32 164 2))
42   - ((16 64 2) (0 132 2) (32 164 2))
  41 + '(((32 64 2) (32 132 2) (32 164 2))
  42 + ((32 64 2) (32 132 2) (32 164 2))
  43 + ((32 64 2) (32 132 2) (32 164 2))
43 44 ((0 255 2) (0 255 2) (64 255 2))))
44 45
45 46
@@ -49,3 +50,24 @@
49 50 (call-next-method))
50 51
51 52
  53 +(defmethod phys-act ((src engine-object) (target game-object) time)
  54 + (let* ((scalar-proj (scalar-proj (vector-scale-1 (direction (force (src)))) (vector-scale-1 (position src))))
  55 + (accel (/ (newtons (force src)) (mass (body target))))
  56 + (accel-vec (scale-vector scalar-proj accel)))
  57 +
  58 +
  59 +
  60 +; time is time elapsed in seconds (with decimal for sub seconds)
  61 +;(defmethod time-step ((engine engine) object time)
  62 +; ; f = ma
  63 +; (let ((accel (/ (force engine) (mass object))))
  64 +; ; x = x +v*t + 1/2 * a * t^2
  65 +; (dotimes (i 3) (progn
  66 +; (incf (aref (coords motion) i)
  67 +; (+ (* (aref (velocity motion) i) time) (* .5 (aref (acceleration motion) i) (expt time 2))))
  68 +; (incf (aref (velocity motion) i)
  69 +; (* time (aref (acceleration motion) i))))))
  70 +
  71 +
  72 +
  73 +
93 flight-sim.lisp
@@ -5,67 +5,6 @@
5 5 ;;; "flight-sim" goes here. Hacks and glory await!
6 6
7 7
8   -
9   -;(defclass engine-object (game-object)
10   -; ((active :initarg :active :reader active :initform nil)
11   -; (start-time :initarg :start-time :reader start-time :initform nil)
12   -; (forces :initarg :forces :accessor forces :initform '())))
13   -
14   -;(defgeneric activate-engine (object engine-sym))
15   -
16   -;(defmethod activate-engine ((object powered-object) engine-sym)
17   -; (push :engine-sym (getf (engines object) :active))
18   -; (engine-start (getf (getf (engines object) :engines) engine-sym) (wall-time)))
19   -
20   -;(defgeneric engine-start (engine time))
21   -;(defmethod engine-start ((engine engine-object) time)
22   -; (setf (slot-value engine 'active) t)
23   -; (setf (slot-value engine 'start-time) time))
24   -
25   -;(defgeneric engine-stop (engine))
26   -;(defmethod engine-stop ((engine engine-object))
27   -; (setf (slot-value engine 'active) nil))
28   -
29   -
30   -; take 2 seconds to fully fire
31   -;(defmethod engine-genmodel ((engine engine-object))
32   -; (let ((time (- (wall-time) (start-time engine))))
33   -; (setf (model engine)
34   -; (make-model-3pyramid (make-2d-array 4 3
35   -; `((0.0 0.5 0.0) (-2.0 -0.5 0.0) (2.0 -0.5 0.0)
36   -; ; z goes from 0 to 1 in 2 seconds
37   -; (0.0 0.0 ,(converge 0 1 2 time))))
38   -; :point-colors (make-2d-array 4 3 `(
39   -; (,(converge 16 64 2 time) ,(converge 0 132 2 time) ,(converge 32 164 2 time))
40   -; (,(converge 16 64 2 time) ,(converge 0 132 2 time) ,(converge 32 164 2 time))
41   -; (,(converge 16 64 2 time) ,(converge 0 132 2 time) ,(converge 32 164 2 time))
42   -; (,(converge 0 255 2 time) ,(converge 0 255 2 time) ,(converge 64 255 2 time))))))))
43   -
44   -
45   -;(defclass powered-object (game-object)
46   -; ;; plist :: ( :objects (plist models) :active (list symbols))
47   -; ((engines :initarg :engines :accessor engines :initform '(:engines () :active ()))))
48   -;; ((engine :initarg :engine :accessor engine :initform nil)))
49   -;
50   -;
51   -; ;(attachments :initarg :attachments :accessor attachments :initform nil)))
52   -
53   -
54   -
55   -
56   -; time is time elapsed in seconds (with decimal for sub seconds)
57   -;(defmethod time-step ((engine engine) object time)
58   -; ; f = ma
59   -; (let ((accel (/ (force engine) (mass object))))
60   -; ; x = x +v*t + 1/2 * a * t^2
61   -; (dotimes (i 3) (progn
62   -; (incf (aref (coords motion) i)
63   -; (+ (* (aref (velocity motion) i) time) (* .5 (aref (acceleration motion) i) (expt time 2))))
64   -; (incf (aref (velocity motion) i)
65   -; (* time (aref (acceleration motion) i))))))
66   -
67   -
68   -
69 8 ;(defmethod time-step ((object powered-object) time)
70 9 ; (loop for engine in (loop for engine-sym in (getf (engines object) :active) collecting (getf (engines engines) engine-sym)) do
71 10 ; (time-step engine object time)))
@@ -73,18 +12,6 @@
73 12
74 13
75 14
76   -
77   -
78   -;(defclass engine ()
79   -; (
80   -
81   -; (make-instance 'model
82   -; :vertices (make-2d-array 4 3 '((0 0 0) (0 1 3) (-2 0 3) (2 0 3)))
83   -; :faces (make-2d-array 4 3 '((0 1 3) (0 2 1) (0 3 2) (1 2 3)))
84   -; :colors (make-2d-array 2 3 '((196 196 196) (32 32 32)))
85   -; :face-colors (make-2d-array 4 3 '((0 0 0) (0 0 0) (0 0 0) (1 1 1)))))
86   -
87   -
88 15 (defparameter *world* nil)
89 16
90 17 (defparameter *self* nil)
@@ -101,17 +28,6 @@
101 28 (defparameter *num-frames* 0)
102 29
103 30
104   -
105   -
106   -;(defmethod object-draw ((object powered-object))
107   -; (draw-entity object)
108   -; (if (eql (active (engine object)) t)
109   -; (progn
110   -; (setf (model (engine object)) (engine-genmodel (engine object)))
111   -; (gl:translate 0 0 0)
112   -; (object-draw (engine object)))))
113   -
114   -
115 31 (defun draw-world (start-time)
116 32 ;; clear the buffer
117 33 (gl:clear :color-buffer-bit :depth-buffer-bit)
@@ -186,6 +102,10 @@
186 102 (setf (aref (acceleration (motion *self*)) 1) 0))
187 103 (otherwise (format t "~a~%" key))))
188 104
  105 +(defun phys-step (time)
  106 + (loop for sym in (active-attachments *self*) do
  107 + (phys-act (getf (attachments *self*) sym) *self* time)))
  108 +
189 109
190 110 (defun sim-step ()
191 111 "draw a frame"
@@ -193,7 +113,7 @@
193 113 (time (- start-time *last-time*)))
194 114
195 115
196   - ; (phys-step time)
  116 + (phys-step time)
197 117 (draw-world start-time)
198 118
199 119
@@ -264,9 +184,10 @@
264 184 :template-colors *thruster-colors*
265 185 :faces (make-2d-array 4 3 '((0 1 3) (0 2 1) (0 3 2) (1 2 3)))
266 186 :face-colors (make-2d-array 4 3 '((0 1 3) (0 2 1) (0 3 2) (1 2 3))))
  187 + :force (make-instance 'force :newtons 10 :direction (vector 0 0 1))
267 188
268 189 :body (make-instance 'body
269   - :coords (vector 0 0.5 3))))))
  190 + :coords (vector 0 0.5 1.5))))))
270 191 ;:engines (list :engines (list :thrust
271 192 ; (make-instance 'engine-object
272 193 ; :motion (make-instance 'motion :coords (vector 0 0.5 3.0))
24 math.lisp
@@ -53,3 +53,27 @@
53 53 (defun rotate-triangle (tri m)
54 54 (make-array (length tri) :initial-contents
55 55 (loop for v across tri collecting (rotate* m v))))
  56 +
  57 +(defun scale-vector (v a)
  58 + (make-array (length v) :initial-contents (loop for i across v collecting (* i a))))
  59 +
  60 +; returns a vector with all elemts scaled to biggest 1 which is scaled to 1
  61 +; e.x. (scale-vector (8 4 2)) -> (1 .5 .25)
  62 +(defun scale-vector-1 (v)
  63 + (let ((max (loop for i across v maximize i into result finally (return result))))
  64 + (make-array (length v) :initial-contents (loop for i across v collecting (float (/ i max))))))
  65 +
  66 +(defun dot (v1 v2)
  67 + (loop for i from 0 to (1- (length v1)) summing (* (aref v1 i) (aref v2 i))))
  68 +
  69 +(defun vector-length (v)
  70 + (sqrt (dot v v)))
  71 +
  72 +(defun scalar-proj (vector direction)
  73 + (let ((length (vector-length direction)))
  74 + (if (eql 0 length)
  75 + 0
  76 + (/ (dot vector direction) length))))
  77 +
  78 +(defun vector- (v1 v2)
  79 + (make-array (length v1) :initial-contents (loop for i from 0 to (1- (length v1)) collecting (- (aref v1 i) (aref v2 i)))))
2  model.lisp
@@ -65,6 +65,6 @@
65 65 (make-2d-array 4 3 '((0 1 3) (0 2 1) (0 3 2) (1 2 3))))))
66 66
67 67 (defparameter *ship-model*
68   - (make-model-3pyramid (make-2d-array 4 3 '((0.0 0.0 0.0) (0.0 1.0 3.0) (-2.0 0.0 3.0) (2.0 0.0 3.0)))
  68 + (make-model-3pyramid (make-2d-array 4 3 '((0.0 -0.5 -1.5) (0.0 0.5 1.5) (-2.0 -0.5 1.5) (2.0 -0.5 1.5)))
69 69 :face-colors (make-2d-array 4 3 '((196 196 196) (196 196 196) (196 196 196) (32 32 32)))))
70 70
3  objects.lisp
@@ -39,3 +39,6 @@
39 39 (draw (model object) time)
40 40 (loop for a in (active-attachments object) do
41 41 (draw (getf (attachments object) a) time)))
  42 +
  43 +
  44 +(defgeneric phys-act (src target time))
1  physics.lisp
@@ -16,3 +16,4 @@
16 16 (defclass force ()
17 17 ((newtons :initarg :newtons :accessor newtons :initform 0)
18 18 (direction :initarg :direction :accessor direction :initform (vector))))
  19 +

0 comments on commit 5ec1bdf

Please sign in to comment.
Something went wrong with that request. Please try again.