flight-sim/objects.lisp

57 lines
2.1 KiB
Common Lisp
Raw Normal View History

(in-package #:flight-sim)
(defclass game-object ()
((model :initarg :model :accessor model :initform (make-instance 'model))
(body :initarg :body :accessor body :initform (make-instance 'body))
(attachments :initarg :attachments :accessor attachments :initform '())
(active-attachments :initarg :active-attachments :accessor active-attachments :initform '())))
;(defmethod coords ((object game-object))
; (coords (body object)))
(defgeneric activate (object start-time))
(defgeneric activate-attachment (object sym start-time))
(defgeneric deactivate (object))
(defgeneric deactivate-attachment (object sym))
(defmethod activate-attachment ((object game-object) sym start-time)
2011-08-03 05:55:24 +00:00
(push sym (active-attachments object))
(activate (getf (attachments object) sym) start-time))
2011-08-03 05:55:24 +00:00
(defmethod deactivate-attachment ((object game-object) sym)
(setf (active-attachments object) (remove sym (active-attachments object))))
2011-08-03 05:55:24 +00:00
(defmethod draw :before ((object game-object) time)
(gl:push-matrix)
(gl:translate (aref (coords (body object)) 0) (aref (coords (body object)) 1) (aref (coords (body object)) 2))
(gl:rotate (aref (angles (body object)) 0) 1 0 0)
(gl:rotate (aref (angles (body object)) 1) 0 1 0)
(gl:rotate (aref (angles (body object)) 2) 0 0 1))
2011-08-03 05:55:24 +00:00
(defmethod draw :after ((object game-object) time)
(gl:pop-matrix))
2011-08-03 05:55:24 +00:00
(defmethod draw ((object game-object) time)
(draw (model object) time)
(loop for a in (active-attachments object) do
(draw (getf (attachments object) a) time)))
2011-08-13 15:10:01 +00:00
2011-08-13 19:09:25 +00:00
;; get the vector of accel src object exerts on target object
(defgeneric get-accel (src target))
(defgeneric apply-accel (object accel time))
; time is time elapsed in seconds (with decimal for sub seconds)
(defmethod apply-accel ((object game-object) accel time)
; x = x +v*t + 1/2 * a * t^2
(dotimes (i 3) (progn
(incf (aref (coords (body object)) i)
2011-08-20 17:16:46 +00:00
(+ (* (aref (velocity (motion (body object))) i) time) (* .5 (aref accel i) (expt time 2))))
(incf (aref (velocity (motion (body object))) i)
2011-08-13 19:09:25 +00:00
(* time (aref accel i))))))
2011-08-13 15:10:01 +00:00