Author Topic: Interaction with a 3D Space  (Read 8435 times)

0 Members and 2 Guests are viewing this topic.

Offline ACagliano

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 919
  • Rating: +32/-2
    • View Profile
    • ClrHome Productions
Interaction with a 3D Space
« on: December 02, 2011, 03:15:47 pm »
I'm looking for someone with experience in programming interaction with a 3D space for my Star Trek game. At present, I only support 6 directions of movement: up, down, left, right, forward, and backwards. This is not truly 3D. I was thinking of using a pair of vectors...an x,y vector and a z vector, but I do not know how to code this. I will need someone to code this up for me.

In addition, if I go with the above, I will need to redo the routine in which we look to see if a ship is in your viewscreen. I'll need that coded, or at least assistance with that, as well.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Interaction with a 3D Space
« Reply #1 on: December 02, 2011, 04:00:43 pm »
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?

Offline ACagliano

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 919
  • Rating: +32/-2
    • View Profile
    • ClrHome Productions
Re: Interaction with a 3D Space
« Reply #2 on: December 02, 2011, 04:17:03 pm »
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?

Right now, the positions of a maximum of 30 ships are in the following format:

5 byte calc ID
8 byte username
6 byte ship location (xx, yy, zz)
3 byte ship sector (x, y, z)

I doubt that would need to change.
No, I only show one generic image if a ship is located in the viewscreen.
What is a non-orthogonal angle?

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Interaction with a 3D Space
« Reply #3 on: December 02, 2011, 04:23:44 pm »
What i mean by taking into account what direction ships are facing, how do you know if 1 ship is able to see another ship?  And a non-orthogonal angle is an angle that is not a multiple of 90 degrees.  Basically I am asking if the ships can rotate or look around, but looking at your data it doesn't seem to have any angle data at all?

Offline ACagliano

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 919
  • Rating: +32/-2
    • View Profile
    • ClrHome Productions
Re: Interaction with a 3D Space
« Reply #4 on: December 02, 2011, 05:27:39 pm »
I will be judging if a ship can see another as follows:

on the active axis (one that the ship is moving on), you can up to 100 units ahead of you and 50 units to the left and right, up and down. If a ship is behind you on the active axis, you don't see it. Similarly, if it is outside the 100 ahead x 100 up/down x 100 left/right, you cannot see it.

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.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Interaction with a 3D Space
« Reply #5 on: December 02, 2011, 05:31:07 pm »
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.

Wait so there is additional data for each ship than what you posted before?

Offline ACagliano

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 919
  • Rating: +32/-2
    • View Profile
    • ClrHome Productions
Re: Interaction with a 3D Space
« Reply #6 on: December 02, 2011, 06:48:48 pm »
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.
Wait so there is additional data for each ship than what you posted before?

There is alot of data that about ships that do not get broadcast. There is the offline/online status of your systems, health and max health of each system, shield health, weapons armed flag, shields raised flag, current traveling direction and speed of your ship, ect. However, all that gets broadcast to other calcs is your ID (so that a PTP can be sent), the username (so that you see a name, not 5 numbers), and the location (position and sector).
« Last Edit: December 02, 2011, 06:49:24 pm by ACagliano »

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: Interaction with a 3D Space
« Reply #7 on: December 03, 2011, 03:37:50 pm »
Do you want to be able to rotate your ship, then fly in the direction it's facing instead of moving in only 6 directions? Or did i misunderstand your question? If you want this, you are going to need fixed point numbers or inflation by 256. Then you need a sine routine. When the ship is moving, you add sin(hdir + 90°) to the X coordinate, and sin(hdir) to thy Y coordinate, where hdir stands for the direction in the horizontal plane. If you then add movement along the Z axis, the controls remain simple and they might be good enough. If you however want to be able to fly in the 3 dimensions, then you need 2 direction variables. The math to calculate the coordinates will be slightly more complex too. Now you'll have to add sin(hdir + 90°) * sin(vdir + 90°) to your X coordinate,  sin(hdir) * sin(vdir + 90°) to your Y coordinate, and sin(vdir) to your z coordinate. You should also keep in mind that if you want to be able to look in different directions, your 3D projection algorithm needs to support rotation of the camera, so a simple 1, 2 or 3 point perspective algorithm (like what you use when making 3D drawings) won't work anymore. You have to go with a perspective projection algorithm. If you don't already use one, you can find a good one on wikipedia, or I can pm you the code for mine, if you want.
« Last Edit: December 03, 2011, 03:40:54 pm by ben_g »
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline ACagliano

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 919
  • Rating: +32/-2
    • View Profile
    • ClrHome Productions
