Author Topic: 3D Stars  (Read 3814 times)

Peter

  • Guest
3D Stars
« on: November 17, 2012, 02:39:38 PM »
Hi,

Some funny stars.
Code: [Select]
screen(640,480,0,"3D Stars")
backcolor(0,0,0)
setframetimer(60)

function Rand(minZahl,maxZahl)
  maxZahl= (maxZahl-minZahl)
  return int(rnd()*maxZahl+minZahl)
end

sx,sy,m,col,anz,speed,xscreen,yscreen = 0,0,0,0,5000,2,640,480
stern_x ={}
stern_y ={}
stern_z ={}

for m=1,anz do
   stern_x[m]=0
   stern_y[m]=0
   stern_z[m]=0
end

function starinit()
  for m=1,anz do
    stern_x[m]= Rand(-(xscreen/2),(xscreen/2)) *128
    stern_y[m]= Rand(-(yscreen/2),(yscreen/2)) *128
    stern_z[m]= Rand(speed,255)
  end
end

starinit()
repeat
k=getkey()
alphachannel(75)
cls()

for m=1,anz do
   stern_z[m] = stern_z[m] - speed
   if stern_z[m] <= speed then
      stern_z[m] = 255
   end
   sx = (stern_x[m] / stern_z[m]) + (xscreen /2)
   sy = (stern_y[m] / stern_z[m]) + (yscreen /2)
   color(255,255,255)
   fillcircle(sx,sy,2)
end

redraw()
until k==27
closewindow()
 

Bereb

  • Guest
Re: 3D Stars
« Reply #1 on: November 17, 2012, 04:48:44 PM »
Fine  :)
May I take the liberty of posting one of my scripts, adapted from an SDLBasic one and modified with EGSL features, which is very similar to yours:

Code: [Select]
#!/usr/bin/egsl

openwindow(800,600,32,"3D Stars - EGSL")
alphachannel(160)
mousehide()

local random, abs = math.random, math.abs

numstars = 3000
x = {}
y = {}
s = {}

for i = 1, numstars do
   x[i] = random(800)-512
   y[i] = random(600)-368
   s[i] = random(4)+1
end

repeat
   key = getkey()
   clearscreen()
   for i = 1, numstars do
      x[i] = x[i]*1.02
      y[i] = y[i]*1.02
      if abs(x[i])>512 or abs(y[i])>368 then
         x[i] = random(512)-256
         y[i] = random(368)-184
      end
      colour(s[i]*32, s[i]*32, 255)
      fillcircle(512+x[i], 368+y[i],random(0.5,1))
   end
   redraw()
until key == 27

closewindow()

You can delete or comment the 2nd 'local' line. That doesn't modify speed ...


*** I'v corrected the code *** (18-nov-2012)
« Last Edit: November 18, 2012, 05:18:14 PM by Bereb »

Peter

  • Guest
Re: 3D Stars
« Reply #2 on: November 17, 2012, 05:26:18 PM »
Hi Bereb,

You said sdlbasic. I am there member.
I think this is by me.

I got an error here:  D:\EGSL\berebstar.lua:23: attempt to perform arithmetic on local 'x' (a table value)

kevin

  • Guest
Re: 3D Stars - help needed for NaaLaa version?
« Reply #3 on: November 17, 2012, 08:23:36 PM »
Hi - I can get a similar thing in NaaLaa, but the EGSL version seems smoother to me - anyone there to show a better version for NaaLaa?
Code: [Select]
rem ---------import required libraries----------
rem --------------------------------------------
 import "Speed.lib"
 constant:
rem we'll have 3000 stars
STARS 3000
 hidden:
rem set window size and position
 set window 0,0, 800,600
 set redraw off
visible:
rem initialise variables

rem arrays to hold x and y positions of each star, and a colour component for each
x#[STARS]
y#[STARS]
s[STARS]

rem set initial positions and colours for each star
for i = 0 to STARS - 1
x[i]=float(rnd(800) - 400)
y[i]=float(rnd(600) - 300)
s[i]=rnd(4) + 1
next


 shouldDraw = true
 do
