News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

3D Math Calc Problem

Started by Kman1011, April 08, 2007, 07:20:08 PM

Previous topic - Next topic

Kman1011

Hey.

I've been working on a 3d engine concept to see if the trig functions would work to give me a 3D simulation. It seems to work fine until you look up and down with the arrow keys. For some reason the horizon seems to bend and get all distorted.

If you look side to side it all seems OK.

Here is the concept...

Imagine you are standing in an array of glass blocks 14 x 14 x 14 and the corner of each block is a dot.

Here is the code...

PlayBASIC Code: [Select]
; PROJECT : 3DMathCalcs
; AUTHOR : Kman
; CREATED : 4/3/2007
; EDITED : 4/4/2007
; ---------------------------------------------------------------------

;3D Math Calcs
;Input Variables
HBearing=0
VBearing=0
lz=0
px=3
py=2
pz=6
fv#=90

;Math Calcs
Do
a$=Inkey$()
Cls 0
For lz=-60 To 60 Step 10
For ly=-60 To 60 Step 10
For lx=-60 To 60 Step 10
dx=lx-px
dy=ly-py
dz=lz-pz
pd#=Sqrt(dx^2+dy^2)
pdz#=Sqrt(dz^2+pd#^2)
Pang#=HBearing-ASin(dx/pd#)
Zang#=VBearing-ASin(dz/pdz#)
vx#=Sin(Pang#)*pd#
vy#=Cos(Pang#)*pd#
vz#=Sin(Zang#)*pdz#
;Print pdz#
;Print "Vx="+Str$(vx#)
;Print "Vy="+Str$(vy#)
;Print "Vz="+Str$(vz#)
Ppu#=GetScreenWidth()/((Sin(fv#/2)*vy#)*2)
Sx=(GetScreenWidth()/2)-Ppu#*vx#
If Pang#>=90 And Pang#<=270
Sy=(GetScreenHeight()/2)-PPu#*vz#
Else
Sy=(GetScreenHeight()/2)+PPu#*vz#
EndIf
If sx>0 And sx<800 And Sy>0 And Sy<600
DotC Sx,Sy,RGB(255,255, 0)
EndIf
;Print vz#
;Print "Sx="+Str$(Sx)
;Print "Sy="+Str$(Sy)
Next lx
Next ly

Next lz
If RightKey()=True
Hbearing=Hbearing+5:If Hbearing>359 Then Hbearing=0
EndIf
If LeftKey()=True
HBearing=Hbearing-5:If Hbearing<0 Then Hbearing=359
EndIf
If UpKey()=True
Dec Vbearing:If Vbearing<-45 Then Vbearing=-45
EndIf
If DownKey()=True
Inc VBearing:If Vbearing>45 Then Vbearing=45
EndIf
If a$="8"
px=px+Sin(HBearing)
py=py+Cos(Hbearing)
EndIf
If DownKey()=True
Inc VBearing:If Vbearing>45 Then Vbearing=45
EndIf
SetCursor 0,0
Print Hbearing
Print VBearing

Print Sy



Sync
Loop
WaitKey




I think it has something to do with the conversion of virtual co-ordinates to screen co-ordinates


Any ideas ??? ???


just to clarify-
   HBearing is angle looking across
   VBearing is looking up and down
   Px,y,z is your position
   fv#  is your field of vision is degrees

Ahh... Another visitor. Stay awhile....STAY FOREVER!!!...MWA-HA-HA-HA

Kman1011

Well I'll be!!! :D

I have everyone STUMPED!!

This is a first.

Well no bother. I'll just take out the Z angle (Zang#) which looks up and down. Everything else works Ok.

I guess It's beyond everyone else... ;) I'll take it from here.
Ahh... Another visitor. Stay awhile....STAY FOREVER!!!...MWA-HA-HA-HA

kevin


   You're aware that such functionality is built it right ?

   See. Projects/Examples/Rotation/RotateVertLIstToCamera.



Kman1011

Quote
   You're aware that such functionality is built it right ?


No I wasn't.

I have Version 1.62. If this fuction is under the 'Camera' heading in the docs. I don't have it. ???

If you have a link to update my docs. I'd much appreciate it. (Outstanding example by the way.. Not sure how it works-- but i'll figure it out).

That's why I do these calcs from scratch. To better understand the 3D concept and carry in texture mapping, world collision, etc.

Thanx for getting back
Ahh... Another visitor. Stay awhile....STAY FOREVER!!!...MWA-HA-HA-HA