Hi Sedit, no offense at all from your side, wasn't sure you had seen my post, since going through all your RL occurances might have easily happened.
A few points:
If you plan using Sparky's that can make things only go better about performance. I never used it therefore I don't know how it works. I suspect it uses collision detection by grouping objects and checking collision of those as a whole. In that case single object identity which you may need later on for tweaking may go lost. But I'm just badly speculating here.
Regarding the "pedantic and heavy duty" cycling many times through all objects, I don't see short cuts available here if you need to keep track of everything. One possible way is to do things in steps in the main loop like
if stepflag = 0 then gosub manage characters
if stepflag = 1 then gosub check enemy
if stepflag = 2 then gosub check food
etc..
Fix object pivot:
does not have an impact on frame rate at all if you don't use it , or use it sparingly, at run time.
That's for changing the default orientation of your object which may be rotated upwards, downwards or facing you or facing away from you and you need it reoriented . It's usually used for that task during setup when loading or creating objects which you may need to be rotated in another way (see the cone example).
Your code still doesn't work for me, but it's my DBP at fault , not your code.
I'm including 2 new versions of my first code.
The first one relies on a plane faking the view cone of the character, but at the target object position, which gets perpendicular to the character direction when a certain distance is reached and then intersect object checks distance. The small sphere is the pointing direction of the intersect function just to show the behaviour. in the real program it won't be there at all and the planes can be made invisible with "hide object" command.
The second one builds a check triangle ( fake, pure positions computing but no objects ) in front of the character (two additional small spheres and the character center make the triangle vertices) simulating the character field of view. This is tweakable at will of course, my choice was only for example sake.
It uses intersect object across the far vertices . As soon as an object is in the way the value becomes non-zero and triggers action. Not ideal because it only senses the front . For a complete triangle check we should cast rays from the character to the 2 front vertices also (ahead side spheres).
But I don't know if your characters will be somewhat driven by the player or it's just random movement.
This doesn't have a square root distance check (or quadratic value check) for vicinity. Most likely it will need to be reintroduced as the number of objects grow. The intersect object function is also very heavy duty, in some of my projects, where i need collision check for thousands of trees there is no way out other than to use it once for the closest object once detected with the square root distance.
I haven't included detection collision between characters, but basically it will go along the lines of, once again, cycling through them .
These are just sketchy proposals of approaches and will probably work only up to a certain object number.
All the best
Scorpyo
example 1
Rem Project: Check-Enemy-Food v2
Rem Created: 27/09/2017
Rem by Scorpyo
__Top:
Rem ***** Main Source File *****
set display mode 1920,1080,32
set global collision off
hide mouse
sync on:sync rate 60
backdrop on
characters=10
dim cflag(characters)
gosub Setupland1
gosub Setambient
gosub MakeFoodObjects
gosub MakeEnemies
gosub MakeCharacters
gosub MakeDummies
set camera range 10,landsize*2
gosub Resetcam
sync
__Main:
do
gosub ManageCharacters
gosub CheckBorders
gosub CheckFood
gosub CheckEnemy
gosub CameraControl
gosub camera_tweaks
gosub update_camera
gosub Printdata
rem Update screen
sync
loop
ManageCharacters:
for n=1 to characters
if cflag(n)=0 then move object n,2.0
next n
return
CheckBorders:
for n=1 to characters
cx#=object position x(n)
cy#=object position y(n)
cz#=object position z(n)
cay#=object angle y(n)
if cx#<=10 or cx#>=9990 then cay#=cay#+90:yrotate object n,cay#
if cz#<=10 or cz#>=9990 then cay#=cay#+90:yrotate object n,cay#
next n
return
CheckFood:
for n=1 to characters
cx#=object position x(n)
cy#=object position y(n)
cz#=object position z(n)
cay#=object angle y(n)
rem displacement value for intersect object (600 ahead)
aheadx#=newxvalue(cx#,cay#,600)
aheady#=newyvalue(cy#,cay#,600)
aheadz#=newzvalue(cz#,cay#,600)
position object n+200,aheadx#,5,aheadz#
for f=11 to 20
fx#=object position x(f)
fy#=object position y(f)
fz#=object position z(f)
fdiffx#=abs(cx#-fx#):fdiffz#=abs(cz#-fz#)
rem quadratic value instead of sqrt
fdist#=(fdiffx#*fdiffx#)+(fdiffz#*fdiffz#)
if fdist#<360000
yrotate object f+100,cay#
fdistcheck#=intersect object (f+100,cx#,5,cz#,aheadx#,5,aheadz#)
if fdistcheck#>0 and fdistcheck#<600
point object n,fx#,0,fz#
cay#=object angle y(n)
food_dist#=intersect object (f,cx#,5,cz#,aheadx#,5,aheadz#)
if food_dist#>0 and food_dist#<20 then cflag(n)=1
rem return
endif
endif
next f
next n
return
CheckEnemy:
for n=1 to characters
cx#=object position x(n)
cy#=object position y(n)
cz#=object position z(n)
cay#=object angle y(n)
aheadx#=newxvalue(cx#,cay#,600)
aheady#=newyvalue(cy#,cay#,600)
aheadz#=newzvalue(cz#,cay#,600)
for e=21 to 30
ex#=object position x(e)
ez#=object position z(e)
ediffx#=cx#-ex#:ediffz#=cz#-ez#
rem quadratic value instead of sqrt
edist#=(ediffx#*ediffx#)+(ediffz#*ediffz#)
if edist#<360000
yrotate object e+100,cay#
edistcheck#=intersect object(e+100,cx#,5,cz#,aheadx#,5,aheadz#)
if edistcheck#>0 and edistcheck#<600 then cay#=cay#+180+rnd(45):yrotate object n,cay#
endif
next e
next n
return
__Ambient:
Setupland1:
rem ground textures
rem create texture
cls rgb(0,100,20)
inkcolor#=rgb(255,255,255)
line 0,0,0,250
line 1,1,1,250
line 2,2,2,250
line 0,0,250,0
line 1,1,250,1
line 2,2,250,2
line 0,0,250,250
line 0,250,250,0
rem next n
get image 2,0,0,250,250
rem Make landscape
mtxrandomize=1
landsize=10000
grid=40
make matrix 1,landsize,landsize,grid,grid
set matrix 1,1,0,0,1,1,1,1
prepare matrix texture 1,2,1,1
randomize matrix 1,mtxrandomize
rem ***********
rem get ground done
update matrix 1
return
Setambient:
rem Create fog and ambience
set ambient light 60
return
__BuildObjecs:
MakeCharacters:
rem cones
randomize 100
for n=1 to 10
make object cone n,100
scale object n,80,100,140
rem point tip forward
xrotate object n,80
fix object pivot n
set object cull n,0
xpos#=rnd(landsize-1000):zpos#=rnd(landsize-1000)
position object n,xpos#,get ground height(1,xpos#,zpos#),zpos#
rot=rnd(360)
yrotate object n,rot
next n
return
MakeFoodObjects:
randomize 200
for n=11 to 20
make object sphere n,80
color object n,rgb(0,0,255)
rem dummy plane - object number = food number +100
make object plain n+100,1200,200
set object n+100,1,1,0
xpos#=rnd(landsize):zpos#=rnd(landsize)
position object n,xpos#,get ground height(1,xpos#,zpos#)+25,zpos#
position object n+100,xpos#,get ground height(1,xpos#,zpos#),zpos#
next n
return
MakeEnemies:
for n=21 to 30
make object cube n,60
color object n,rgb(255,0,0)
rem dummy cube - object number = enemy number +100
make object plain n+100,1200,200
set object n+100,1,1,0
xpos#=rnd(landsize):zpos#=rnd(landsize)
position object n,xpos#,get ground height(1,xpos#,zpos#)+25,zpos#
position object n+100,xpos#,get ground height(1,xpos#,zpos#),zpos#
next n
return
Makedummies:
for n=1 to 10
make object sphere n+200,30
next n
return
__Print_Routine:
Printdata:
set cursor 0,0
set text opaque
rem debug check
print " Polygons=",statistic(1)
print " Fps=",screen fps()
print
print " camera x#= ",camx#
print " camera z#= ",camz#
print " camera y angle= ",cameray#
print " camera x angle= ",cax#
print " camera height= ",camhgt
print
print " Arrows = Drive Camera"
print " z , x raise / lower camera"
print " c , v = Tilt camera"
print " mousewheel = Raise/Lower Camera"
print " < = Reset Camera"
print
print " cubes = ENEMIES"
print " spheres = FOOD"
print
return
rem **********CAMERA*****************
__Camera_routines:
CameraControl:
rem ****Character walk ****
if upkey()=1 and cs#<12 then cs#=cs#+2
if downkey()=1 and cs#>-12 then cs#=cs#-2
if cs#>0.0 then cs#=cs#-0.5
if cs#<0.0 then cs#=cs#+0.5
if leftkey()=1 then cameray#=wrapvalue(cameray#-4)
if rightkey()=1 then cameray#=wrapvalue(cameray#+4)
return
camera_tweaks:
mymousez=mousemovez()
camhgt=camhgt+mymousez
if inkey$()="z" then camhgt=camhgt+10
if inkey$()="x" then camhgt=camhgt-10
if inkey$()="c" then cax#=cax#+2
if inkey$()="v" then cax#=cax#-2
if inkey$()="<" then gosub Resetcam
return
rem camera update
update_camera:
camx#=newxvalue(camx#,cameray#,cs#)
camz#=newzvalue(camz#,cameray#,cs#)
position camera camx#,camy#+camhgt,camz#
yrotate camera cam#
xrotate camera cax#
return
rem camera reset
Resetcam:
camx#=5000:camy=0:camz#=0:cameray#=0
camhgt=4700
camrot=0
cax#=56
return
end
Example 2
Rem Project: Check-Enemy-Food v4
Rem Created: 27/09/2017
Rem by Scorpyo
__Top:
Rem ***** Main Source File *****
set display mode 1920,1080,32
set global collision off
hide mouse
sync on:sync rate 60
backdrop on
characters=10
dim cflag(characters)
gosub Setupland1
gosub Setambient
gosub MakeFoodObjects
gosub MakeEnemies
gosub MakeCharacters
gosub MakeDummies
set camera range 10,landsize*2
gosub Resetcam
sync
__Main:
do
gosub ManageCharacters
gosub CheckBorders
gosub CheckFood
gosub CheckEnemy
gosub CameraControl
gosub camera_tweaks
gosub update_camera
gosub Printdata
rem Update screen
sync
loop
ManageCharacters:
for n=1 to characters
if cflag(n)=0 then move object n,2.0
next n
return
CheckBorders:
for n=1 to characters
cx#=object position x(n)
cy#=object position y(n)
cz#=object position z(n)
cay#=object angle y(n)
if cx#<=10 or cx#>=9990 then cay#=cay#+90:yrotate object n,cay#
if cz#<=10 or cz#>=9990 then cay#=cay#+90:yrotate object n,cay#
next n
return
CheckFood:
for n=1 to characters
cx#=object position x(n)
cy#=object position y(n)
cz#=object position z(n)
cay#=object angle y(n)
rem displacement value for intersect object
aheadx#=newxvalue(cx#,cay#,500)
aheady#=newyvalue(cy#,cay#,500)
aheadz#=newzvalue(cz#,cay#,500)
aheadx1#=newxvalue(cx#,cay#+45,1000)
aheady1#=newyvalue(cy#,cay#+45,1000)
aheadz1#=newzvalue(cz#,cay#+45,1000)
aheadx2#=newxvalue(cx#,cay#-45,1000)
aheady2#=newyvalue(cy#,cay#-45,1000)
aheadz2#=newzvalue(cz#,cay#-45,1000)
position object n+200,aheadx#,5,aheadz#
position object n+210,aheadx1#,5,aheadz1#
position object n+220,aheadx2#,5,aheadz2#
for f=11 to 20
fx#=object position x(f)
fy#=object position y(f)
fz#=object position z(f)
foodviewcheck#=intersect object (f,aheadx1#,5,aheadz1#,aheadx2#,5,aheadz2#)
if foodviewcheck#>0 then point object n,fx#,0,fz#
food_dist#=intersect object (f,cx#,5,cz#,aheadx#,5,aheadz#)
if food_dist#>0 and food_dist#<20 then cflag(n)=1
rem return
next f
next n
return
CheckEnemy:
for n=1 to characters
cx#=object position x(n)
cy#=object position y(n)
cz#=object position z(n)
cay#=object angle y(n)
aheadx#=newxvalue(cx#,cay#,500)
aheady#=newyvalue(cy#,cay#,500)
aheadz#=newzvalue(cz#,cay#,500)
aheadx1#=newxvalue(cx#,cay#+45,1000)
aheady1#=newyvalue(cy#,cay#+45,1000)
aheadz1#=newzvalue(cz#,cay#+45,1000)
aheadx2#=newxvalue(cx#,cay#-45,1000)
aheady2#=newyvalue(cy#,cay#-45,1000)
aheadz2#=newzvalue(cz#,cay#-45,1000)
position object n+200,aheadx#,5,aheadz#
position object n+210,aheadx1#,5,aheadz1#
position object n+220,aheadx2#,5,aheadz2#
for e=21 to 30
enemyviewcheck#=intersect object(e,aheadx1#,5,aheadz1#,aheadx2#,5,aheadz2#)
if enemyviewcheck#>0 then cay#=cay#+180+rnd(45):yrotate object n,cay#
next e
next n
return
__Ambient:
Setupland1:
rem ground textures
rem create texture
cls rgb(0,100,20)
inkcolor#=rgb(255,255,255)
line 0,0,0,250
line 1,1,1,250
line 2,2,2,250
line 0,0,250,0
line 1,1,250,1
line 2,2,250,2
line 0,0,250,250
line 0,250,250,0
rem next n
get image 2,0,0,250,250
rem Make landscape
mtxrandomize=1
landsize=10000
grid=40
make matrix 1,landsize,landsize,grid,grid
set matrix 1,1,0,0,1,1,1,1
prepare matrix texture 1,2,1,1
randomize matrix 1,mtxrandomize
rem ***********
rem get ground done
update matrix 1
return
Setambient:
rem Create fog and ambience
set ambient light 60
return
__BuildObjecs:
MakeCharacters:
rem cones
randomize 100
for n=1 to 10
make object cone n,100
scale object n,80,100,140
rem point tip forward
xrotate object n,80
fix object pivot n
set object cull n,0
xpos#=rnd(landsize-1000):zpos#=rnd(landsize-1000)
position object n,xpos#,get ground height(1,xpos#,zpos#),zpos#
rot=rnd(360)
yrotate object n,rot
next n
return
MakeFoodObjects:
randomize 200
for n=11 to 20
make object sphere n,80
color object n,rgb(0,0,255)
xpos#=rnd(landsize):zpos#=rnd(landsize)
position object n,xpos#,get ground height(1,xpos#,zpos#)+25,zpos#
next n
return
MakeEnemies:
for n=21 to 30
make object cube n,60
color object n,rgb(255,0,0)
xpos#=rnd(landsize):zpos#=rnd(landsize)
position object n,xpos#,get ground height(1,xpos#,zpos#)+25,zpos#
next n
return
Makedummies:
for n=1 to 30
make object sphere n+200,30
next n
return
__Print_Routine:
Printdata:
set cursor 0,0
set text opaque
rem debug check
print " Polygons=",statistic(1)
print " Fps=",screen fps()
print
print " camera x#= ",camx#
print " camera z#= ",camz#
print " camera y angle= ",cameray#
print " camera x angle= ",cax#
print " camera height= ",camhgt
print
print " Arrows = Drive Camera"
print " z , x raise / lower camera"
print " c , v = Tilt camera"
print " mousewheel = Raise/Lower Camera"
print " < = Reset Camera"
print
print " cubes = ENEMIES"
print " spheres = FOOD"
print
return
rem **********CAMERA*****************
__Camera_routines:
CameraControl:
rem ****Character walk ****
if upkey()=1 and cs#<12 then cs#=cs#+2
if downkey()=1 and cs#>-12 then cs#=cs#-2
if cs#>0.0 then cs#=cs#-0.5
if cs#<0.0 then cs#=cs#+0.5
if leftkey()=1 then cameray#=wrapvalue(cameray#-4)
if rightkey()=1 then cameray#=wrapvalue(cameray#+4)
return
camera_tweaks:
mymousez=mousemovez()
camhgt=camhgt+mymousez
if inkey$()="z" then camhgt=camhgt+10
if inkey$()="x" then camhgt=camhgt-10
if inkey$()="c" then cax#=cax#+2
if inkey$()="v" then cax#=cax#-2
if inkey$()="<" then gosub Resetcam
return
rem camera update
update_camera:
camx#=newxvalue(camx#,cameray#,cs#)
camz#=newzvalue(camz#,cameray#,cs#)
position camera camx#,camy#+camhgt,camz#
yrotate camera cam#
xrotate camera cax#
return
rem camera reset
Resetcam:
camx#=5000:camy=0:camz#=0:cameray#=0
camhgt=4700
camrot=0
cax#=56
return
end