Post by tsh73 on Apr 23, 2018 20:49:11 GMT
Just to say.
JB 2.0 makes this program work - I only had to remove spritecenter - seems not affected by it, not much anyway
(JB1.01 - doesn't work, needs more sprite commands)
Also added flag to add sprites only once - indeed memory usage was growing without it.
JB 2.0 makes this program work - I only had to remove spritecenter - seems not affected by it, not much anyway
(JB1.01 - doesn't work, needs more sprite commands)
Also added flag to add sprites only once - indeed memory usage was growing without it.
''bluatigro 21 apr 2018
''robot sim : whit 3d sprites
global mmax
mmax = 20
gosub [basis3D]
global rotx , roty , rotz , trans , temp , number , pi
trans = mmax + 1
rotx = mmax + 2
roty = mmax + 3
rotz = mmax + 4
temp = mmax + 5
pi = atn( 1 ) * 4
global xyz , xzy , yxz , yzx , zxy , zyx
xzy = 1
yxz = 2
yzx = 3
zxy = 4
zyx = 5
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global winx , winy , angle , pi , day , year
global s.max , s.tel , state , key$ , factor
global toRad, spritesAdded
toRad=pi / 180
winx = WindowWidth
winy = WindowHeight
pi = atn( 1 ) * 4
s.max = 40
year = 365.26
factor = 1
dim s.x( s.max ) , s.y( s.max ) , s.z( s.max ) , ry( s.max )
nomainwin 'ms per frame prints there
open "robot sim" for graphics as #m
#m "trapclose [quit]"
call sprite.cls
' call sprite.rond 30,30 , 25 _
' , "yellow" , "yellow" , 1
call sprite.rond3d 30,30 , 25 _
,255,255,0, 1
#m "getbmp yellow25 0 0 60 120"
call sprite.cls
' call sprite.rond 30,30 , 10 _
' , "blue" , "blue" , 1
call sprite.rond3d 30,30 , 10 _
,0,0,255 , 1
#m "getbmp blue10 0 0 60 120"
call sprite.cls
' call sprite.rond 30,30 , 10 _
' , "red" , "red" , 1
call sprite.rond3d 30,30 , 10 _
,255,0,0, 1
#m "getbmp red10 0 0 60 120"
for i = 0 to s.max
ry( i ) = i
next i
#m "font 50 bold"
#m "fill black"
call text 100 , 100 , "robot sim ." _
, "green" , "black"
#m "getbmp screen 0 0 " ; winx ; " " ; winy
#m "background screen"
#m "when characterInput [key]"
#m "setfocus"
timer 40 , [timer]
wait
[timer]
scan
'---------add timer
'#m "font 30 bold"
'call text 10 , 150 , "";time$("ms")-t0;" ms per frame" _
', "green" , "black"
print time$("ms")-t0;" ms per frame"
t0=time$("ms")
'------------------
s.tel = 0
call skelet 0 , pend(angle,30) , 0 , 0
call skelet 1 , -30 , 0 , 0
call skelet 2 , pend(angle+180,30) , 0 , 0
call skelet 3 , -30 , 0 , 0
call skelet 4 , pend(angle+180,30) , 0 , 0
call skelet 5 , pend(angle+90,30)+30 , 0 , 0
call skelet 6 , pend(angle,30) , 0 , 0
call skelet 7 , pend(angle-90,30)+30 , 0 , 0
call link 1 , 0,0,0 , angle/10,0,0 , xyz , 0
call add.atom 0 , 0 , 0 , "yellow25"
call add.atom 0 , 50 , 0 , "yellow25"
call child 2 , 30,20,0 , 0 , xzy , 1
call add.atom 0 , 0 , 0 , "red10"
call add.atom 0 , -20 , 0 , "red10"
call child 3 , 0,-40,0 ,1, xyz , 2
call add.atom 0,0,0 , "red10"
call add.atom 0,-20,0 , "red10"
call add.atom 0,-40,0 , "red10"
call child 2 , -30,20,0 , 2 , xzy , 1
call add.atom 0 , 0 , 0 , "blue10"
call add.atom 0 , -20 , 0 , "blue10"
call child 3 , 0,-40,0 , 3 , xyz , 2
call add.atom 0,0,0 , "blue10"
call add.atom 0,-20,0 , "blue10"
call add.atom 0,-40,0 , "blue10"
call child 2 , 15,-35,0 , 4 , yzx , 1
call add.atom 0,0,0 , "red10"
call add.atom 0,-20,0 , "red10"
call child 3 , 0,-40,0 , 5 , xyz , 2
call add.atom 0,0,0 , "red10"
call add.atom 0,-20,0 , "red10"
call add.atom 0,-40,0 , "red10"
call add.atom 0,-60,0 , "red10"
call add.atom 0,-60,-20 , "red10"
call child 2 , -15,-35,0 , 6 , yzx , 1
call add.atom 0,0,0 , "blue10"
call add.atom 0,-20,0 , "blue10"
call child 3 , 0,-40,0 , 7 , xyz , 2
call add.atom 0,0,0 , "blue10"
call add.atom 0,-20,0 , "blue10"
call add.atom 0,-40,0 , "blue10"
call add.atom 0,-60,0 , "blue10"
call add.atom 0,-60,-20 , "blue10"
spritesAdded=1
''sort atom's
for high = 1 to s.tel - 1
for low = 0 to high - 1
if s.z( ry( high ) ) < s.z( ry( low ) ) then
help = ry( high )
ry( high ) = ry( low )
ry( low ) = help
end if
next low
next high
angle = angle + 5
''draw molecule
for i = 0 to s.tel - 1
#m "spritetoback spr" ; ry( i )
#m "spritexy spr" ; ry( i ) ; " " _
; winx / 2 + s.x( ry( i ) ) ; " " _
; winy / 2 - s.y( ry( i ) ) _
- s.z( ry( i ) ) / 10
next i
#m "drawsprites"
angle = angle + 5
wait
[key]
key$ = right$( Inkey$ , 1 )
if key$ <> chr$( _VK_ESCAPE ) then wait
[quit]
close #m
end
sub text x , y , txt$ , l$ , b$
#m "goto " ; x ; " " ; y
#m "color " ; l$
#m "backcolor " ; b$
#m "down"
#m "\" ; txt$
#m "up"
end sub
sub sprite.cls
#m "fill white"
#m "goto 0 60"
#m "size 1"
#m "down"
#m "color black"
#m "backcolor black"
#m "boxfilled 60 120"
#m "up"
end sub
sub sprite.rect x1,y1,x2,y2,clr$
#m "goto " ; x1 ; " " ; y1
#m "size 1"
#m "color black"
#m "backcolor black"
#m "down"
#m "boxfilled " ; x2 ; " " ; y2
#m "up"
#m "goto " ; x1 ; " " ; y1 + 60
#m "color " ; clr$
#m "backcolor " ; clr$
#m "down"
#m "boxfilled " ; x2 ; " " ; y2 + 60
#m "up"
end sub
sub sprite.rond x,y,d,clr$,bclr$,size
#m "goto " ; x ; " " ; y
#m "size " ; size
#m "down"
#m "color black"
#m "backcolor black"
#m "circlefilled " ; d
#m "up"
#m "goto " ; x ; " " ; y + 60
#m "down"
#m "color " ; clr$
#m "backcolor " ; bclr$
#m "circlefilled " ; d
#m "up"
end sub
sub sprite.rond3d x,y,d,r,g,b,size
#m "goto " ; x ; " " ; y
#m "size " ; size
#m "down"
#m "color black"
#m "backcolor black"
#m "circlefilled " ; d
#m "up"
#m "goto " ; x ; " " ; y + 60
#m "down"
'in faked 3d
for RR=d to 1 step -1
a=(1-RR/d)^.5
clr$=rgb$(r*a,g*a,b*a)
#m "color " ; clr$
#m "backcolor " ; clr$
#m "place " ; x-(d-RR)/5 ; " " ; y-(d-RR)/5 + 60
#m "circlefilled " ; RR
'#m "circle " ; RR
next
#m "up"
end sub
sub remove.al.atoms
if s.tel = 0 then exit sub
for i = 0 to s.tel
#m "spriteimage spr" ; i ; " empty"
next i
s.tel = 0
end sub
sub add.atom x , y , z , i$
if not(spritesAdded) then
#m "addsprite spr" ; s.tel ; " " ; i$
'#m "centersprite spr" ; s.tel
end if
call spot x , y , z
s.x( s.tel ) = x
s.y( s.tel ) = y
s.z( s.tel ) = z
s.tel = s.tel + 1
end sub
function rad( deg )
rad = deg * pi / 180
end function
function rad2( d , i )
rad = d * pi * 2 / i
end function
function rgb$( r , g , b )
r = int( r ) and 255
g = int( g ) and 255
b = int( b ) and 255
rgb$ = str$( r ) ; " " ; g ; " " ; b
end function
function rainbow$( deg )
r = sin( rad( deg ) ) * 127 + 128
g = sin( rad( deg - 120 ) ) * 127 + 128
b = sin( rad( deg + 120 ) ) * 127 + 128
rainbow$ = rgb$( r , g , b )
end function
''bluatigro 18 nov 2016
''3d engine block
''needs math block
[basis3D]
dim m( ( mmax + 5 ) * 4 * 4 + 16 ) , cam( 6 )
dim skx( 64 ) , sky( 64 ) , skz( 64 )
call startmatrix
return
function pend( fase , amp )
pend = sin( toRad*fase ) * amp
end function
sub rotate byref k , byref l , deg
s = sin( toRad* deg )
c = cos( toRad* deg )
hk = k * c - l * s
hl = k * s + l * c
k = hk
l = hl
end sub
sub skelet lim , x , y , z
''for animating avatar lim's
skx( lim ) = x
sky( lim ) = y
skz( lim ) = z
end sub
sub child no , x , y , z , lim , ax , p
''for creating lim's of a avatar
if lim < 0 or lim > 64 then exit sub
call link no , x , y , z _
, sky( lim ) , skx( lim ) , skz( lim ) , ax , p
end sub
sub link no , x , y , z , xz , yz , xy , ax , p
''set draw matrix : wil efect future drawing
''no : number new matrix
''x,y,z : translation
''xz,yz,xy : rotation in degrees
''ax : sequence of axes
''p : number old matrix
if no < 1 or no > mmax then exit sub
if p < 0 or p > mmax then exit sub
if no < 1 or no > mmax then exit sub
if p < 0 or p > mmax then exit sub
if p = no then exit sub
''copy matrix 0 into matrix's
' call copy 0 , rotx
' call copy 0 , roty
' call copy 0 , rotz
' call copy 0 , trans
call copy0 rotx
call copy0 roty
call copy0 rotz
call copy0 trans
''create rotation matrix's
m( in( rotx , 1 , 1 ) ) = cos( toRad* yz )
m( in( rotx , 1 , 2 ) ) = 0-sin( toRad* yz )
m( in( rotx , 2 , 1 ) ) = sin( toRad* yz )
m( in( rotx , 2 , 2 ) ) = cos( toRad* yz )
m( in( roty , 0 , 0 ) ) = cos( toRad* xz )
m( in( roty , 0 , 2 ) ) = 0-sin( toRad* xz )
m( in( roty , 2 , 0 ) ) = sin( toRad* xz )
m( in( roty , 2 , 2 ) ) = cos( toRad* xz )
m( in( rotz , 0 , 0 ) ) = cos( toRad* xy )
m( in( rotz , 0 , 1 ) ) = 0-sin( toRad* xy )
m( in( rotz , 1 , 0 ) ) = sin( toRad* xy )
m( in( rotz , 1 , 1 ) ) = cos( toRad* xy )
''create translation matrix
m( in( trans , 3 , 0 ) ) = x
m( in( trans , 3 , 1 ) ) = y
m( in( trans , 3 , 2 ) ) = z
''select axes sequence [ 1 of 6 ] and act on i
select case ax
case xyz
call multiply rotx , roty , temp
call multiply temp , rotz , no
call multiply no , trans , temp
call multiply temp , p , no
case xzy
call multiply rotx , rotz , temp
call multiply temp , roty , no
call multiply no , trans , temp
call multiply temp , p , no
case yxz
call multiply roty , rotx , temp
call multiply temp , rotz , no
call multiply no , trans , temp
call multiply temp , p , no
case yzx
call multiply roty , rotz , temp
call multiply temp , rotx , no
call multiply no , trans , temp
call multiply temp , p , no
case zxy
call multiply rotz , rotx , temp
call multiply temp , roty , no
call multiply no , trans , temp
call multiply temp , p , no
case zyx
call multiply rotz , roty , temp
call multiply temp , rotx , no
call multiply no , trans , temp
call multiply temp , p , no
case else
call multiply rotx , roty , temp
call multiply temp , rotz , no
call multiply no , trans , temp
call multiply temp , p , no
end select
number = no
end sub
sub copy a , b
''matrix( b ) = matrix( a )
for i = 0 to 3
for j = 0 to 3
m( in( b , i , j ) ) = m( in( a , i , j ) )
next j
next i
end sub
sub copy0 b
''matrix( b ) = unity matrix
for i = 0 to 3
for j = 0 to 3
m( in( b , i , j ) ) = 0
next j
m( in( b , i , i ) ) = 1
next i
end sub
sub spot byref x , byref y , byref z
'''lokal coordinates to world coordinates
''x,y,z = matrix( number ) * x,y,z
no = number
hx = m( in( no , 0 , 0 ) ) * x _
+ m( in( no , 1 , 0 ) ) * y _
+ m( in( no , 2 , 0 ) ) * z _
+ m( in( no , 3 , 0 ) )
hy = m( in( no , 0 , 1 ) ) * x _
+ m( in( no , 1 , 1 ) ) * y _
+ m( in( no , 2 , 1 ) ) * z _
+ m( in( no , 3 , 1 ) )
hz = m( in( no , 0 , 2 ) ) * x _
+ m( in( no , 1 , 2 ) ) * y _
+ m( in( no , 2 , 2 ) ) * z _
+ m( in( no , 3 , 2 ) )
x = hx - cam( 0 )
y = hy - cam( 1 )
z = hz - cam( 2 )
call rotate x , y , 0 - cam( 5 )
call rotate y , z , 0 - cam( 4 )
call rotate x , z , 0 - cam( 3 )
if cam( 6 ) = 0 then cam( 6 ) = 1
x = x * cam( 6 )
y = y * cam( 6 )
z = z * cam( 6 )
end sub
sub camara x,y,z,pan,tilt,rol,zoom
cam( 0 ) = x
cam( 1 ) = y
cam( 2 ) = z
cam( 3 ) = pan
cam( 4 ) = tilt
cam( 5 ) = rol
cam( 6 ) = zoom
end sub
sub multiply a , b , c
''matrix( c ) = matrix( a ) * matrix( b )
for i = 0 to 3
for j = 0 to 3
' dest=in( c , i , j )
dest= c*16 + i + j*4
m( dest) = 0
for k = 0 to 3
' m( dest ) = m( dest ) _
' + m( in( a , i , k ) ) * m( in( b , k , j ) )
m( dest ) = m( dest ) _
+ m( a*16 + i + k*4 ) * m( b*16 + k + j*4 )
next k
next j
next i
end sub
sub startmatrix
''set startmatrix to unity
for x = 0 to 3
for y = 0 to 3
m( in( 0,x,y ) ) = 0
next y
m( in( 0,x,x ) ) = 1
next x
end sub
function in( no , x , y )
''LB4 has no 3d array's
''so i simulate them
in = x + y * 4 + no * 16
end function