07-04-2024, 05:48 PM
So, as the math midget I am, I stumbled upon a way to make 3D looking wire frames using the ATAN2 command which turns "Cartesian coordinates into global coordinates" or so I read. I don't understand why/how the position of the ship on the screen affects the rendering of the eleven 2D points I created. For example, pressing z and putting the ship on the left-hand side of the screen smooshes it flat. Can anyone explain to me on, say, an 8th grade level, what's going on triginomically here? What have I done?!
Code: (Select All)
OPTION _EXPLICIT
DIM AS INTEGER DTW, DTH: DTW = _DESKTOPWIDTH: DTH = _DESKTOPHEIGHT
SCREEN _NEWIMAGE(DTW, DTH, 32)
_FULLSCREEN _SQUAREPIXELS , _SMOOTH '
_MOUSEHIDE '
TYPE pnt
x AS SINGLE
y AS SINGLE '
ang AS SINGLE '
radius AS SINGLE '
END TYPE '
DIM pnt(1 TO 13) AS pnt
DIM c AS INTEGER
DIM AS DOUBLE radians
DIM AS INTEGER radius
DIM AS SINGLE adder, sizeFactor, angle, fpChange
DIM AS SINGLE shipX, shipY
DIM AS LONG starScape
DIM AS _UNSIGNED LONG yellow
yellow = _RGB32(249, 244, 17)
shipX = DTW / 2
shipY = DTH / 2
DATA 180,25
' 1 11 POINTS TO PLAY WITH, 1 is nose point
DATA 265,30
' 2 DATA = THETA ANGLE, RADIUS - larger radius = closer to viewer
DATA 275,30
' 3
DATA 340,4
' 4
DATA 0,5
' 5
DATA 20,4
' 6
DATA 85,30
' 7
DATA 95,30
' 8
DATA 130,4
' 9
DATA 230,4
' 10
DATA 0,0
' 11
DATA 270,27
' 12, side panel point
DATA 90,27
' 13, other side panel
FOR c = 1 TO UBOUND(pnt) ' init points array
READ angle, radius
pnt(c).ang = angle
pnt(c).radius = radius
NEXT c
sizeChange 2.5
sizeFactor = 38 ' start up settings
adder = .25
angle = 270
fpChange = 66 ' the nose point
pnt(1).radius = pnt(1).radius + fpChange
drawStars
DO
CLS
FOR c = 1 TO UBOUND(pnt) ' rotate ship
pnt(c).ang = pnt(c).ang + adder
radians = _D2R(pnt(c).ang)
pnt(c).x = pnt(c).radius * COS(radians) + shipX ' atan2 subbed for x/cos value spins in Y axis, neg for left side, pos for right
pnt(c).y = pnt(c).radius * _ATAN2(pnt(c).x, pnt(c).y) + shipY ' atan2 subbed for y/sin value spins in X axis, upside down, rightside up...
NEXT c
_PUTIMAGE , starScape, 0
' -----------------------------*******************
LINE (pnt(1).x, pnt(1).y)-(pnt(2).x, pnt(2).y), yellow ' draw ship
FOR c = 3 TO 8
LINE -(pnt(c).x, pnt(c).y), yellow
NEXT c
LINE (pnt(8).x, pnt(8).y)-(pnt(1).x, pnt(1).y), yellow
LINE (pnt(3).x, pnt(3).y)-(pnt(1).x, pnt(1).y), yellow
LINE (pnt(7).x, pnt(7).y)-(pnt(1).x, pnt(1).y), yellow
LINE (pnt(2).x, pnt(2).y)-(pnt(10).x, pnt(10).y), yellow
LINE (pnt(8).x, pnt(8).y)-(pnt(9).x, pnt(9).y), yellow
LINE (pnt(10).x, pnt(10).y)-(pnt(11).x, pnt(11).y), yellow
LINE (pnt(9).x, pnt(9).y)-(pnt(11).x, pnt(11).y), yellow
LINE (pnt(5).x, pnt(5).y)-(pnt(11).x, pnt(11).y), yellow
IF sizeFactor > 10 AND sizeFactor < 80 AND fpChange > -20 AND shipX = DTW / 2 AND shipY = DTH / 2 THEN
IF angle < 80 OR angle > 287 THEN PAINT (_WIDTH / 2, _HEIGHT / 2 + 40), _RGB32(250, 50, 150, 60), yellow
IF angle > 77 AND angle < 255 THEN PAINT (_WIDTH / 2, _HEIGHT / 2 + 40), _RGB32(100, 190, 0, 90), yellow
END IF
IF sizeFactor > -50 THEN
CIRCLE (pnt(12).x, pnt(12).y), 2, _RGB32(249, 249, 0) ' side panel
PAINT (pnt(12).x, pnt(12).y), _RGB32(255, 0, 0), _RGB32(249, 249, 0)
CIRCLE (pnt(13).x, pnt(13).y), 2, _RGB32(249, 250, 0)
PAINT (pnt(13).x, pnt(13).y), _RGB32(255, 0, 0), _RGB32(249, 250, 0)
END IF
' -----------------------------******************* user input
IF _KEYDOWN(122) THEN shipX = shipX - 2
IF _KEYDOWN(120) THEN shipX = shipX + 2
IF _KEYDOWN(99) THEN shipY = shipY - 1.554
IF _KEYDOWN(118) THEN shipY = shipY + 1.554
IF _KEYDOWN(19200) THEN
IF fpChange > -500 THEN
pnt(1).radius = pnt(1).radius - 1 ' far point changes
fpChange = fpChange - 1
END IF
END IF
IF _KEYDOWN(19712) THEN
IF fpChange < 500 THEN
pnt(1).radius = pnt(1).radius + 1
fpChange = fpChange + 1
END IF
END IF
IF _KEYDOWN(97) THEN adder = -.5 ' angle changes
IF _KEYDOWN(100) THEN adder = .5
IF _KEYDOWN(18432) THEN IF sizeFactor < 150 THEN sizeChange 1.01: sizeFactor = sizeFactor + .5 ' size changes
IF _KEYDOWN(20480) THEN IF sizeFactor > -250 THEN sizeChange .99: sizeFactor = sizeFactor - .5
IF _KEYDOWN(114) THEN adder = .2 ' r to rotate
IF _KEYDOWN(115) THEN adder = 0 ' s to stop
angle = angle + adder ' auto-spin
IF angle > 359 THEN angle = 0 ' mind the angle
IF angle < 0 THEN angle = 359
PRINT "Angle:"; INT(angle)
PRINT "Size Factor:"; sizeFactor
PRINT "Far Point Change:"; fpChange
PRINT
PRINT "up arrow = ENLARGE SHIP"
PRINT "down arrow = SHRINK SHIP"
PRINT "left arrow = 'PULL' NOSE POINT"
PRINT "right arrow = 'PUSH' NOSE POINT"
PRINT "d = spin fast clockwise"
PRINT "a = spin fast counterclockwise"
PRINT "r = back to slow rotate"
PRINT "s = stop rotation"
PRINT "z = LEFT"
PRINT "x = RIGHT"
PRINT "c = UP"
PRINT "v = DOWN"
_DISPLAY
_LIMIT 200
LOOP UNTIL _KEYDOWN(27)
SYSTEM
' ------------------------------------
SUB sizeChange (r AS SINGLE)
DIM c AS INTEGER
SHARED AS pnt pnt()
FOR c = 1 TO UBOUND(pnt)
pnt(c).radius = pnt(c).radius * r
NEXT c
END SUB
' ------------------------------------
SUB drawStars () ' starscape backdrop
DIM c AS INTEGER
DIM AS LONG virtual
SHARED AS LONG starScape
virtual = _NEWIMAGE(1280, 720, 32) '
_DEST virtual
c = 0
DO
c = c + 1
PSET ((INT(RND * _WIDTH)), INT(RND * _HEIGHT)), _RGB32(200) ' whites
LOOP UNTIL c = 2000 ' was 3600
c = 0
DO
c = c + 1
PSET ((INT(RND * _WIDTH)), INT(RND * _HEIGHT)), _RGB32(70) ' grays
LOOP UNTIL c = 6000 ' was 3600
c = 0
DO
c = c + 1
PSET ((INT(RND * _WIDTH)), INT(RND * _HEIGHT)), _RGB32(255, 67, 55, 124) ' reddies
DRAW "S2U1R1D1L1"
LOOP UNTIL c = 26
c = 0
DO
c = c + 1
PSET ((INT(RND * _WIDTH)), INT(RND * _HEIGHT)), _RGB32(0, 255, 0, 116) ' greenies
DRAW "S2U1R1D1L1"
LOOP UNTIL c = 86
c = 0
DO
c = c + 1
PRESET ((INT(RND * _WIDTH)), INT(RND * _HEIGHT)), _RGB32(255, 255, 183, 120) ' big yellows
DRAW "S4U1R1D1L1"
LOOP UNTIL c = 130
starScape = _COPYIMAGE(virtual, 32) ' software image
_DEST 0
_FREEIMAGE virtual
END SUB
' -----------------------------------------