From a2879d45edd4ac8ff904371aae1072c0133f229b Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Sat, 20 Aug 2011 10:16:46 -0700 Subject: [PATCH] forward motion! --- engine.lisp | 8 ++++---- flight-sim.lisp | 6 +++--- math.lisp | 7 +++++-- objects.lisp | 4 ++-- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/engine.lisp b/engine.lisp index f1cd482..fa8a97c 100644 --- a/engine.lisp +++ b/engine.lisp @@ -35,7 +35,7 @@ (defparameter *thruster-vertices* '((0.0 0.5 0.0) (-2.0 -0.5 0.0) (2.0 -0.5 0.0) ; z goes from 0 to 1 in 2 seconds - (0.0 0.0 (0 1 2)))) + (0.0 0.0 (0 1.5 2)))) (defparameter *thruster-colors* '(((32 64 2) (32 132 2) (32 164 2)) @@ -51,9 +51,9 @@ (defmethod get-accel ((src engine-object) (target game-object)) - (let* ((scalar-proj (scalar-proj (scale-vector-1 (direction (force src))) (scale-vector-1 (coords (body src))))) - (accel (/ (newtons (force src)) (mass (body target)))) - (accel-vec (scale-vector scalar-proj accel))) + (let* ((force-used (scalar-proj (scale-vector-1 (direction (force src))) (scale-vector-1 (coords (body src))))) + (accel (/ (* (newtons (force src)) force-used) (mass (body target)))) + (accel-vec (scale-vector (scale-vector-1 (direction (force src))) (- accel)))) accel-vec)) diff --git a/flight-sim.lisp b/flight-sim.lisp index bd688b8..4237cd9 100644 --- a/flight-sim.lisp +++ b/flight-sim.lisp @@ -175,7 +175,7 @@ (setf *self* (make-instance 'game-object - :body (make-instance 'body :coords (vector 0 0 11)) + :body (make-instance 'body :coords (vector 0 0 11) :mass 1000) :model *ship-model* :attachments (list :thruster @@ -186,10 +186,10 @@ :template-colors *thruster-colors* :faces (make-2d-array 4 3 '((0 1 3) (0 2 1) (0 3 2) (1 2 3))) :face-colors (make-2d-array 4 3 '((0 1 3) (0 2 1) (0 3 2) (1 2 3)))) - :force (make-instance 'force :newtons 10 :direction (vector 0 0 1)) + :force (make-instance 'force :newtons 10000 :direction (vector 0 0 1)) :body (make-instance 'body - :coords (vector 0 0.5 1.5)))))) + :coords (vector 0 0 1.5)))))) ;:engines (list :engines (list :thrust ; (make-instance 'engine-object ; :motion (make-instance 'motion :coords (vector 0 0.5 3.0)) diff --git a/math.lisp b/math.lisp index 367b1c3..428fd9e 100644 --- a/math.lisp +++ b/math.lisp @@ -60,7 +60,7 @@ ; returns a vector with all elemts scaled to biggest 1 which is scaled to 1 ; e.x. (scale-vector (8 4 2)) -> (1 .5 .25) (defun scale-vector-1 (v) - (let ((max (loop for i across v maximize i into result finally (return result)))) + (let ((max (loop for i across v maximize (abs i) into result finally (return result)))) (make-array (length v) :initial-contents (loop for i across v collecting (float (/ i max)))))) (defun dot (v1 v2) @@ -76,4 +76,7 @@ (/ (dot vector direction) length)))) (defun vector- (v1 v2) - (make-array (length v1) :initial-contents (loop for i from 0 to (1- (length v1)) collecting (- (aref v1 i) (aref v2 i))))) \ No newline at end of file + (make-array (length v1) :initial-contents (loop for i from 0 to (1- (length v1)) collecting (- (aref v1 i) (aref v2 i))))) + +(defun vector+ (v1 v2) + (make-array (length v1) :initial-contents (loop for i from 0 to (1- (length v1)) collecting (+ (aref v1 i) (aref v2 i))))) \ No newline at end of file diff --git a/objects.lisp b/objects.lisp index 7117f4b..31eb13e 100644 --- a/objects.lisp +++ b/objects.lisp @@ -50,7 +50,7 @@ ; x = x +v*t + 1/2 * a * t^2 (dotimes (i 3) (progn (incf (aref (coords (body object)) i) - (+ (* (aref (velocity (body object)) i) time) (* .5 (aref accel i) (expt time 2)))) - (incf (aref (velocity (body object)) i) + (+ (* (aref (velocity (motion (body object))) i) time) (* .5 (aref accel i) (expt time 2)))) + (incf (aref (velocity (motion (body object))) i) (* time (aref accel i))))))