rem prepare the drawing board
set color 0,0,0
 cls
 set color 255,255,255

rem move the stars one at a time
for i = 0 to STARS - 1
x[i]=x[i]*1.02
y[i]=y[i]*1.02
rem replace stars as they leave the window
if abs#(x[i])>400.0 or abs#(y[1])>300.0
x[i]=float(rnd(800)-400)
y[i]=float(rnd(600)-300)
endif
rem draw each star using its own colour
set color s[i]*32,s[i]*32,255
draw rect  int(x[i])+400,int(y[i])+300,3,3,true
next

redraw
shouldDraw = SPD_HoldFrameDraw(60)
 until keydown(27) or not running()




Edit....I've noticed that NaaLaa stops responding after a minute or so of running this, so maybe it needs a more efficient way of doing this in NaaLaa...it may on the other hand be my laptop - an older Centrino Duo, or maybe a silly mistake in the code above...I'll keep looking.....
« Last Edit: November 17, 2012, 08:34:53 PM by kevin »

Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Re: 3D Stars
« Reply #4 on: November 17, 2012, 08:42:43 PM »
Same here ... I tried to change the variable name to xc but then I got (even if I give xc a value of e.g. 1):
Quote
/home/markus/Examples-EGSL/stars2.lua:25: attempt to perform arithmetic on local 'xc' (a nil value)

Peter

  • Guest
Re: 3D Stars
« Reply #5 on: November 17, 2012, 09:32:13 PM »
Hi cyber,

The error was the double x!

This Bereb source code runs now.
Code: [Select]
openwindow(800,600,32,"3D Stars - EGSL")
alphachannel(120)
mousehide()

local random, abs = math.random, math.abs
local numstars,key,i,s = 3000,0,0,0

x = {}
y = {}

for i=1, numstars do
   x[i] = random(800)-512
   y[i] = random(600)-368
end

repeat
   key = getkey()
   clearscreen()
   for i=1,numstars do
      x[i] = x[i]*1.02
      y[i] = y[i]*1.02
      if abs(x[i])>512 or abs(y[i])>368 then
         x[i] = random(512)-256
         y[i] = random(368)-184
      end
      --'colour(s*32, s*32, 255)
      color(255,255,255)
      fillcircle(512+x[i],368+y[i],random(.5,1))
   end
   redraw()
until key == 27
closewindow()
« Last Edit: November 18, 2012, 11:54:38 AM by Peter »

kevin

  • Guest
Re: 3D Stars in NaaLaa
« Reply #6 on: November 18, 2012, 11:01:34 AM »
It seems the issue of NaLaa crashing with my earlier example can be fixed using a 2 dimensional array instead:
Code: [Select]
rem ---------import required libraries----------
rem --------------------------------------------
 import "Speed.lib"
 constant:
rem we'll have 3000 stars
STARS 3000
X 0
Y 1
S 2
rem STAR_IMG 0
 hidden:
rem set window size and position
 set window 0,0, 800,600


 set redraw off
visible:
rem initialise variables

rem array to hold x and y positions of each star, and a colour component for each
starfield#[STARS][3]

rem set initial positions and colours for each star
for i = 0 to STARS - 1
starfield[i][X]=float(rnd(800) - 400)
starfield[i][Y]=float(rnd(600) - 300)
starfield[i][S]=float(rnd(4) + 1)
next


 shouldDraw = true
 do
rem prepare the drawing board
set color 0,0,0
 cls
 set color 255,255,255

rem move the stars one at a time
for i = 0 to STARS - 1
starfield[i][X]=starfield[i][X]*1.02
starfield[i][Y]=starfield[i][Y]*1.02
rem replace stars as they leave the window
if abs#(starfield[i][X])>400.0 or abs#(starfield[i][Y])>300.0
starfield[i][X]=float(rnd(800)-400)
starfield[i][Y]=float(rnd(600)-300)
endif
rem draw each star using its own colour
set color int(starfield[i][S])*32,int(starfield[i][S])*32,255
draw rect  int(starfield[i][X])+400,int(starfield[i][Y])+300,4,4
next

