Here is the latest sample of the physics code for sonic. I though it would be 'safe' to hide all our source code until the game is a little farther along. Also, for reasons of program secrecy, I'd like to ask that no one copy the code outside of this hidden area.
Revised 03.31.2005...
History:
03.31.2005 - Added jump vectors for 32 surface angles
Keys:
Left - accelerate left
Right - accelerate right
Up - simulates a jump on a pre-set incline
Down - instantly go right
Code: #include "crash82.inc"
.db "Physics Demo",0
#DEFINE NO_MOD_AX ; Uniform sprite height (8 pixels)
DefaultSpriteHeight = 8
terminal_vel_plus = 127 ; Max speed for going down or right?
terminal_vel_minus = - 127 ; Max speed for going up or left?
jump_strength = -64 ; Jumping power (-127 max; must be negative)
slope_angle = TEXT_MEM ; Slope of current hill (0-62; evens only)
ROM_CALL(CLEARLCD)
ld a,0 ; Simulate 0-degree incline
ld (slope_angle),a
top:
;---Key scans (Sonic 3 used this order: left, up, right, down)
ld a,%11111111 ; Clear KEY port
out (1),a ;
ld a,%11111110 ; Mask out irrelevant keys
out (1),a
in a,(1) ; Get keys
bit 1,a ; < Check for LEFT
jr z,trymove_left
bit 3,a ; < Check for UP
jr z,jump
bit 2,a ; < Check for RIGHT
jr z,trymove_right
bit 0,a ; < Check for DOWN
jr z,dash
;---No keys pressed?
xor a
ld (x_accel),a
action_loop:
call Motion
call Display
ld a,(y_pos)
cp 64 - 8
jr nz,top
ld a,-2
ld (y_vel),a
jr top
trymove_left:
ld a,-1 ; Attempt left-wise acceleration
ld (x_accel),a
jr action_loop
trymove_right:
ld a,1 ; Attempt right-wise accleration
ld (x_accel),a
jr action_loop
jump:
; <-- ** Check if you're touching the ground here
; <-- ** Insert collision detect here
jump_now:
ld hl,launch_vectors ; Find jump angles table
ld a,(slope_angle) ; Get angle of the terrian (A = 0-62; evens only)
ld e,a
ld d,0
add hl,de ; HL -> X jump vector
ld c,(hl)
ld a,(x_vel)
add a,c
call pe,signed_restrict
ld (x_vel),a
inc hl ; HL -> Y jump vector
ld c,(hl)
ld a,(y_vel)
add a,c
call pe,signed_restrict
ld (y_vel),a
jr action_loop
signed_restrict:
jp p,signed_r_00 ; Negative -> Positive underflow?
ld a,127 ; Positive -> Negative overflow?
ret
signed_r_00:
ld a,-127
ret
dash:
ld a,127
ld (x_vel),a
jr action_loop
Motion:
ld hl,y_accel ; HL -> Y Acceleration vector for first object
ld b,2 ; B = 2 * (number of objects)
move_next_object:
;---Y/X-dimension movements
ld a,(hl) ; Load X or Y accelaration
ld c,a
inc hl ; HL -> X or Y velocity
ld a,(hl) ; Load X or Y velocity
add a,c ; Velocity = Velocity + Acceleration
jp po,limit_done ; Jump if No over/underflow
;---Impose maximum velocity
jp p,max_negative_velocity ; Negative -> Positive underflow?
ld a,terminal_vel_plus
jr limit_done
max_negative_velocity: ; Positive -> Negative overflow?
ld a,terminal_vel_minus
limit_done:
ld (hl),a ; Save new X or Y velocity
ld c,a
inc hl ; HL -> X or Y sub-pixel counter
ld a,(hl) ; Load X or Y sub-pixel position
add a,c ; Position = Position + Velocity
ld (hl),a ; Save X or Y sub-pixel position
inc hl ; HL -> X or Y position
jp po,motion_done_00 ; Jump if No over/underflow
;---Move object 1 pixel after overflow
jp p,pixel_dec ; Negative -> Positive underflow?
pixel_inc:
call ld_de_hlPTR
inc de
call ld_hlPTR_de ; Move object right
jr motion_done_01
pixel_dec:
call ld_de_hlPTR
dec de ; Move object right
call ld_hlPTR_de
jr motion_done_01
motion_done_00:
dec hl
add a,c
ld (hl),a
inc hl
jp po,motion_done_01
jp p,pixel_dec
jr pixel_inc
motion_done_01:
inc hl ; HL -> X or Y positon (low byte)
inc hl ; HL -> X or Y acceleration
djnz move_next_object
ret
;------------------------------------------------
; HL POINTER LOAD/SAVE: ;
; ;
; Places 16-bit content of the (HL) value onto;
; DE. ;
; ;
; ...Or places theh 16-bit value of DE into (HL);
; ;
; IN: -nothing- ;
; OUT: DE = (HL) big endian ;
; DESTROYED: D,E,flags ;
;------------------------------------------------
ld_de_hlPTR: ; Load from pointer
ld e,(hl)
inc hl
ld d,(hl)
dec hl
ret
ld_hlPTR_de: ; Save to pointer
ld (hl),e
inc hl
ld (hl),d
dec hl
ret
Display:
ld hl,spr_demo
ld a,(x_pos)
ld b,a
ld a,(y_pos)
ld c,a
call PutSprClp ; Clipped sprite
call CR_GRBCopy ; Refresh display (high speed)
call GRAPH_CLEAR
ret
GRAPH_CLEAR:
ld hl,GRAPH_MEM
ld de,GRAPH_MEM+1
ld bc,768 - 1
ld (hl),0
ldir
ret
; Object Physics Tables
y_accel:
.db 2
y_vel:
.db 0
y_sub:
.db 0
y_pos:
.dw $0000
x_accel:
.db 0
x_vel:
.db 0
x_sub:
.db 0
x_pos:
.dw $0000
spr_demo:
;AND MASK
.db %11111111
.db %11000111
.db %10000011
.db %00000001
.db %00000001
.db %00000001
.db %10000011
.db %11000111
; XOR MASK
.db %00000000
.db %00111000
.db %01111100
.db %11111110
.db %11111110
.db %11111110
.db %01111100
.db %00111000
launch_vectors:
; ; X-vector ; Y-vectors ; Launch angle
.db (jump_strength * 0 )/127 \ .db (jump_strength * 127)/127; 0 degrees
.db (jump_strength * 27 )/127 \ .db (jump_strength * 124)/127; 11.25
.db (jump_strength * 50 )/127 \ .db (jump_strength * 117)/127; 22.5
.db (jump_strength * 73 )/127 \ .db (jump_strength * 104)/127; 33.75
.db (jump_strength * 92 )/127 \ .db (jump_strength * 88)/127; 45
.db (jump_strength * 108)/127 \ .db (jump_strength * 67)/127; 56.25
.db (jump_strength * 119)/127 \ .db (jump_strength * 44)/127; 67.50
.db (jump_strength * 126)/127 \ .db (jump_strength * 19)/127; 78.75
.db (jump_strength * 127)/127 \ .db (jump_strength * 0 )/127; 90
.db (jump_strength * 126)/127 \ .db (jump_strength * -27)/127; 101.25
.db (jump_strength * 119)/127 \ .db (jump_strength * -50)/127; 112.50
.db (jump_strength * 108)/127 \ .db (jump_strength * -73)/127; 123.75
.db (jump_strength * 92)/127 \ .db (jump_strength * -92)/127; 135
.db (jump_strength * 73)/127 \ .db (jump_strength *-108)/127; 146.25
.db (jump_strength * 50)/127 \ .db (jump_strength *-119)/127; 157.50
.db (jump_strength * 27)/127 \ .db (jump_strength *-126)/127; 168.75
.db (jump_strength * 0 )/127 \ .db (jump_strength *-127)/127; 180
.db (jump_strength * -27)/127 \ .db (jump_strength *-126)/127; 191.25
.db (jump_strength * -50)/127 \ .db (jump_strength *-119)/127; 202.50
.db (jump_strength * -73)/127 \ .db (jump_strength *-108)/127; 213.75
.db (jump_strength * -92)/127 \ .db (jump_strength * -92)/127; 225
.db (jump_strength *-108)/127 \ .db (jump_strength * -73)/127; 236.25
.db (jump_strength *-119)/127 \ .db (jump_strength * -50)/127; 247.50
.db (jump_strength *-126)/127 \ .db (jump_strength * -27)/127; 258.75
.db (jump_strength *-127)/127 \ .db (jump_strength * 0 )/127; 270
.db (jump_strength *-126)/127 \ .db (jump_strength * 27)/127; 281.25
.db (jump_strength *-119)/127 \ .db (jump_strength * 50)/127; 292.50
.db (jump_strength *-108)/127 \ .db (jump_strength * 73)/127; 303.75
.db (jump_strength * -92)/127 \ .db (jump_strength * 92)/127; 315
.db (jump_strength * -73)/127 \ .db (jump_strength * 108)/127; 326.25
.db (jump_strength * -50)/127 \ .db (jump_strength * 119)/127; 337.50
.db (jump_strength * -27)/127 \ .db (jump_strength * 126)/127; 348.75
; 0 or 360
#include "spr30ax.inc" ; Clipped sprites; CrASH_MAN
Last edited by Guest on 31 Mar 2005 01:52:44 am; edited 1 time in total |