@Fluffy Rabbit
Quote: "You will find the same speed boost if your objects are positioned after your collisions are checked for."
One problem I can see with this is that your object would already be through the wall before you had a chance to test if it will go through the wall.
@Silverman
Interesting. Would you agree that essentially you're just breaking up the tasks to happen every other loop? Wouldn't that seem like a performance booster for just about anything. In your example though, collision testing in DBC is process heavy so it really makes a difference here.
One other optimization we can add to this mix is to dump all other DBC collision except for MAKE OBJECT COLLISION BOX flagged without rotating collision box. If you have generally even sized objects, you can wrap them in a collision box and the performance will improve greatly over the other collision (except static collision which is as fast if not faster).
Here's your code now using MAKE OBJECT COLLISION BOX. You'll noitce that the speed difference between OPTIMIZE=0 and OPTIMIZE=1 is very small but the program runs faster:
rem lock cli
remstart
the principle is the following:
(OPTIMIZATION OFF : (position object + object collision) in the same frame, ---> very slow.
(OPTIMIZATION ON : (position object) in an odd frame, and (object collision) in a even frame, ---> fast!
remend
autocam off
sync on
sync rate 0
set display mode 1024,768,32
rem 0=OFF, 1=ON
rem on my laptop, 37 with OPTIMIZE=0 gives the same result as 194 with OPTIMIZE=1
max_object=37 : OPTIMIZE=0
altkey=56
rem indispensable for SPYTIME() functions
maxbar=10
dim SPYTIMETIM(maxbar)
dim SPYTIMECOL(maxbar)
dim SPYTIMEKEY$(maxbar) : SPYTIMEKEY$(0)=str$(maxbar)
rem make scene
for i=1 to max_object
make object sphere i,10
color object i,rgb(0,200,0)
x=rnd(300)-150
y=rnd(100)-50
z=rnd(400)+200
position object i,x,y,z
ghost object on i
make object collision box i,-5,-5,-5,5,5,5,0
set object collision on i
next i
if OPTIMIZE=1 then GOTO optimiz1
rem OPTIMIZATION OFF
optimiz0:
rem only for "get real FPS"
mtimer=timer()
DO
rem get real FPS
inc cpt
dtime=timer()-mtimer
if dtime>=1000
inc mtimer,1000
Rfps=cpt
cpt=0
endif
rem position object + collision in same loop = slow!
for c=1 to max_object
rem move object
po=object position z(c)
if po>200
position object c,object position x(c),object position y(c),object position z(c)-1
else
rem set object collision off c
endif
SPYTIME("position")
rem change color object if collision
if object collision(c,0)=0
color object c,rgb(0,200,0)
else
color object c,rgb(200,0,0)
endif
SPYTIME("collisions")
next c
SPYGRAPH(altkey)
text 0,0,"optimize OFF"
text 0,20,"max_object="+str$(max_object)
text 0,40,"FPS="+str$(screen fps() )
text 0,60,"Loop Per Second="+str$(Rfps )
text 0,80,"Alt key = SPYGRAPH ON/OFF"
rem yrotate camera wrapvalue(camera angle y()+mousemovex()/10.0)
rem xrotate camera wrapvalue(camera angle x()+mousemovey()/10.0)
STARTSPYTIME()
SYNC
SPYTIME("display")
LOOP
end
rem OPTIMIZATION ON
optimiz1:
VALU=0
rem only for "get real FPS"
mtimer=timer()
DO
rem get real FPS
inc cpt
dtime=timer()-mtimer
if dtime>=1000
inc mtimer,1000
Rfps=cpt
cpt=0
endif
rem frame 1 : VALU = 1 ;position object
rem frame 2 : VALU = 0 ;test object collision
rem frame 3 : VALU = 1 ;position object
rem . . .
VALU=(VALU!1)
select VALU
CASE 0
rem odd frame : position object and display text
rem move object
for c=1 to max_object
po=object position z(c)
if po>200
position object c,object position x(c),object position y(c),object position z(c)-1
else
rem set object collision off c
endif
next c
SPYTIME("position")
SPYGRAPH(altkey)
text 0,0,"optimize ON"
text 0,20,"max_object="+str$(max_object)
text 0,40,"FPS="+str$(screen fps() )
text 0,60,"Loop Per Second="+str$(Rfps )
text 0,80,"Alt key = SPYGRAPH ON/OFF"
rem yrotate camera wrapvalue(camera angle y()+mousemovex()/10.0)
rem xrotate camera wrapvalue(camera angle x()+mousemovey()/10.0)
STARTSPYTIME()
SYNC
SPYTIME("display")
ENDCASE
CASE 1
rem even frame : collision
for c=1 to max_object
rem change color object if collision
if object collision(c,0)=0
color object c,rgb(0,200,0)
else
color object c,rgb(200,0,0)
endif
next c
SPYTIME("collisions")
ENDCASE
endselect
LOOP
end
end
`=================================================
`=================================================
`=================================================
remstart
STARTSPYTIME()
SPYTIME(key$)
SPYGRAPH(display_key)
remend
function SPYGRAPH(display_key)
rem display_key = keystate code
x=0
y=20
rem 1 millisecond = 1 pixel * scale_box_height
scale_box_height=1
rem in pixels
box_width=20
oldkeystate=curkeystate
curkeystate=keystate(display_key)
if curkeystate>oldkeystate then display=(display!1)
if display
screenh=screen height()
lony=100*scale_box_height
mx=mousex()
my=mousey()
ox=x
oy=y
maxbar=val(SPYTIMEKEY$(0))
ink rgb(255,255,255),0
lonx=maxbar*box_width
line x,screenh-y,x+lonx,screenh-y
line x,screenh-y,x,screenh-y+lony
fin=SPYTIMECOL(0)
for moi=1 to fin
deltat=SPYTIMETIM(moi)
coul=SPYTIMECOL(moi)
ink coul,0
x1=x
y1=(screenh-y)-deltat*scale_box_height
if y1<0 then y1=0
x2=x+box_width
y2=screenh-y
box x1,y1,x2,y2
if mx>=x1 and mx<=x2
rem if my>=y1 and my<=y2
key$=SPYTIMEKEY$(moi)
rem tim=SPYTIMETIM(moi)
c$="SPYTIME(" + key$ + ")= "
c$=c$+ str$(deltat) + " ms"
text ox,screenh-oy,c$
rem endif
endif
inc x,(box_width+1)
next moi
ink rgb(255,255,255),0
endif
rem reset
SPYTIMETIM(0)=timer()
SPYTIMECOL(0)=0
endfunction
function SPYTIME(key$)
maxbar=val(SPYTIMEKEY$(0))
trouv=0
oldtime=SPYTIMETIM(0)
moi=SPYTIMECOL(0)
cumultime=0
for i=1 to maxbar
if SPYTIMEKEY$(i)=key$
trouv=1
moi=i
cumultime=SPYTIMETIM(moi)
endif
next i
if trouv=0
inc moi
SPYTIMECOL(0)=moi
if SPYTIMECOL(moi)=0
r=rnd(191)+64
g=rnd(191)+64
b=rnd(191)+64
coul=rgb(r,g,b)
SPYTIMECOL(moi)=coul
endif
SPYTIMEKEY$(moi)=key$
endif
currtime=timer()
SPYTIMETIM(moi)=(currtime-oldtime)+cumultime
SPYTIMETIM(0)=currtime
endfunction
function STARTSPYTIME()
maxbar=val(SPYTIMEKEY$(0))
for i=1 to maxbar
SPYTIMETIM(i)=0
SPYTIMEKEY$(i)=""
next i
SPYTIMETIM(0)=timer()
SPYTIMECOL(0)=0
endfunction
Enjoy your day.