redraw
shouldDraw = SPD_HoldFrameDraw(60)
 until keydown(27) or not running()

I think I still prefer the look of the ESGL version though....maybe because of its use of circles instead of my rectangles?
« Last Edit: November 18, 2012, 11:15:01 AM by kevin »

kevin

  • Guest
Re: 3D Stars
« Reply #7 on: November 18, 2012, 11:27:05 AM »
One final change, then I'll be moving on to other things....this makes the size of the "stars" vary for a more interesting result....


Code: [Select]
rem ---------import required libraries----------
rem --------------------------------------------
 import "Speed.lib"
 constant:
rem we'll have 3000 stars
STARS 3000
X 0
Y 1
S 2
SIZE 3

 hidden:
rem set window size and position
 set window 0,0, 800,600

 set redraw off
visible:
rem initialise variables

rem array to hold x and y positions of each star, and a colour component for each
starfield#[STARS][4]

rem set initial positions and colours for each star
for i = 0 to STARS - 1
starfield[i][X]=float(rnd(800) - 400)
starfield[i][Y]=float(rnd(600) - 300)
starfield[i][S]=float(rnd(4) + 1)
starfield[i][SIZE]=float(rnd(4) + 1)


next


 shouldDraw = true
 do
rem prepare the drawing board
set color 0,0,0
 cls
 set color 255,255,255

rem move the stars one at a time
for i = 0 to STARS - 1
starfield[i][X]=starfield[i][X]*1.02
starfield[i][Y]=starfield[i][Y]*1.02
rem replace stars as they leave the window
if abs#(starfield[i][X])>400.0 or abs#(starfield[i][Y])>300.0
starfield[i][X]=float(rnd(800)-400)
starfield[i][Y]=float(rnd(600)-300)
endif
rem draw each star using its own colour
set color int(starfield[i][S])*32,int(starfield[i][S])*32,255
draw rect  int(starfield[i][X])+400,int(starfield[i][Y])+300,int(starfield[i][SIZE]),int(starfield[i][SIZE])

next

redraw
shouldDraw = SPD_HoldFrameDraw(60)
 until keydown(27) or not running()

Bereb

  • Guest
Re: 3D Stars
« Reply #8 on: November 18, 2012, 05:07:00 PM »
I got an error here:  D:\EGSL\berebstar.lua:23: attempt to perform arithmetic on local 'x' (a table value)

Really sorry 
:-[

I don't know why in:
Code: [Select]
if abs(x[i])>512 or abs(y[i])>368 then
x[i] = random(512)-256
y[i] = random(368)-184
end
x[] and y[] became x and y when I posted my code in this forum ? (maybe because of the BBcode ?)
 ???

Hi Bereb,

You said sdlbasic. I am there member.
I think this is by me.
Fine. So thanks for that script which inspired me  :D
In a way we have been working as a team  ;)


P.S.: I confirm. The "bug" was due to BBcode: "i" between square brackets is used to mean "in italics". All the code was wrong. Now it's correct
« Last Edit: November 18, 2012, 05:26:20 PM by Bereb »

Peter

  • Guest
Re: 3D Stars
« Reply #9 on: November 18, 2012, 05:31:43 PM »
Hi Bereb,

Just I saw that you are also SdlBasic member.
Is a little boring there, at the moment and I think in the future too.

Bereb

  • Guest
Re: 3D Stars
« Reply #10 on: November 18, 2012, 05:50:42 PM »
Hi Peter,
I have not been there (SDLBasic forum) for a long time.
This forum is more active  :)
I think EGSL is more complete than SDLBasic. Lua gives it higher performances, while looking like Basic  ;)


P.S.: and since I'm on Linux, EGSL is more convenient, for me, than SDLBasic
« Last Edit: November 20, 2012, 06:33:14 PM by Bereb »