Re: Interaction with a 3D Space
« Reply #8 on: December 03, 2011, 08:47:47 pm »
You have it exactly right. I want to be able to rotate the ship and fly in the direction it's facing. I have three axises, x,y,and z. I have no idea how to do this in z80 :(

As for the looking in different directions, you are referring to the "view-screen" correct? If so, then yes. I can use a pretty standard space image when there are no ships in front of you...but the issue is looking ahead of you for a ship, then rendering it. Perhaps a perspective projection algorithm is what I need. But, since I have never done this before, i would have no idea how to mold it for my purposes.
« Last Edit: December 03, 2011, 08:57:11 pm by ACagliano »

Offline ACagliano

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 919
  • Rating: +32/-2
    • View Profile
    • ClrHome Productions
Re: Interaction with a 3D Space
« Reply #9 on: December 05, 2011, 03:42:45 pm »
Bump^^

I plan on remolding the menu GUI's a bit, just for the record.

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: Interaction with a 3D Space
« Reply #10 on: December 05, 2011, 04:28:55 pm »
here is some code:
Spoiler For code:
Quote from: Z80 Assembly
;variables used for saving the location. replace locvars with the memory location they should be
x .equ locVars
y .equ x+2
z .equ y+2
hdir .equ z+2
vdir .equ hdir+1

;Variables used by the 3D engine. replace EngineVars with the memory location they should be.
temp .equ EngineVars
temp2 .equ temp+2
temp3 .equ temp2+2

Asqr .equ temp3+2
Bsqr .equ Asqr+2
Csqr .equ Bsqr+2
 
xfrom .equ Csqr+2
yfrom .equ xfrom+2
zfrom .equ yfrom+2
xto .equ zfrom+2
yto .equ xto+2
zto .equ yto+2
xup .equ zto+2
yup .equ xup+2
zup .equ yup+2
angle .equ zup+2
xcross .equ angle+1
ycross .equ xcross+2
zcross .equ ycross+2
xpoint .equ zcross+2
ypoint .equ xpoint+2
zpoint .equ ypoint+2
valid .equ zpoint+2
screenx .equ valid+1
screeny .equ screenx+2
screenz .equ screeny+2

#define
speed $0040 ;move with 1/4 unit per frame

;call this routine before rendering each screen
Init3D:
  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
  ret
InValid:
  ld a, 0
  ret


;These routines are for the math in the 3D engine.

;;;;;;;;;;;;;;;;;;;;;;;;;; MATH ROUTINES ;;;;;;;;;;;;;;;;;;;;;;;;;;;

 
SqrtA:
   LD   (Asqr),A
   SRL   A
   JR   DataOver
DataOver:
   LD   (Bsqr),A
   LD   B,A
   LD   (Csqr),A
iterate:
   LD   A,(Bsqr)
   ld     b, a
   LD   a,(Asqr)
   LD   D,A
   LD   E,B
dividedbyEreturnA:
   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   iterate
done:
   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,16
Mul16Loop:
  add hl,hl
  rl e
  rl d
  jp nc,NoMul16
  add hl,bc
  jp nc,NoMul16
  inc de
NoMul16:
  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
    RET
sine_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, 4
shiftLoopz:
   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
   ret

