0 Members and 1 Guest are viewing this topic.
Hmm could you explain how you are storing the coordinates of the ships right now? Do you also plan to take into account what direction the ships are facing? Do you want to include non-orthogonal angles?
Thus, all I need in order to judge visibility is location. Angle data can be ship-specific. Now, I understand that this system may need to be adjusted, but it should work well without having each ship broadcast its angles as well.
Quote from: ACagliano on December 02, 2011, 05:27:39 pmThus, all I need in order to judge visibility is location. Angle data can be ship-specific. Now, I understand that this system may need to be adjusted, but it should work well without having each ship broadcast its angles as well.Wait so there is additional data for each ship than what you posted before?
;variables used for saving the location. replace locvars with the memory location they should bex .equ locVarsy .equ x+2z .equ y+2hdir .equ z+2vdir .equ hdir+1;Variables used by the 3D engine. replace EngineVars with the memory location they should be.temp .equ EngineVarstemp2 .equ temp+2temp3 .equ temp2+2Asqr .equ temp3+2Bsqr .equ Asqr+2Csqr .equ Bsqr+2 xfrom .equ Csqr+2yfrom .equ xfrom+2zfrom .equ yfrom+2xto .equ zfrom+2yto .equ xto+2zto .equ yto+2xup .equ zto+2yup .equ xup+2zup .equ yup+2angle .equ zup+2xcross .equ angle+1ycross .equ xcross+2zcross .equ ycross+2xpoint .equ zcross+2ypoint .equ xpoint+2zpoint .equ ypoint+2valid .equ zpoint+2screenx .equ valid+1screeny .equ screenx+2screenz .equ screeny+2#define speed $0040 ;move with 1/4 unit per frame;call this routine before rendering each screenInit3D: ld hl, (x) ld (xfrom), hl push hl ld a, (hdir) add a, 64 call SinA push hl ld a, (vdir) add a, 64 call SinA ex de, hl pop bc call MulFP pop de add hl, de ld (xto), hl ld hl, (y) ld (yfrom), hl push hl ld a, (hdir) call SinA push hl ld a, (vdir) add a, 64 call SinA ex de, hl pop bc call MulFP pop de add hl, de ld (yto), hl ld hl, (z) ld (zfrom), hl push hl ld a, (vdir) call SinA pop de add hl, de ld (zto), hl ld hl, 0 ld (xup), hl ld (yup), hl ld hl, $0100 ld (zup), hl call InitView ret;call this routine to move:Move: ld hl, (x) push hl ld a, (hdir) add a, 64 call SinA push hl ld a, (vdir) add a, 64 call SinA ex de, hl pop bc call MulFP ex de, hl ld bc, speed call MulFP pop de add hl, de ld (x), hl ld hl, (y) push hl ld a, (hdir) call SinA push hl ld a, (vdir) add a, 64 call SinA ex de, hl pop bc call MulFP ex de, hl ld bc, speed call MulFP pop de add hl, de ld (y), hl ld hl, (z) push hl ld a, (vdir) call SinA ex de, hl ld bc, speed call MulFP pop de add hl, de ld (z), hl ret;the routines of the 3D engine.InitView: ;call once every frame, before C3DTo2D ;IN: The engine's vars ;OUT: vars are initialised ;DESTROYS: all registers and the engine's variables ld hl, (xto) ld de, (xfrom) SubFP ld (xto), hl ld hl, (yto) ld de, (yfrom) SubFP ld (yto), hl ld hl, (zto) ld de, (zfrom) SubFP ld (zto), hl ld de, (xto) ld b, d \ ld c, e call MulFP ld (temp), hl ld de, (yto) ld b, d \ ld c, e call MulFP ld de, (temp) add hl, de ld (temp), hl ld de, (zto) ld b, d \ ld c, e call MulFP ld de, (temp) add hl, de call SqrtFP ld (temp), hl ld hl, (xto) ld de, (temp) call DivFP ld (xto), hl ld hl, (yto) ld de, (temp) call DivFP ld (yto), hl ld hl, (zto) ld de, (temp) call DivFP ld (zto), hl ld de, (xup) ld bc, (xto) call MulFP ld (temp), hl ld de, (yup) ld bc, (yto) call MulFP ld de, (temp) add hl, de ld (temp), hl ld de, (zup) ld bc, (zto) call MulFP ld de, (temp) add hl, de ld (temp), hl ld de, (temp) ld bc, (xto) call MulFP ld d, h \ ld e, l ld hl, (xup) SubFP ld (xup), hl ld de, (temp) ld bc, (yto) call MulFP ld d, h \ ld e, l ld hl, (yup) SubFP ld (yup), hl ld de, (temp) ld bc, (zto) call MulFP ld d, h \ ld e, l ld hl, (zup) SubFP ld (zup), hl ld de, (xup) ld b, d \ ld c, e call MulFP ld (temp), hl ld de, (yup) ld b, d \ ld c, e call MulFP ld de, (temp) add hl, de ld (temp), hl ld de, (zup) ld b, d \ ld c, e call MulFP ld de, (temp) add hl, de call sqrtFP ld (temp), hl ;Multiply (temp) with the tangent of (angle)/2, which is, in this test program, equal to 1, so we don't need to multiply ld hl, (xup) ld de, (temp) call DivFP ld (xup), hl ld hl, (yup) ld de, (temp) call DivFP ld (yup), hl ld hl, (zup) ld de, (temp) call DivFP ld (zup), hl ld de, (yup) ld bc, (zto) call MulFP ld (temp), hl ld de, (zup) ld bc, (yto) call MulFP ld d, h \ ld e, l ld hl, (temp) SubFP ld (xcross), hl ld de, (zup) ld bc, (xto) call MulFP ld (temp), hl ld de, (xup) ld bc, (zto) call MulFP ld d, h \ ld e, l ld hl, (temp) SubFP ld (ycross), hl ld de, (xup) ld bc, (yto) call MulFP ld (temp), hl ld de, (yup) ld bc, (xto) call MulFP ld d, h \ ld e, l ld hl, (temp) SubFP ld (zcross), hl ret C3DTo2D: ;IN: xpoint, ypoint and zpoint vars ;OUT: screenx and screeny are set to the onscreen coordinates for the point ;Will not work if InitView wasn't called before this routine ;destroys all registers and xpoint, ypoint and zpoint ld hl, (xpoint) ld de, (xfrom) SubFP ld (xpoint), hl ld hl, (ypoint) ld de, (yfrom) SubFP ld (ypoint), hl ld hl, (zpoint) ld de, (zfrom) SubFP ld (zpoint), hl ld de, (xpoint) ld bc, (xto) call MulFP ld (temp), hl ld de, (ypoint) ld bc, (yto) call MulFP ld de, (temp) add hl, de ld (temp), hl ld de, (zpoint) ld bc, (zto) call MulFP ld de, (temp) add hl, de ld (temp), hl ld (screenz), hl ;kijk hier of het punt zichtbaar is bit 7, h jp nz, InValid ld de, (xpoint) ld bc, (xcross) call MulFP ld (temp2), hl ld de, (ypoint) ld bc, (ycross) call MulFP ld de, (temp2) add hl, de ld (temp2), hl ld de, (zpoint) ld bc, (zcross) call MulFP ;ld de, (temp2) ;ld bc, (zup) ;call MulFP ld de, (temp2) add hl, de ld de, (temp) call DivFP ld (screenx), hl ld de, (xpoint) ld bc, (xup) call MulFP ld (temp2), hl ld de, (ypoint) ld bc, (yup) call MulFP ld de, (temp2) add hl, de ld (temp2), hl ld de, (zpoint) ld bc, (zup) call MulFP ld de, (temp2) add hl, de ld de, (temp) call DivFP ld (screeny), hl ld hl, $0100 ld de, (screenx) SubFP ld b, h \ ld c, l ld de, 48 call MulFP ld (screenx), hl ld de, (screeny) ld hl, $0100 SubFP ld b, h \ ld c, l ld de, 32 call MulFP ld (screeny), hl ld a, 1 retInValid: ld a, 0 ret;These routines are for the math in the 3D engine.;;;;;;;;;;;;;;;;;;;;;;;;;; MATH ROUTINES ;;;;;;;;;;;;;;;;;;;;;;;;;;; SqrtA: LD (Asqr),A SRL A JR DataOverDataOver: LD (Bsqr),A LD B,A LD (Csqr),Aiterate: LD A,(Bsqr) ld b, a LD a,(Asqr) LD D,A LD E,BdividedbyEreturnA: RL D RLA SUB E JR nc,$+3 ADD A,E LD E,A LD A,D CPL push af ;1 LD a,(Bsqr) ld b, a pop af ;0 ADD A,B SRL A LD (Bsqr),A LD a,(Bsqr) ld b, a LD A,(Csqr) DEC A CP B JR z,done push af ;1 LD (Csqr),a ld b, a pop af ;0 JR iteratedone: LD A,(Bsqr) RET ;SubFP:; ;substracts 2 16-bit fixed-point numbers; ;IN: hl, de; ;OUT: hl - de in hl; ;DESTROYS: a, hl; or a ;clear carry flag; sbc hl, de; ret Div16By8: ;divides a 16-bit number by an 8-bit number ;IN: hl and d ;OUT: hl = hl/d, remainder = a ;DESTROYS: a, b, c, d, hl xor a ld b, 16_D16b8Loop: add hl, hl rla jr c, _D16b8Overflow cp d jr c, _D16b8Skip_D16b8Overflow: sub b inc l_D16b8Skip: djnz _D16b8Loop ret MulFP: ;Multiplies 2 16bit fixed-point numbers ;IN: de, bc ;OUT: de * bc in hl ;DESTROYS: af, bc, de, hl bit 7, d jr nz, _MulFP_FirstNeg bit 7, b jr nz, _MulFP_AnsNeg jr z, _MulFP_AnsPos_MulFP_FirstNeg: bit 7, b jr nz, _MulFP_AnsPos_MulFP_AnsNeg: ld a, 1 push af ;1 jr _MulFP_Cont_MulFP_AnsPos: ld a, 0 push af ;1_MulFP_Cont: bit 7, b jr z, _MulFP_BCPos call NegBC_MulFP_BCPos: bit 7, d jr z, _MulFP_DEPos call NegDE_MulFP_DEPos: ld hl,0 ld a,16Mul16Loop: add hl,hl rl e rl d jp nc,NoMul16 add hl,bc jp nc,NoMul16 inc deNoMul16: dec a jp nz,Mul16Loop ld l, h ld h, e pop af ;0 cp 1 call z, NegHL ret ;adding fixed-point numbers:;load one of the numbers in hl, and the other in an other 16-bit register.;then, do: add hl, <register>DivFP: ;IN: hl, de ;OUT: hl = hl / de ;DESTROYS: af, hl, bc, d bit 7, d jr nz, _DivFP_FirstNeg bit 7, h jr nz, _DivFP_AnsNeg jr z, _DivFP_AnsPos_DivFP_FirstNeg: bit 7, h jr nz, _DivFP_AnsPos_DivFP_AnsNeg: ld a, 1 push af jr _DivFP_Cont_DivFP_AnsPos: ld a, 0 push af_DivFP_Cont: bit 7, h jr z, _DivFP_HLPos call NegHL_DivFP_HLPos: bit 7, d jr z, _DivFP_DEPos call NegDE_DivFP_DEPos ld a, h ld h, l ld l, 0 push hl ;INPUTS: ahl = dividend de = divisor ;1 pop ix ;OUTPUTS: ahl = quotient de = divisor ;0 ld hl,0 ld b,24_Div24by16loop: add ix,ix rla adc hl,hl jr c,_Div24by16setbit or a sbc hl,de add hl,de jr c,_Div24by16skip_Div24by16setbit: or a sbc hl,de inc ix_Div24by16skip: djnz _Div24by16loop push ix ;1 pop hl ;0 pop af cp 1 call z, NegHL ret SinA: ;calculates the sine of a as a fixed point number ;IN: a ;OUT: hl = sin(a) LD h, 0 LD l, a add hl, hl LD DE, sine_table ADD HL, DE LD A, (HL) INC HL LD H, (HL) LD L, A RETsine_table:.dw 0, 6, 13, 19, 25, 31, 38, 44, 50, 56, 62, 68, 74, 80, 86, 92, 98, 104, 109, 115, 121, 126, 132, 137, 142.dw 147, 152, 157, 162, 167, 172, 177, 181, 185, 190, 194, 198, 202, 206, 209, 213, 216, 220, 223, 226, 229, 231, 234.dw 237, 239, 241, 243, 245, 247, 248, 250, 251, 252, 253, 254, 255, 255, 256, 256, 256, 256, 256, 255, 255, 254, 253.dw 252, 251, 250, 248, 247, 245, 243, 241, 239, 237, 234, 231, 229, 226, 223, 220, 216, 213, 209, 206, 202, 198, 194 .dw 190, 185, 181, 177, 172, 167, 162, 157, 152, 147, 142, 137, 132, 126, 121, 115, 109, 104, 98, 92, 86, 80, 74, 68.dw 62, 56, 50, 44, 38, 31, 25, 19, 13, 6, 0, -6, -13, -19, -25, -31, -38, -44, -50, -56, -62, -68, -74, -80, -86, -92.dw -98, -104, -109, -115, -121, -126, -132, -137, -142, -147, -152, -157, -162, -167, -172, -177, -181, -185, -190.dw -194, -198, -202, -206, -209, -213, -216, -220, -223, -226, -229, -231, -234, -237, -239, -241, -243, -245, -247.dw -248, -250, -251, -252, -253, -254, -255, -255, -256, -256, -256, -256, -256, -255, -255, -254, -253, -252, -251.dw -250, -248, -247, -245, -243, -241, -239, -237, -234, -231, -229, -226, -223, -220, -216, -213, -209, -206, -202.dw -198, -194, -190, -185, -181, -177, -172, -167, -162, -157, -152, -147, -142, -137, -132, -126, -121, -115, -109.dw -104, -98, -92, -86, -80, -74, -68, -62, -56, -50, -44, -38, -31, -25, -19, -13, -6 SqrtFP:;#####################################################;square root of fixed point hl;input: hl;output: hl=sqrt(hl)sqrt_hl: ld a, l ld l, h ld de, 0040h ; 40h appends "01" to D ld h, d ld b, 7 ; need to clear the carry beforehand or a _loopz: sbc hl, de jr nc, $+3 add hl, de ccf rl d rla adc hl, hl rla adc hl, hl djnz _loopz sbc hl, de ; optimised last iteration ccf rl d ld h, d xor a ld b, 4shiftLoopz: srl h rra djnz shiftLoopz ld l, a ret NegHL: xor a sub l ld l,a sbc a,a sub h ld h,a retNegBC: xor a sub c ld c,a sbc a,a sub b ld b,a retNegDE: xor a sub e ld e,a sbc a,a sub d ld d,a retGenerated by the BBify'r (http://clrhome.org/resources/bbify/)
#define SubFP xor a \ sbc hl, de