forked from gregcman/sucle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
closphys.lisp
414 lines (352 loc) · 13.2 KB
/
closphys.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
(defpackage #:physics
(:use #:cl)
(:import-from #:utility
#:dobox
#:with-vec
#:floatify
#:etouq
#:once-only
#:dohash
#:%list
#:toggle)
(:export))
(in-package #:physics)
;;;; Utilities
;; Access nsb-cga:vec through wrapper macros in case the
;; implementation of vectors changes
(deftype vec () 'nsb-cga:vec)
(defparameter *temp-vec* (nsb-cga:vec 0.0 0.0 0.0))
(defmacro modify (fun a &rest rest)
(once-only (a)
`(,fun ,a ,a ,@rest)))
(defun vec-magnitude (vec)
(nsb-cga:vec-length vec))
(defmacro vec-setf (place x y z)
`(progn
(setf (vec-x ,place) ,x)
(setf (vec-y ,place) ,y)
(setf (vec-z ,place) ,z)))
(defmacro vec-incf (place delta)
`(modify nsb-cga:%vec+
,place
,delta))
(defmacro vec-decf (place delta)
`(modify nsb-cga:%vec-
,place
,delta))
(defun set-temp-vec (x y z &optional (temp-vec *temp-vec*))
(with-vec (a b c) (temp-vec symbol-macrolet)
(setf a x
b y
c z))
temp-vec)
(defmacro vec (x y z)
`(nsb-cga:vec ,x ,y ,z))
(defmacro vec* (a f &optional (temp-vec '*temp-vec*))
`(nsb-cga:%vec* ,temp-vec ,a ,f))
(defmacro vec+ (a f &optional (temp-vec '*temp-vec*))
`(nsb-cga:%vec+ ,temp-vec ,a ,f))
(defmacro vec- (a f &optional (temp-vec '*temp-vec*))
`(nsb-cga:%vec- ,temp-vec ,a ,f))
(defun vec-x (vec)
(aref vec 0))
(defun vec-y (vec)
(aref vec 1))
(defun vec-z (vec)
(aref vec 2))
(defun (setf vec-x) (new vec)
(setf (aref vec 0) new))
(defun (setf vec-y) (new vec)
(setf (aref vec 1) new))
(defun (setf vec-z) (new vec)
(setf (aref vec 2) new))
(define-modify-macro *= (&rest args) *)
(define-modify-macro logiorf (&rest args) logior)
;;;; Classes
(defclass has-position ()
((position :type vec
:initarg :pos
:initform (vec 0.0 0.0 0.0)
:accessor pos))
(:documentation "An object with a world position"))
(defclass has-physics (has-position)
((velocity :type vec
:initarg :velocity
:initform (vec 0.0 0.0 0.0)
:accessor velocity)
(acceleration :type vec
:initform (vec 0.0 0.0 0.0)
:accessor acceleration))
(:documentation "An object that moves through the world"))
(defclass has-mass ()
((mass :type float
:initarg :mass
:accessor mass))
(:documentation "An object with a mass"))
(defmethod mass (object) "Default mass of 1 unit" 1)
;; not useful without a more in-depth physics engine
#+nil(defmethod apply-force ((object has-mass) force)
(vec-incf (acceleration object)
(vec* force (mass object))))
(defgeneric apply-impulse (object impulse dt))
(defmethod apply-impulse ((object has-mass) impulse dt)
(vec-incf (velocity object)
(vec* impulse (/ dt (mass object)))))
(defclass has-drag ()
((drag-coefficient :type float
:initform 0.0003
:initarg :drag-coefficient
:accessor drag-coefficient))
(:documentation "An object which experiences drag"))
(defmethod acceleration :around ((entity has-drag))
"Apply acceleration due to drag before returning acceleration"
(let* ((velocity (velocity entity))
(speed (vec-magnitude velocity))
(k (drag-coefficient entity)))
(vec- (call-next-method)
(vec* velocity
(/ (* k (expt speed 2))
(mass entity))))))
(defvar *default-acceleration-due-to-gravity* (vec 0.0 -13.0 0.0))
(defclass has-gravity ()
((gravity-p :type boolean
:initform t
:accessor gravity-p))
(:documentation "An object whose motion is affected by gravity"))
(defmethod acceleration-due-to-gravity ((object has-gravity))
*default-acceleration-due-to-gravity*)
;; method on the acceleration accessor function. Doesn't implement the
;; frame of gravity lag, but I'll figure that out later.
(defmethod acceleration :around ((object has-gravity))
"Apply acceleration due to gravity before returning acceleration"
(if (gravity-p object)
(vec+ (acceleration-due-to-gravity object)
(call-next-method object))
(call-next-method object)))
(defclass has-aabb ()
((aabb :type aabb
:initarg :aabb
:accessor aabb))
(:documentation "An object with an axis alligned bounding box"))
(defun create-aabb (&optional (maxx 1.0) (maxy maxx) (maxz maxx)
(minx (- maxx)) (miny (- maxy)) (minz (- maxz)))
(aabbcc:make-aabb
:minx minx
:maxx maxx
:miny miny
:maxy maxy
:minz minz
:maxz maxz))
(defparameter *block-aabb*
;;;;1x1x1 cube
(create-aabb 1.0 1.0 1.0 0.0 0.0 0.0))
(defun block-to-block-aabb (blockid)
(declare (ignore blockid))
;;FIXME :use defmethod on objects?
*block-aabb*)
(defclass has-world-collision (has-aabb)
((world-contact :type (integer 0 63)
:initform 0
:accessor world-contact)
(clip-p :type boolean
:initform t
:accessor clip-p))
(:documentation "An object whose motion is interrupted by blocks in
the world"))
(defun on-ground (entity)
(when (typep entity 'has-world-collision)
(logtest (world-contact entity) 4)))
;; (defclass point-projectile (has-physics has-drag) ()
;; (:documentation "An object "))
(defclass entity (has-physics
has-drag
has-mass
has-world-collision)
())
(defclass living-entity (entity)
((neck-yaw :type float
:initform 0.0
:accessor neck-yaw)
(neck-pitch :type float
:initform 0.0
:accessor neck-pitch)
(hips :type (or nil float)
:initform nil
:accessor hips)
(movement-speed :type float
:accessor movement-speed
:allocation class)
(step-power :type float
:accessor step-power
:allocation class)))
(defclass player-entity (living-entity)
((movement-speed :initform 4.317)
(step-power :initform 4.0)
(fly-p :type boolean
:initform nil
:accessor fly-p)
(sneak-p :type boolean
:initform nil
:accessor sneak-p)))
;;;; Physics system
(defgeneric step-physics (entity dt)
(:documentation "Entry point to the physics system. Modify ENTITY to
have experienced DT seconds"))
(defmethod step-physics (entity dt))
(defgeneric target-velocity (entity))
(defmethod step-physics :before ((entity has-world-collision) dt)
"Before physics is calculated, find how ENTITY is in contact
with the world and store that information in ENTITY"
(with-vec (x y z) ((pos entity))
(setf (world-contact entity) (find-blocks-in-contact-with
x y z (aabb entity)))))
(defun find-blocks-in-contact-with (px py pz aabb)
(let ((acc #b000000))
(aabbcc:get-blocks-around (px py pz aabb) (mx my mz contact-var)
(declare (ignorable contact-var))
(let ((blockid (world:getblock mx my mz)))
(when (block-data:data blockid :hard)
(logiorf acc (aabbcc:aabb-contact px py pz aabb mx my mz
(block-to-block-aabb blockid))))))
acc))
(defmethod step-physics :around ((entity has-physics) dt)
"Run all physics methods, then step
velocity and positon"
(call-next-method entity dt)
(step-velocity entity (vec* (acceleration entity) dt))
(step-position entity (vec* (velocity entity) dt)))
(defmethod step-physics ((entity player-entity) dt)
())
(defmethod target-velocity ((entity player-entity))
(let* ((flying (fly-p entity))
(yaw (neck-yaw entity))
(dir (hips entity))
(speed (movement-speed entity))
(yvalue (case flying
(:up speed)
(:down (- speed))
(t 0.0))))
(if dir
(let ((diraux (+ dir yaw)))
(nsb-cga:vec
(* speed (- (sin diraux)))
yvalue
(* speed (cos diraux))))
(nsb-cga:vec 0.0 yvalue 0.0))))
(defun apply-movement-impulse (current-velocity target-velocity step-power dt)
(let* ((delta-velocity (nsb-cga:vec-
target-velocity
current-velocity))
(dv-magnitude (nsb-cga:vec-length delta-velocity)))
(unless (zerop dv-magnitude)
(let ((bump-direction (nsb-cga:vec/
delta-velocity
dv-magnitude)))
(let ((step-force (* 2.0 dv-magnitude)))
(apply-impulse current-velocity
(vec* bump-direction
(* step-power step-force))
dt))))))
(defgeneric step-position (entity delta)
(:documentation "Increment ENTITY's position by DELTA, taking
collision in to account"))
(defmethod step-position (entity delta)
(vec-incf (pos entity) delta))
(defmethod step-position ((entity has-world-collision) delta)
"Increment ENTITY's position by delta, clamping position and
velocity to prevent clipping with the world"
(if (not (clip-p entity))
(vec-incf (pos entity) delta)
(with-vec (px py pz) ((pos entity))
(with-vec (dx dy dz) (delta)
(let ((dead-axis #b000)
(clamp #b000)
(aabb (aabb entity)))
(flet ((deadify (x bit)
(when (zerop x)
(logiorf dead-axis bit))))
(deadify dx #b100)
(deadify dy #b010)
(deadify dz #b001))
(step-recursive entity px py pz dx dy dz clamp dead-axis aabb))))))
(defun step-recursive (entity px py pz dx dy dz clamp dead-axis aabb)
(if (= #b111 dead-axis)
(progn
(vec-setf (velocity entity) 0 0 0)
(vec-setf (pos entity) px py pz))
(multiple-value-bind (newclamp ratio)
(collect-touch px py pz dx dy dz aabb)
(let ((whats-left (- 1 ratio)))
(macrolet ((axis (pos delta bit)
`(unless (logtest ,bit dead-axis)
(incf ,pos (* ratio ,delta))
(if (logtest ,bit newclamp)
(setf ,delta 0)
(*= ,delta whats-left)))))
(axis px dx #b100)
(axis py dy #b010)
(axis pz dz #b001))
(if (>= 0 whats-left)
(progn (vec-setf (pos entity) px py pz)
(when (logtest #b100 clamp)
(setf (vec-x (velocity entity)) 0))
(when (logtest #b010 clamp)
(setf (vec-y (velocity entity)) 0))
(when (logtest #b001 clamp)
(setf (vec-z (velocity entity)) 0)))
(step-recursive entity
px py pz
dx dy dz
(logior clamp newclamp)
(logior dead-axis newclamp)
aabb))))))
(defun collect-touch (px py pz dx dy dz aabb)
(aabbcc:with-touch-collector (collect-touch collapse-touch min-ratio)
;;[FIXME] aabb-collect-blocks does not check
;;slabs, only blocks upon entering. also check
;;"intersecting shell blocks?"
(aabbcc:aabb-collect-blocks (px py pz dx dy dz aabb)
(x y z contact)
(declare (ignorable contact))
(let ((blockid (world:getblock x y z)))
(when (block-data:data blockid :hard)
(multiple-value-bind (minimum type)
(aabbcc:aabb-collide aabb
px py pz
(block-to-block-aabb blockid)
x y z
dx dy dz)
(collect-touch minimum type)))))
(values (collapse-touch dx dy dz) min-ratio)))
(defgeneric step-velocity (entity delta)
(:documentation "Increment ENTITY's velocity by DELTA, taking
collision in to account"))
(defmethod step-velocity (entity delta)
(vec-incf (velocity entity) delta))
(defmethod step-velocity :after ((entity has-world-collision) delta)
"Nullify ENTITY's velocity where it is in contact with the world"
(declare (ignore delta))
(let ((contact-state (world-contact entity)))
(flet ((nullify-velocity-where-obstructed (velocity i+ i- j+ j- k+ k-)
;;velocity is a 3 float array.
;;If we are moving along an axis, but the contact
;;state says that direction is obstructed, then
;;set the physical motion along that direction to 0
(with-vec (xvel yvel zvel) (velocity symbol-macrolet)
(when (or (and (plusp xvel) i+)
(and (minusp xvel) i-))
(setf xvel 0.0))
(when (or (and (plusp yvel) j+)
(and (minusp yvel) j-))
(setf yvel 0.0))
(when (or (and (plusp zvel) k+)
(and (minusp zvel) k-))
(setf zvel 0.0)))))
(nullify-velocity-where-obstructed
(velocity entity)
(logtest contact-state #b100000)
(logtest contact-state #b010000)
(logtest contact-state #b001000)
(logtest contact-state #b000100)
(logtest contact-state #b000010)
(logtest contact-state #b000001)))))