NegBC:
   xor a
   sub c
   ld c,a
   sbc a,a
   sub b
   ld b,a
   ret

NegDE:
   xor a
   sub e
   ld e,a
   sbc a,a
   sub d
   ld d,a
   ret

Generated by the BBify'r (http://clrhome.org/resources/bbify/)

Use it like the comments tell you. To render, AFTER calling Init3D, load the x, y and z coordinates in the xpoint, ypoint and zpoint variables (in signed 8.8 fixed-point format). Then call C3D to2D. If the point was behind the camera, a will be set to 0. If the point is in front of the camera, a will be 1. The screen coordinates will be stored in screenx and screeny, and the distance to the point will be stored in screenz.

For inputting those fixed-point numbers, I created a tool in java. If you want to use it, you can download it here: http://dl.dropbox.com/u/11215358/base%20converter.jar
To use it to convert a decimal value into hexadecimal, fill in decimal and hexadecimal in the top field, type in the number , make sure the option 'Signed' is enabled and select '8.8 fixed-point' as format. Then click 'convert'. It will then generate the Hexadecimal code in the field at the bottom. Then just take the last 4 characters or add zeros to the front and you've got your hexadecimal number. For debugging, you can switch decimal and hexadecimal and type in a hex number.
« Last Edit: December 05, 2011, 04:30:04 pm by ben_g »
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline ACagliano

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 919
  • Rating: +32/-2
    • View Profile
    • ClrHome Productions
Re: Interaction with a 3D Space
« Reply #11 on: December 05, 2011, 05:17:41 pm »
Ok, question. The format I use has x, y,and z sector (1-byte for each) and an x, y, and z position within that sector (2-bytes each). Can these routines handle two-byte locations?

The two byte locations are they for 8.8, or for a full 2-byte position? Because I intended to have a full 0-65536 position range. Is it possible to do 16.16 or 16.8? lol


For ease of use, should I have the x, y, and z coordinates as 8.8 fixed point already?

As for speed...Well, I have a speed variable. (Speed). 0 = stopped, 1 = 1/4 impulse, 2 = 1/2 impulse, 3 = 3/4 imulse, 4 = full impulse, 5 = warp speed (100 x full impulse)

I also have variables preassigned. (PositionX), (PositionY), (PositionZ). Will simply replacing your (x), (y), and (z) with those break anything?

Can the memory areas from (temp) down be overwritten? Or should I use a memory area that I don't use?
« Last Edit: December 06, 2011, 10:32:41 am by ACagliano »

Offline ACagliano

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 919
  • Rating: +32/-2
    • View Profile
    • ClrHome Productions
Re: Interaction with a 3D Space
« Reply #12 on: December 06, 2011, 12:42:56 pm »
Oh, question....In the code above, when you say "SubFP", do you mean "call SubFP"

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: Interaction with a 3D Space
« Reply #13 on: December 06, 2011, 01:55:55 pm »
Sorry, I forgot to include it. Add this to your code:
Code: [Select]
#define SubFP xor a \ sbc hl, deAnd all numbers handled by the 3D engine are signed 8.8fixed-points (eccept for the angles). Speed stands for the number of units per secound. You can't use 16.8 for the position, but you can easily just send everything within 128 units to the 3D engine, in relative coordinates. Everything further than that will probably be to far to see anyway.
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline ACagliano

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 919
  • Rating: +32/-2
    • View Profile
    • ClrHome Productions
Re: Interaction with a 3D Space
« Reply #14 on: December 06, 2011, 02:05:32 pm »
Shit, that was way over my head. Let me break this down to see if I understand.

1. The 3D engine uses 8.8 fixed point numbers. What impact does that have on my two-byte position variables?

2. I understand about speed. How do I make Speed variable?

3. Do I need to look for a ship, then call the 3D engine each time I find one? Or does one of those routines handle that?

After I get a handle on those concepts, I think I should be good.