Skip to content

Commit

Permalink
Added spring and player classes. Apperenly fixed the no disabling of …
Browse files Browse the repository at this point in the history
…physics objects issue somewhere along the way.
  • Loading branch information
warweasle committed Jun 6, 2015
1 parent 58e0195 commit 29b099d
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 23 deletions.
4 changes: 3 additions & 1 deletion clode.asd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
(:file "box")
(:file "plane")
(:file "ray")
(:file "spring")
(:file "clode-node")
(:file "shapes")))))
(:file "shapes")
(:file "player")))))


4 changes: 2 additions & 2 deletions clode/bindings.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,8 @@

(defcfun-rename-function "dGeomRayGet" :void
(ray dGeomID)
(start dVector3)
(dir dVector3))
(start :pointer)
(dir :pointer))


(defcfun-rename-function "dGeomRaySetParams" :void
Expand Down
6 changes: 6 additions & 0 deletions clode/object.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,9 @@
do (joint-attach
(joint-create-contact *physics-world* *physics-contact-group* (cffi:mem-aptr contact '(:struct clode::dContact) x))
b1 b2))))))))


(defmethod enabled ((this physics-object))
(let ((pbody (pointer (body this))))

(eq 1 (Body-Is-Enabled pbody))))
4 changes: 4 additions & 0 deletions clode/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#:physics-init
#:physics-step
#:physics-uninit
#:body #:pointer
#:enabled

#:BOX-INDEXES #:BOX-VERTEXES #:BOX-NORMALS #:BOX-TEXCOORDS #:SPHERE-INDEXES
#:SPHERE-VERTEXES #:SPHERE-NORMALS
Expand All @@ -38,4 +40,6 @@
#:set-position
#:close-callback
#:update
#:ray-length #:physics-spring #:springiness #:damping
#:physics-player
))
35 changes: 16 additions & 19 deletions clode/player.lisp
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
(in-package #:clode)

(defclass spring ()
())

(defclass physics-player ()
((head :initform nil
:initarg :head
:reader head)
(legs :initform nil
:initarg :legs
:reader legs)
(up :initform '(0 1 0)
:initarg :up
:reader up-vector)
(direction :initform '(1 0 0)
:initarg :direction
:reader direction-vector)
(max-speed :initform 1
:initarg :max-speed
:accessor max-speed)))
((body :initform (error "You must pass a physics-body to a physics-player object!")
:initarg :body
:reader body)))


(defmethod initialize-instance :after ((this physics-player) &key)

(let ((pbody (clode::pointer (clode::body this))))

(clode:body-set-max-angular-speed pbody 0)
(clode::body-set-kinematic pbody)
(clode:Body-Set-Auto-Disable-Flag pbody 0)
(clode:Body-Set-Angular-Damping pbody 0)
(clode:Body-Set-linear-Damping pbody 0)))


18 changes: 17 additions & 1 deletion clode/ray.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
(when (body this)
(clode:geom-set-body (geometry this) (pointer (body this)))))

(defmethod ray-length ((this physics-ray) &key)
(geom-ray-get-length (geometry this)))

(defmethod (setf transform) ((length number) (this physics-ray))
(geom-ray-set-length (geometry this) length))

(defmethod close-callback ((this physics-ray) (that physics-object))

(let* ((o1 (geometry this))
Expand All @@ -33,8 +39,9 @@
;;(format t "CLODE:ray-callback b1 = ~A~%" distance)

(let* ((vel (body-get-linear-vel b1))
(len (ray-length this))
(x (+ (* .90 (aref vel 1))
(* 1/2 (abs (- (abs distance) 3))))))
(* 1/2 (abs (- (abs distance) len))))))
;; (+ (*
;; (abs (* 1/25 (max 0 (- distance 1.5))))))))

Expand All @@ -46,3 +53,12 @@
(defmethod close-callback ((this physics-object) (that physics-ray))
(close-callback that this))

(defmethod Ray-Get ((this physics-ray))

(cffi:with-foreign-objects ((start 'dVector3)
(dir 'dVector3))

(Geom-Ray-Get (geometry this) start dir)
(values (cffi:convert-from-foreign start 'dVector3)
(cffi:convert-from-foreign dir 'dVector3))))

54 changes: 54 additions & 0 deletions clode/spring.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
(in-package #:clode)


(defclass physics-spring (physics-ray)
((springiness :initform 1
:initarg :springiness
:accessor springiness)
(damping :initform .1
:initarg :damping
:accessor damping)))





(defmethod close-callback ((this physics-spring) (that physics-object))

(let* ((o1 (geometry this))
(o2 (geometry that))
(b1 (geom-get-body o1))
(b2 (geom-get-body o2)))

(with-foreign-object (contact '(:struct dContact) *physics-max-contacts*)
(let ((gg (foreign-slot-pointer contact '(:struct ode::dContact) 'ode::geom)))


(let ((num-contacts (collide o1 o2 *physics-max-contacts* gg (foreign-type-size '(:struct ode::dContact)))))
(unless (zerop num-contacts)

(let ((distance (abs (foreign-slot-value gg '(:struct ode::dContactGeom) 'ode::depth))))

;;(format t "CLODE:ray-callback b1 = ~A~%" distance)
(multiple-value-bind (start dir) (Ray-Get this)
;;(format t "spring dir: ~A start: ~A~%" dir start)


(let* ((vel (body-get-linear-vel b1))
(len (ray-length this))
(x (+ (* (- 1 (damping this)) (aref vel 1))
(* (springiness this) (abs (- (abs distance) len))))))
;; (normal-dir (sb-cga:dot-product (apply #'clinch:make-vector
;; (subseq (map 'list (lambda (x)
;; (coerce x 'single-float))
;; dir) 0 3))
;; (apply #'clinch:make-vector
;; (subseq (map 'list (lambda (x)
;; (coerce x 'single-float))
;; vel) 0 3)))))

(clode:body-set-linear-vel b1 (aref vel 0) x (aref vel 2)))))))))))


(defmethod close-callback ((this physics-object) (that physics-ray))
(close-callback that this))

0 comments on commit 29b099d

Please sign in to comment.