True 3d Stars

Post Reply
DrChip
Posts: 167
Joined: Wed Oct 22, 2014 3:26 pm
My devices: iPhone 4 to 6+,iPad mini to iPad air 2

True 3d Stars

Post by DrChip »

Code: Select all

rem True 3D Stars  
rem iPhone 6 Plus / iOS 8.4 b3 / 4.8
rem Enjoy...
'It's a nice interactive  3D starfield Demonstration
'and  this is a true 3D starfield.. The others I did
'before never had rotations This looks nicer somehow

gosub initialize

'umode = -1 ' 1 on -1 off
mmm=16384

loop:
refresh off
graphics clear 10/255,10/255,30/255
'fill color 10/255,10/255,30/255
'fill tri 0,0, sw,sh, 0,sh
'fill tri 0,0, sw,sh, sw,0

gosub rotate
gosub draw
gosub display
refresh on
goto loop


display:
'The interacitve bit. Also does text stuff. 

xin=1
yin=1
zin=2

if umode=1 then
if accel_x()<0 then xin=-5
if accel_y()>0 then yin=5
if accel_y()<0 then yin=-5
if accel_x()>0 then xin=5
if accel_z()>0 then zin=-5
if accel_z()<0 then zin=5
draw text "THE INTERACTIVE STARFIELD DEMO" at 0,30
draw text "USER CONTROL MODE : ON! PRESS TURN OFF" at 0,50
draw text "TRY TILTING YOUR IDEVICE!" at 0,70
if xin>0 then xin=xin-1
if xin<0 then xin=xin+1
if yin>0 then yin=yin-1
if yin<0 then yin=yin+1
if zin>0 then zin=zin-1
if zin<0 then zin=zin+1
end if
draw text mid$(s$,p,67) at scx,sh-20
scx=scx-2
if scx<-10 then
 scx=scx+10
 p=p+1
 if p>len(s$)-1 then p=0
end if
return


draw:
'Draw The object
b=1
for a=1 to polys
sz=((80-tz(a))/14)
fill color sz/255,sz/255,(sz*25)/255
fill rect tx(a)-sz,ty(a)-sz to tx(a)+sz,ty(a)+sz
fill color 50/255,250/255,250/255
fill rect tx(a)-2,ty(a)-2 to tx(a)+2,ty(a)+2
next a
return

rotate:

'Rotate And Scale Each Point Store Result 
 for a=1 to polys
z(a)=z(a)+2
if z(a)>50 then z(a)=z(a)-100
  x1=x(a)
  y1=y(a)
  z1=z(a)

'X,Y,Z rotations

  xx=x1
  yy=y1*cs(xr)+z1*sn(xr)
  zz=z1*cs(xr)-y1*sn(xr)
  y1=yy
  x1=xx*cs(yr)-zz*sn(yr)
  z1=xx*sn(yr)+zz*cs(yr)
  zz=z1
  xx=x1*cs(zr)-y1*sn(zr)
  yy=x1*sn(zr)+y1*cs(zr)

'Apply Perspective

  xx=size*(xx/((zz/70)+1))+sw/2
  yy=size*(yy/((zz/70)+1))+sh/2
  tx(a)=xx
  ty(a)=yy
  tz(a)=zz
 next a
xr=xr+xin
yr=yr+yin
zr=zr+zin
if xr>=720 then xr=xr-720
if yr>=720 then yr=yr-720
if zr>=720 then zr=zr-720
if xr<=0 then xr=xr+720
if yr<=0 then yr=yr+720
if zr<=0 then zr=zr+720
return


initialize:

'Open Gfx Screen
graphics
sw=screen_width()
sh=screen_height()
s$="                                                                "
s$=s$&"   The interactive starfield demo..  "
s$=s$&"This one uses a true 3D starfield code. The other "
s$=s$&"basic ones you have seen are silly... I wanted "
s$=s$&"to see if it could be achieved properly."
s$=s$&" try the interactive mode. Enjoy!"

p=0
scx=0
'Define the necessary variables
size=6 ' how big do you want it?
pi=3.1415
polys=70       '  The amount of Stars.
dim x(polys+1) '  Original X co-ordinate store
dim y(polys+1) '  Original Y co-ordinate store
dim z(polys+1) '  Original Z co-ordinate store
dim tx(polys+1) ' Transformed  X co-ordinate store
dim ty(polys+1) ' Transformed Y co-ordinate store
dim tz(polys+1) ' Transformed Z co-ordinate store
umode=-1 ' user control mode on/off 1/0.
mmm=0.0
 
'Define Sine Tables
 dim cs(721)
 dim sn(721)
 for ang=0 to 720
  cs(ang)=cos(ang*(pi/360))
  sn(ang)=sin(ang*(pi/360))
 next ang

'Create Starfield
mmm=-25
for a=1 to polys 
x(a)=-40+rnd(80)
y(a)=-40+rnd(80)
z(a)=mmm
mmm=mmm+100/polys
next a
return
Attachments
image.jpg
image.jpg (111.52 KiB) Viewed 2603 times
image.jpg
image.jpg (91.79 KiB) Viewed 2603 times
image.jpg
image.jpg (91.51 KiB) Viewed 2603 times

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: True 3d Stars

Post by rbytes »

This is a fantastic star field display. I feel like I am on the Enterprise. Thanks for sharing.

Any chance you could add a forward-reverse variable so that if desired, the viewer could fly forward through the stars?
The only thing that gets me down is gravity...

DrChip
Posts: 167
Joined: Wed Oct 22, 2014 3:26 pm
My devices: iPhone 4 to 6+,iPad mini to iPad air 2

Re: True 3d Stars

Post by DrChip »

Hi! Thank you!

The z movment controls the depth going forward and backward. It's tricky. You can make it a fixed number -5 forward ,+5 backward.

Enjoy! :)

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: True 3d Stars

Post by rbytes »

Could you please post the code with z changed as you suggest? I have tried changing it and the direction of the stars hasn't changed.

Thanks
The only thing that gets me down is gravity...

Post Reply