
In previous demo (lesson about spot lights) there was a stair and you saw that our code that covers collisions against the terrain is good enough to our avatar goes up in the stair. Why?
Because the avatar sends rays continualy to redefine his position in the Y axis. Look at the figure. At moment 2 the point of contact changes up. So: the avatar jumps to a new position. Realy the avatar does not collide against the floor. His position is recalculed every loop-timestep to be just "flying" over the floor. Very funny :-)
But realy important in this lesson is that we will make a total review of all we have seeing in out tutorial until now, presenting the complete code of an standard/basic application. Is the 3dD Basic Kit, a group of code that you can use like a first step in all projects. Because we have in the kit:
It's usual that, if you are making a 1st person world, you will need this things in your project.
The general schema of the 3dD Basic Kit is:
1 - We have first of all a SETUPSCRIPT where we declare the world and call two sub-scripts: avatarobj and cameraobj:
global myworld
global avatarobj, cameraobj
on preparemovie me
setupobjects
end
on setupobjects
clearglobals
member("world").resetworld()
myworld = member("world")
avatarobj = new(member("avatarobj").script)
cameraobj = new(member("cameraobj").script)
end
In the AVATAROBJ sub-script we define:
property fwvect
property upvect
property distanceforward
property steer
property ang, xvect,avatarheight
global myworld
on new me
upvect = vector(0, 1,0)
fwvect = vector(0,0,1)
distanceforward = 0
steer = 0
myworld.model("avatar").visibility = #none
--for collision
xvect = vector(1,0,0)
ang = 0
avatarheight = 40
--
--for wall collision
myworld.model("avatar").addmodifier(#collision)
myworld.model("avatar").collision.enabled = true
myworld.model("avatar").collision.resolve = true
myworld.model("avatar").collision.mode = #mesh
myworld.model("logo").addmodifier(#collision)
myworld.model("logo").collision.enabled = true
myworld.model("logo").collision.resolve = true
myworld.model("logo").collision.mode = #mesh
return me
end
on control me
--avatar movements
if keypressed(126) then
acc = 0.025
else
acc = 0
end if
if keypressed(123) then
turn = 2
else
if keypressed(124) then
turn = -2
else
turn = 0
end if
end if
distanceforward = distanceforward + acc - 0.015
if distanceforward < 0 then distanceforward = 0
if keypressed(125) then distanceforward = - 0.8
if distanceforward > 0.8 then distanceforward = 0.8
--rotation of the avatar
steer = (steer + turn)*0.8
if steer < -5.0 then steer = -5.0
if steer > 5.0 then steer = 5.0
myworld.model("avatar").transform.rotate(myworld.model("avatar").worldposition, upvect, steer)
--translation of the avatar
rotator = transform()
rotator.rotate(vector(0,0,0), upvect, steer)
fwvect = rotator*fwvect
movement = ( fwvect*distanceforward )
myworld.model("avatar").translate(movement ,#world)
-- puting the avatar in the floor using data returned by seekfloor method
floordetails = seekfloor("avatar", "land", "world", avatarheight, upvect)
ang = 0
if floordetails[1] = 0 then
myworld.model("avatar").translate(-movement ,#world)
else
if floordetails[2] <> 0 then
xvect = floordetails[2]
ang = floordetails[3]
end if
end if
myworld.model("avatar").transform.rotate(myworld.model("avatar").worldposition, xvect, ang)
end
--seekfloor method
on seekfloor modelname, floorname, worldname, modelheight, directionvector
flag = 0
tiltangle = 0
tiltvector = 0
alist = myworld.modelsUnderRay(myworld.model(modelname).worldposition - directionvector*100 ,directionvector,5, #detailed)
repeat with i = 1 to count(alist)
if alist[i].model.name contains floorname then
a = alist[i].isectposition
av = vector(0,(-myworld.model(modelname).worldposition.y + alist[i].isectposition.y +modelheight ), 0)
myworld.model(modelname).translate(av, #world)
ay = -directionvector
tiltangle = -ay.anglebetween(alist[i].isectnormal)
tiltvector = alist[i].isectnormal.perpendicularto(ay)
flag = 1
exit repeat
end if
end repeat
return [flag, tiltvector, tiltangle]
end
In the sub-script CAMERAOBJ we define:
property mycam
property aux
property cameraoffset
global avatarobj,myworld
on new me
aux = myworld.newmodel("aux")
mycam = myworld.camera[1]
aux.visibility = #none
mycam.projectionangle = 75 -- read about this properties in previous lesson
cameraoffset = [0.0001, -0.0003]
return me
end
on control me
aux.transform.interpolateto(myworld.model("avatar").getworldtransform(), 100.0)
camvect = (avatarobj.upvect*cameraoffset[1]+ avatarobj.fwvect*cameraoffset[2])
aux.translate(camvect, #world)
aux.pointat(myworld.model("avatar").worldposition )
lag = 100
mycam.transform.interpolateto(aux.getworldtransform(), lag)
end
In the TIMESTEP script installed in the frame where the world is
global avatarobj, cameraobj, myworld, trigger
on prepareframe me
control avatarobj
control cameraobj
end
on exitFrame me
go the frame
end
To start an action trigerred we have sometimes the Macromedia standard behaviors: by example:
We will talk about this soon.
The script TRIGGER (there are not included in the kit) could be:
global avatarobj on exitFrame me avatarobj.flagd=1 go to "play" end
You need to understand very well this group of scripts that are the "soul" of the inteligence of all the 1st person worlds that you will make.
DOWNLOAD OF THE 3dD BASIC KIT
The 3dD Basic Kit is the basic scenery where you can make the exercices of our tutorial.
We have a basic 3ds max world: kitb.max that is only a floor with an auxiliary sphere to be used to put the spot-light to iluminate the logo.
The Director world is: kitb.dir. It has the four scripts that we are talking about:
Normaly you will put YOUR code in the avatarobj script using one of the two methods: new - for one-time execution code or control - for code inside the timestep-loop.
In our lessons we will use the 3dD Basic Kit like a pre-requirement for the exercices and make references to it.
To download the files right-click the links, saving the target in your PC:
This is the 3dD Basic Kit world - it's a real Director world. You can walk and go up-stairs:
PREVIOUS LESSON NEXT LESSON T. CONTENTS HOMEPAGE
