Today has been code tidy up day, putting as much as I could into working functions and a change in the collision code, which allows for gravity. Now that almost everything engine related is set into functions, I feel like putting this stage in the engine up as open source, it should be fairly easy to use.
-Requires Sparky's DLL NOTE: SPARKY DLL VERSION 2 DOES NOT WORK, YOU CAN DOWNLOAD SPARKY'S DLL VERSION ONE IN A POST BELOW.
Engine Part 1:
`MAKE/LOAD OBJECT FUNCTIONS
Var:
`Varibles
global talk#
global camdis#
global cam1#
global g#
global gravity#
function player_object(play#,shadowflag#,slope#,radius#)
make object cube play#,70
if shadowflag# = 1 then set shadow shading on play#,0,500,1
setupobject play#,0,2
setupcollision(play#,slope#,radius#,60)
setcollisionon(play#)
endfunction
function player_object_load(file$,play#,shadowflag#,slope#,radius#)
load object file$,play#
if shadowflag# = 1 then set shadow shading on play#,0,500,1
setupobject play#,0,2
setupcollision(play#,slope#,radius#,60)
setcollisionon(play#)
endfunction
function scene_object(file$,scene#,bumpimg#)
load object file$,scene#
set bump mapping on scene#,bumpimg#
endfunction
type collision
enabled as boolean
object as integer
hradius as float
vradius as float
checks as integer
anglesize as float
slope as float
oldy as float
vstate as integer
vobject as integer
vslope as float
endtype
`COLLISION FUNCTIONS
function void()
if memblock exist(1)=1 then delete memblock 1
endfunction
function setupcollisionsystem()
global dim collision() as collision
endfunction
function destroycollisionsystem()
undim collision()
endfunction
function setupcollision(object,slope#,radius#,checks)
array insert at bottom collision(0)
number=array count(collision(0))
collision(number).oldy=object position y(object)
collision(number).object=object
collision(number).hradius=radius#
collision(number).vradius=radius#*tan(slope#)
collision(number).slope=slope#
collision(number).checks=checks
collision(number).anglesize=360/checks
endfunction
function destroycollision(object)
array delete element collision(0),findobject(object)
endfunction
function setcollisionon(object)
collision(findobject(object)).enabled=1
endfunction
function setcollisionoff(object)
collision(findobject(object)).enabled=0
endfunction
function getverticalobject(object)
returnobject=collision(findobject(object)).vobject
endfunction returnobject
function getverticalstate(object)
onground=collision(findobject(object)).vstate
endfunction onground
function getverticalslope(object)
slope#=collision(findobject(object)).vslope
endfunction slope#
function updatehorizontalcollision(object)
number=findobject(object)
x#=object position x(collision(number).object)
y#=object position y(collision(number).object)
z#=object position z(collision(number).object)
if collision(number).enabled
cd#=collision(number).hradius*2
for x=0 to collision(number).checks-1
a#=x*collision(number).anglesize
if intersectobject(0,0,x#,y#,z#,newxvalue(x#,a#,collision(number).hradius),y#,newzvalue(z#,a#,collision(number).hradius),0)>0
d#=getcollisiondistance()
if d#<cd#
cd#=d# : ca#=a#
cx#=getstaticcollisionx()
cz#=getstaticcollisionz()
endif
endif
next x
if cd#<collision(number).hradius
ta#=wrapvalue(ca#-180)
position object collision(number).object,newxvalue(cx#,ta#,collision(number).hradius),y#,newzvalue(cz#,ta#,collision(number).hradius)
endif
endif
endfunction
function updateverticalcollision(object)
number=findobject(object)
oy#=collision(number).oldy
x#=object position x(collision(number).object)
y#=object position y(collision(number).object)
z#=object position z(collision(number).object)
if collision(number).enabled
if oy#-y#>=0
if intersectobject(0,0,x#,oy#,z#,x#,y#-collision(number).vradius,z#,0)>0
cy#=getstaticcollisiony()
collision(number).vslope=acos(getcollisionnormaly())
if collision(number).vslope<collision(number).slope
collision(number).vobject=getobjecthit()
collision(number).vstate=1
else
collision(number).vobject=0
collision(number).vstate=0
endif
position object collision(number).object,x#,cy#+collision(number).vradius,z#
else
collision(number).vobject=0
collision(number).vslope=0
collision(number).vstate=0
endif
else
if intersectobject(0,0,x#,oy#,z#,x#,y#+collision(number).vradius,z#,0)>0
cy#=getstaticcollisiony()
collision(number).vslope=acos(getcollisionnormaly())
if collision(number).vslope>180-collision(number).slope
collision(number).vobject=getobjecthit()
collision(number).vstate=-1
else
collision(number).vobject=0
collision(number).vstate=0
endif
position object collision(number).object,x#,cy#-collision(number).vradius,z#
else
collision(number).vobject=0
collision(number).vslope=0
collision(number).vstate=0
endif
endif
endif
collision(number).oldy=object position y(collision(number).object)
endfunction
function findobject(object)
for x=0 to array count(collision(0))
if collision(x).object=object then number=x
next x
endfunction number
`this is the function for the collision box
Function CollBox(ob#)
obsx#= Object Size X(ob#): Rem Find the object size X
obsy#= Object Size Y(ob#): Rem Find the object size Y
obsz#= Object Size Z(ob#): Rem Find the object size Z
Make object Collision Box ob#,0-(obsx#/2),0-(obsy#/2),0-(obsz#/2),(obsx#/2),(obsy#/2),(obsz#/2),0
endfunction
`Now that the collision box is made, the function can be put in the main loop
Function SlideColl(pn#) : Rem pn# is PLAYER NUMBER
obx#= Object position x(pn#) : Rem Get the objects X position
oby#= Object position y(pn#) : Rem Get the objects Y position
obz#= Object position z(pn#) : Rem Get the objects Z position
If object collision(pn#,0)>0 : Rem here you may put NOT any objects you dont want collision with
dec obx#,get object collision x()
dec oby#,get object collision y()
dec obz#,get object collision z()
position object pn#,obx#,oby#,obz#
endif
endfunction
`-----------------------
`update bloom stuff
UpDate_Bloom:
`-----------------------
`Update bloom camera
position camera bloom_cam,camera position x(),camera position y(),camera position z()
rotate camera bloom_cam,camera angle x(),camera angle y(),camera angle z()
`Update screen object
dis#=(sh#/2)/tan(fov/2)
position object bloom_obj+2,camera position x(),camera position y(),camera position z()
rotate object bloom_obj+2,camera angle x(),camera angle y(),camera angle z()
move object bloom_obj+2,dis#/400
move object up bloom_obj+2,0.01
move object left bloom_obj+2,0.01
scale object bloom_obj+2,101,101,0
return
`-----------------------
`-----------------------
`Set up bloom objects and cameras
SetUp_Bloom:
`-----------------------
fov=70
sw#=screen width()
sh#=screen height()
dis#=(sh#/2)/tan(fov/2)
brightness#=0.13
blurWidth#=0.0275
bloom_cam=50
bloom_img=50
bloom_obj=50
`Camera
set camera range 0.5,100000
set camera fov fov
`Screen grabbing Camera
make camera bloom_cam
set camera to image bloom_cam,bloom_img,256,256
set camera range bloom_cam,3,100000
set camera fov bloom_cam,fov
`Tone
make object plain bloom_obj,sw#/100,sh#/100
load effect "Bloom.fx",bloom_obj,0
texture object bloom_obj,0,bloom_img
set object effect bloom_obj,bloom_obj
position object bloom_obj,0,-10000,dis#/100
make camera bloom_cam+1
set camera to image bloom_cam+1,bloom_img+1,256,256
set camera range bloom_cam+1,1,100
set camera fov bloom_cam+1,fov
position camera bloom_cam+1,0,-10000,0
`Blur X
make object plain bloom_obj+1,sw#/100,sh#/100
load effect "Bloom.fx",bloom_obj+1,0
texture object bloom_obj+1,bloom_img+1
set object effect bloom_obj+1,bloom_obj+1
position object bloom_obj+1,500,-10000,dis#/100
make camera bloom_cam+2
set camera to image bloom_cam+2,bloom_img+2,256,256
set camera range bloom_cam+2,1,100
set camera fov bloom_cam+2,fov
position camera bloom_cam+2,500,-10000,0
`Screen object + Blur Y
make object plain bloom_obj+2,-sw#/(400),sh#/(400)
load effect "Bloom.fx",bloom_obj+2,0
texture object bloom_obj+2,0,bloom_img+2
set object effect bloom_obj+2,bloom_obj+2
ghost object on bloom_obj+2
`Set effect data
set effect technique bloom_obj,"Tone"
set effect technique bloom_obj+1,"blurpassX"
set effect technique bloom_obj+2,"blurpassY"
set effect constant float bloom_obj+1,"Brightness",brightness#
set effect constant float bloom_obj+1,"blurWidth",blurWidth#
set effect constant float bloom_obj+2,"Brightness",brightness#
set effect constant float bloom_obj+2,"blurWidth",blurWidth#
return
`-----------------------
function MakeWater()
`Globals
global ObjNum=500
global ImgNum=500
global FXNum=500
global WaterHeight#=-70.0
global WaterCamY#
`Setup Refraction Camera
` make camera 30
` set camera range 30,1,15000
` set camera aspect 30,1.325
` backdrop off 30
` set camera to image 30,ImgNum,256,256 ` set to 256 to speed things up
` set camera fov 30,65
`Setup Reflection Camera
` make camera 31
` set camera range 31,1,15000
` set camera aspect 31,1.325
` backdrop off 31
` set camera to image 31,ImgNum+1,256,256
` set camera fov 31,65
`Make Water plain
` load image "Waves2.dds",ImgNum+2
` load image "WaterMask.bmp",ImgNum+3
make object plain ObjNum,5000,5000
texture object ObjNum,0,ImgNum+2
` texture object ObjNum,1,ImgNum
` texture object ObjNum,2,ImgNum+1
` texture object ObjNum,3,ImgNum+3
load effect "Water.fx",FXNum,0
`set object effect ObjNum,FXNum
xrotate object ObjNum,270
` set object transparency ObjNum,2
set object specular ObjNum,rgb (10,30,100)
color object ObjNum,rgb(50,70,255)
ghost object on ObjNum
` set reflection shading on ObjNum
position object ObjNum,object position x(ObjNum),-70,object position z(ObjNum)
endfunction
`------------------
`------------------
` UpDate Water
`------------------
function UpDateWater()
`UpDate Water plain
position object ObjNum,object position x(ObjNum),WaterHeight#,object position z(ObjNum)
WaterCamY#=camera position y()-WaterHeight#
Hide object ObjNum
`Upade Refract/Reflect cameras
UpDateRefraction()
UpDateReflection()
`Show Water
show object ObjNum
`Mask
sync mask 0x3fffffff
endfunction
`------------------
`------------------
` Refraction Update
`------------------
function UpDateRefraction()
`UpDate Refraction camera
position camera 30,camera position x(),camera position y(),camera position z()
rotate camera 30,camera angle x(),camera angle y(),camera angle z()
`Clip Camera
`set camera clip 30,1,0,WaterHeight#+(5+WaterCamY#/25),0,0,-1,0
`set camera clip 31,0,0,0,0,0,0,0
`Only sync Camera 30
sync mask 0x40000000
fastsync
endfunction
`------------------
`------------------
` Reflection Update
`------------------
function UpDateReflection()
`UpDate Reflection camera
position camera 31,camera position x(),WaterHeight#-WaterCamY#,camera position z()
rotate camera 31,-camera angle x(),camera angle y(),camera angle z()
`Clip Camera
ClipY#=WaterCamY#
if ClipY#>100 then ClipY#=100
`set camera clip 31,1,0,WaterHeight#-(1+(ClipY#/25)),0,0,1,0
`set camera clip 30,0,0,0,0,0,0,0
`Only sync Camera 31
sync mask 0x80000000
fastsync
endfunction
`----------
Part 2:
function control_object(play#,fwdis#,bwdis#,angle#)
cy# = camera angle y()
dy# = object angle y(play#)
if cam1# = 0 then yrotate object 1,cy#
`Switch the Cameras
if inkey$() = "2" then cam1# = 0
if inkey$() = "1" then cam1# = 1
`Movement via the WASD if the character isn't talking and movement dependant on what camera type is enabled
if keystate(17)=1 and talk# = 0 then move object play#,fwdis#
if keystate(31)=1 and talk# = 0 then move object play#,-bwdis#
if keystate(30)=1 and cam1# = 0 and talk# = 0 then yrotate camera cy#-angle#
if keystate(32)=1 and cam1# = 0 and talk# = 0 then yrotate camera cy# +angle#
if keystate(30)=1 and cam1# = 1 and talk# = 0 then yrotate object play#,dy#-angle#
if keystate(32)=1 and cam1# = 1 and talk# = 0 then yrotate object play#,dy# + angle#
endfunction
function control_camera(play#,speed#)
`RECOMMENDED SPEED = 0.25
position camera object position x(play#),object position y(play#),object position z(play#)
xrotate camera camera angle x()-mousemovey()*speed#
yrotate camera camera angle y()+mousemovex()*speed#
if mouseclick()=2 then CamDis#=CamDis#-mousemovey()*speed#
CamDis#=CamDis#+mousemovez()*0.25
if camera angle x()>80 then xrotate camera 80
if camera angle x()<-60 then xrotate camera -60
if CamDis#>-70 then CamDis#=-70
if CamDis#<-300 then CamDis#=-300
`CamDis# = -300
move camera CamDis#
if camera position y()<object position y(play#) then position camera camera position x(),object position y(play#),camera position z()
endfunction
function updateCollision(play#)
updatehorizontalcollision(play#)
updatehorizontalcollision(play#)
x#=object position x(play#)
y#=object position y(play#)
z#=object position z(play#)
inc g#,gravity#
if getverticalstate(play#)=1 then g#=gravity#
if getverticalstate(play#)=-1 then g#=0
if returnkey()=1 then g#=0-jump#
position object play#,x#,y#-g#,z#
updateverticalcollision(play#)
endfunction
List of functions:
Quote: "Objhide()"
-
A snippet Sett found when we were working on Ronin, it basically hides non-shown objects, if I remember correctly it was made by RPGGamer.
Quote: "player_object(play#,shadowflag#,slope#,radius#)"
-
Sets up a cube for test play, that is size 70 (With shadows and collision setup):
-Play# = Object number to set
-Shadowflag# = High performance shadow shader, put '1' in there if you want a shadow to be applied.
-Slope# - the slope angle, I'm using '20' as my angle, but change to fit you.
-Radius# - The collision radius
Quote: "player_object_load(file$,play#,shadowflag#,slope#,radius#)"
Basically the same before, except you load an object instead
-file$ = File name
Quote: "scene_object(file$,scene#,bumpimg#)"
-Load an object without the set up, which requires bump mapping, more features may be added later.
+-file$ - filename
+-scene# - object number
+-bumpimg# - Normal Map image number.
If you want to set up a 'scene object' for collision, use sparky's command:
setupcomplexobject play#,0,2
Quote: "gosub SetUp_Bloom"
Sets up the bloom ready for action, for the moment you will need to go to that label and edit parameters within.
Don't worry about the main collision commands, they are built into my other functions, which should make setting up the scene easier.
USE THESE IN A LOOP
Quote: "control_object(play#,fwdis#,bwdis#,angle#)"
-Choose an object to control
+- play# - object number
+- fwdis# - Distance to move forward
+- bwdis# - Distance to move backwards
+- angle# - The angle to turn left or right with
-Controls are:
WASD - Move object
Mouse - Move Camera
'1' - Set to Camera Type 1 - Moves around player
'2' - Set to Camera Type 2 - Rotates player and moves up and down
Default = Camera type 2
Quote: "control_camera(play#,speed#)"
-Set the camera for control
+- play# - object number
+- speed# - The speed in which the camera moves at recommended value '0.25'
Quote: "updateCollision(play#)"
-Updates the collision of the player, including gravity.
+- play# - Object number.
Quote: "gosub update_bloom"
Updates the bloom, so it can be seen on screen.
Once I have got a complete write of the engine, I will release it officially with tutorials and examples (hopefully)
Enjoy!
[edit]
If you have issues with the engine, let me know, I am about to start the process of debugging, I see Cian already has issues and I am trying to solve them for him. For now, I'll let you download the Shadow Behind the Leaves project folder, which should demonstrate where all goes.
http://files.filefront.com/Shadow_Behind_the_Leaveszip/;6305415;;/fileinfo.html
"Cut down the gods if they stand in your way" - Hakamoto Tsunetomo