I personally wouldn't free the entities all the time. Just set it all up once, and set up a grid of "occlusion area's", which are basically pivots. Then you parent your entity's to them upon creation, and you can easily hide/show occlusion areas's (and their child entities) on the fly. This will speed up your game because collision detection is not done on entities that are hidden - otherwise, the engine will have to check wheter your car collided with all entities. Also set up CameraRange so the engine doesn't have to draw things that are very far away.
I did this like this (these are just code snippets from troughout my game, you'll have to read them and modify them and put them in the right places if you want to use it) :
global OcclusionAreas(0,0)
type OcclusionArea
field entity%
field x%
field z%
end type
function GetOcclusionArea.OcclusionArea(x,z)
for o.OcclusionArea = each OcclusionArea
if o\x=x and o\z=z then
return o
end if
next
end function
; gVar_Width% is the width of my current level, I have an occlusion area every 32 units.
dim OcclusionAreas(gVar_Width%/32,gVar_Width%/32)
for i=0 to gVar_Width%/32
for j=0 to gVar_Width%/32
o.OcclusionArea=new OcclusionArea
o\entity=createpivot()
hideentity o\entity
o\x=i
o\z=j
positionentity o\entity,i*32,0,j*32
OcclusionAreas(i,j)=o
next
next
; this is to parent an entity to the pivots
o.OcclusionArea=OcclusionAreas(your_entity_x/32,your_entity_z/32)
entityparent your_entity_here,o\entity
Now, for every player (or race car in your case), you'll need a field to store the nearest occlusion area of the previous frame. Then you can easily call this code every frame (it first hides the player's previous occlusion area and it's surrounding area's, and then it show the current area and it's surrounding area's - which may be the same as the previous one off course. My game has multiple players (all controlable by the player, but they can be on different locations far from eachother), so they still need collision detection with everything, that's why I loop trough them and show/hide the occlusion area's for all of them.
for pl.player=each player
if not pl\prevArea=null then
for xc=pl\prevArea\x-1 to pl\prevArea\x+1
for zc=pl\prevArea\z-1 to pl\prevArea\z+1
if xc>0 and zc>0 and xc<(gVar_Width/32) and zc<(gVar_height/32)
o.occlusionarea=occlusionAreas(xc,zc)
hideentity o\entity
end if
next
next
end if
next
for pl.player=each player
currentArea.OcclusionArea=occlusionAreas(entityx(pl\entity)/32,entityz(pl\entity)/32)
if not currentArea=null then
for xc=currentArea\x-1 to currentArea\x+1
for zc=currentArea\z-1 to currentArea\z+1
if xc>0 and zc>0 and xc<(gVar_Width/32) and zc<(gVar_height/32)
o.occlusionarea=occlusionAreas(xc,zc)
showentity o\entity
end if
next
next
end if
next
I hope this is helpful, it gave my game a speed-increase of about 20-30 fps, and should work better than using freeentity/copyentity all the time. Only if you have a reeealy big level, you may not want to load it all at the same time and then you might need to free and copy the entities, but that can be done using a same area-principle.
BTW, if anyone thinks I'm doing this wrong, don't hesitate to tell me :) I've only been using blitz for the past 2/3 months. Oh, and there is no uppercasing in my code, because apparantly my editor displays all the functions and keywords with right casing, but when I copy-paste it, it's all lowercase.
|